Then you can implement a queue triggered background task to retrieve message(s) from the queue and update your database record(s). TimeSpan duration = user_specified_datetime.Subtract(datetime_now) Īwait theQueue.SendMessageAsync(newPost, duration, default) If (null != await theQueue.CreateIfNotExistsAsync()) QueueClient theQueue = new QueueClient(connectionString, "mystoragequeue") While your application user want to create a new post and specify a publishing date time, you can insert a new message (with specified visibilityTimeout based on user expected datetime) into the queue, so that this new inserted message would be only visible at specified date time in the queue. It seems that you'd like to execute a background task/job at a user specified datetime, to achieve the requirement, you can try to use some message queue services, such as Azure Queue Storage, which enable us to specify how long the message should be invisible to Dequeue and Peek operations by setting visibilityTimeout. Then, how can I schedule a background task for it by using hosted services to run only for one time and to change a flag in database and save changes after that? I want to add a specific functionality to it to schedule publishing a post in the future in a date and time specified by post author.For example, if we receive this date and time from user : 14:08:07. To see the full implementation in my Github repo which has the possibility to recover the scheduled tasks from database after restarting the server, use the following link: You could read more about background tasks with hosted services in ASP.NET Core here from official Microsoft docs: If you look at the part of the above code in which I passed (item.ScheduledFor - DateTime.UtcNow) as Timer class constructor's third parameter to initialize a new instance of it, I actually ask the timer to do a specific work in a specific time I stored as a DateTime in item.ScheduledFor. Using (var scope = Services.CreateScope()) Private async Task ScheduledItemChangeState(DateTime scheduleFor) Changes after the scheduled time comes ScheduledItemChangeState(DateTime.UtcNow).Wait() _timer = new Timer(DoWork, null, (item.ScheduledFor - DateTime.UtcNow).Duration(), TimeSpan.Zero) Private Task SetTimer(CancellationToken stoppingToken) TaskQueue.QueueBackgroundWorkItem(SetTimer) Public async Task SetAndQueueTaskAsync(ScheduleTypeEnum scheduleType, DateTime scheduleFor, Guid scheduledItemId) Set task to schedule for a specific date and time _crontabSchedule = CrontabSchedule.Parse(Schedule, new CrontabSchedule.ParseOptions Public SomeScheduledService(SomeTask task) Private const string Schedule = "0 0 1 * * *" // run day at 1 am Private readonly CrontabSchedule _crontabSchedule public class SomeScheduledService: IHostedService The implementation below is lightweight (no architecture imposing libs) and no polling. I combined CrontabSchedule with IHostedService.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |