I am working on a scheduler to do certain tasks like send email to users etc. Its a basic one which runs every 5 mins and do its job. I need to make some modifications into that so that it can work on demand. For example, I need to send email to a user at 11:00 AM, to another user at 11:02 AM. How can I make the scheduler to run on time without using a lot of resources on server. I know if I need to send emails every minute, then its sleep time should be one minute or less.
Is there any way to call it on demand or I need to have one thread active all the time to check when to do what and at that time activate scheduler or other process responsible for sending email.
Please suggest. Your inputs will be valuable.
Thanks.
If you need to send emails every minute it would be better to create a service and have the service poll the database every minute to look for work and sleep for 1 minute in between loops.
If you only need to run your code once an hour it would be better to use the Windows Task Scheduler.
Related
Hi I am making a reminder feature for a bot framework chat bot using c#.
What is the best trigger for a proactive reminder? I saw samples using task delay. is it applicable for reminder that can be set to 30 days from now or every 15 days? if I use a post API to send a proactive reminder. Is there a third party app that can trigger the post request with data coming from a database automatically?
You probably don't want to use Task.Delay for the order of days - if you program dies during that time the reminder will be lost.
Usually you would store a DateTime for when the reminder should fire, then have a job that polls every so often and if the current DateTime is after that one, then it fires. This way it will survive reboots/changing instances etc. and as a bonus makes it easier to test your logic and change the reminder date and time after it is saved.
How would you go about creating a global timer all users use? Such as a tournament and every 24 hours the timer resets. How would you make all users see the same time and not base it off the client side?
I'd think to use a service like playfab or app42, but how would this work through them anyway?
Thanks
Since there is no code here im gonna tell you the how to but you will need to do research in order to achieve it probably.
You can have a time span in your server, and every time a user logs in you send the time span since the server started to run minus the time that the server has been running, and the client starts running its own timer taking into account that time span.
Im assuming you are working with a client server model here.
I have written a web application in asp.net. It has some user roles. There are 3 roles which are Manager, Accountant and Employee. The employees write their expenses in a form and send it to Manager. When manager approves it, it'll be sent to Accountant to pay it. I need to have an idea that when manager doesn't approve the employee's expense in 48 hours, it should send an automatic e-mail to Manager's mail.
I thought that I can write another small console application to handle that by checking every hour. But it would waste resources and decrease performance.
I need a good idea to handle that. How should I do?
There are several options, but if I were you I would go with first or second options.
Console App & scheduler
I would create that console application that every time is run perform the check for you.
Then I will have it run using Windows Scheduler in a daily basis (at 00:05) or a hourly basis if you prefer so. This way Windows Scheduler daemon will launch it every hour and the rest of the time your app is not running.
Check this Microsoft link to see how a scheduled task is created in windows.
Restful Web Service & scheduler
As suggested in #marapet answer, having a restful web service that allow you to perform this action instead of a console application would give you the advantage of having all code in your web application.
Similar as previous one, you should only invoke the restful uri to have your action done. As possible disadvantage, you have to get sure that that uri is not accessible to end users. In usual architecture (Web Server --> Application Server --> DB) this restful service should be in the Application Servers, far away from end user access.
Windows Service
Another option is creating a Windows Service that runs all the time and check the time itself so every hour perform the job (maybe using Quartz or similar). But this does not meet your performance requirements.
The performance hit will be small anyway as your service should check every minute to see if an hour has pass and is time to do its job.. a task pretty easy.
The advantage is that a windows service is easier to control and monitor than a Scheduled tasks
DB job
Yet another option... If your app uses SQL Server you can have a t-sql job that runs daily or hourly. I wouldn't recommend this option unless you really have performance problems.
The problem with this is that you would be splitting the logic and responsibilities of your code. A future developer or admin would find hard to maintain your app.
If you'd like to keep the logic within the web application for simplicity (depending on the total size of your solution, this may or may not be desired):
For a given URL, have the web app check for due approvals and sends emails out if needed. Be sure to keep track of emails sent in order to prevent sending the same email multiple times.
Call this URL in a regular interval. You may use a scheduled task or a third party url monitoring service to do this.
You may call the URL with a simple VBScript (or wget, or curl, or powershell, or whatever is fastest for you), which in turn you can automate by using the task scheduler (see also).
An example script in vbscript for calling an URL:
Function LoadUrl(url)
Dim objRequest
Set objRequest = CreateObject("MSXML2.ServerXMLHTTP.6.0")
objRequest.open "POST", url , false
objRequest.Send
LoadUrl = objRequest.responseText
Set objRequest = Nothing
End Function
Checking every hour won't affect performance. Even checking every minute is probably fine, depending on your database. The simplest option is a console program fired as a Scheduled Task. You can also try a Windows Service but they're a bit trickier.
Also give some thought how you'll count the 48 hours. If an employee puts in expenses just before the weekend then 48 hours will probably elapse every time and you'll end up with a manager having lots of emails in their Inbox on Monday morning. That could cause some friction :)
I have a website that allows a user to upload a spreadsheet of items to a database. When the spreadsheet is uploaded it will be assessed for keywords that match individual user interests and email those users an alert to visit the website. However, when the spreadsheet is successfully uploaded the administrator needs to ftp into their server and upload any related images. This can take up to one hour to perform.
I will be executing the email alerts on a new thread and was wondering if, when that thread is called, I can safely call Thread.Sleep(360000) from within it to pause execution for one hour. Would this create a hog on the servers resources? If so is there another way to do this?
I can't create a scheduled task as uploading can happen at any time/day of the week.
You can't be 100% sure that your application pool won't be recycled, your server rebooted, or any other number of events that could cause your thread to disappear out from under you.
You'll have to have some form of scheduled task, that checks for "things older than X" and runs every minute/couple of minutes and processes any as that approach will allow you to recover from pretty much anything (eg. Server looses power for an hour, your scheduled task catches up on all email alerts that should have happened during that time).
Write the necessary information to a queue or file and write a service that processes the queue. It's a much more robust way to handle such tasks. However, I'm curious about your comment:
However, when the spreadsheet is
successfully uploaded the
administrator needs to ftp into their
server and upload any related images.
This can take up to one hour to
perform.
How do you know the task will be completed in one hour? What signals the completion of the task? Why can't you fire off an email at that point, rather than pre-scheduling the alert? I could be missing something, but it seems like you might not be thinking this through very well.
What if the server explodes (replace with more likely event, like "unexpectedly shuts down") after half an hour?
Maybe you should think of a completely different mechanism (store some information in a database, check periodically for requests completed more than one hour ago...).
EDIT: maybe Windows Workflow Foundation could provide you some help for what you are trying to do.
I'm using c# to communicate with twitter and i need to code a schedule system to send twitter messages at a user defined date.
The messages to be sent are in a database with the date and time of deliver.
Which is the best method to check the db for scheduled messages and send it when the time arrives?
How accurate do you need the timing to be? Could you get away with polling the database every 5 minutes, saying "Tell me all the messages which need to be delivered before current time + 5 minutes" and then sending them all? (And marking them as sent in the database, of course.) Does it matter if they're a bit early or late?
You can do the scheduling on the C# side to make it more accurate, but unless you really need to I'd stick with a very simple solution.
(Depending on your database there may be clever ways of getting callbacks triggered etc... but again, I'd stick with the simplest solution which works.)
In addition to the windows service option (or background thread), you could just set up a scheduled task to run an app that polls the DB and sends the tweets once every defined interval.
Windows schedules can be setup using C# if needed and are really easy to set up manually.
There are several ways to do this, but I guess the best way is to set up a Windows Service that will periodically poll (frequency is up to you) the DB for any scheduled tweets that hasn't been sent.
Needless to say you'll need to handle scenarios such as the Internet connection or DB being down, etc.
In fact the solution consists in using a windows service but it can't communicate directly with the ASP.NET MVC app. I've added a Web Service that handles the task and a System.Threading.Timer in Windows Service to periodically call the Web Service.