Hello i'm trying to make an asp web application that send SMS to registered users whenever they receive an email , I'm a beginner when it comes to coding so I can't think of a pratique way to make it do that.
I thought of adding a timer and making it check every 5 minutes in the sql database for every user if there was something new but this will make the application slow assuming we have hundred of users.
I also thought of adding a trigger in the database and having it do the work but I can't find how to use sql trigger in visual studio, any Ideas that could help me ?
Thank you.
I thought of adding a timer and making it check every 5 minutes in the sql
database for every user if there was something new but this will make
the application slow assuming we have hundred of users ,
Why? Because I do not see that. Except slow means an average delay of 2.5 minutes. Then check every minute. Average delay down to 30 seconds.
I also thought of adding a trigger in the database and having it do the work but
I can't find how to use an sql trigger in visual studio , any Ideas that could
help me ?
Finding triggers? LEARN YOUR TOOLS. Seriously. But I personally would fire anyone using a trigger to then call an external program to send the SMS - the database is NOT where you want this.
Want it fast? Use a trigger to write a message to message broker (in sql server, it is a subsystem you likely do not know about because of not reading the documentation).
Have your app having some threads wait for messages there. There IS a special syntax to wait until a message arrives.
Voila, case closed.
May I suggest that you split up your code, put a message on a queue with the recipient and content of the SMS, and then create a separate webjob/service that subsbcribes to that queue and sends SMS whenever there is an entry there.
This gives you a clean separation of concerns, and removes the need to do polling to the database.
A couple of queues that are nice and easy to get started with:
Azure Storage Queue
Azure Service Bus Queue
Related
I have this scenario:
an Asp.net/c# website with a rss reader/parser and a database. I would like to update the rss every x hours and save the new records in a database.
The indications I am receiving are that I need a dedicated server and a scheduler. I am not an expert about Asp but knowing that the server has a system time, it is natural to me thinking about using a timer.
I see that there is a timer in Asp.
Would it be possible to use a timer properly wrapped in the code, to get the functionality of the above scenario?
Yes you could use a timer inside a class (static or something that always has an instance activated) to accomplish this. One thing you are going to have to watch out for is that App Pools by default timeout and shutdown after 20 minutes of inactivity so you will want to change the settings to make it constantly run or make sure that something sends a request to the server every so often to keep the App Pool alive.
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 windows service that listens to a message queue and processes request from this message queue. I have an initial application that places messages in the message queue. The service itself actually creates some files on the fly and when these files are created I need the initial application to grab these files and essentially paint them on the screen. (the files are an html format) For the time being I have the application and the service running on my local machine so the files are just located on the c: drive of my machine.
My issue is that I need some way to signal my initial application that the service has picked up and processed my msg successfully or unsuccessfully. I have been researching how to do this and have come up empty handed. I have also been brainstorming ideas as to what I could do to make this work. One non viable solution was to insert a record into a database table when sending a msg to the queue and upon processing finished within the service update the queue record, all the while having the application query the table checking for a status of processing complete. I don't like this idea at all as I feel like it would be too intensive, and I believe that there has to be a better way of doing this. I am going to continue to research how this can be done, but I am definitely hoping someone has a better solution to this than my sql query running over and over again. Note in the past I have tried to query the event viewer for processes, and I know how to do this, but this will take far too long for the timing I am looking to accomplish.
I ended up going with the route of inserting record into database prior to call windows service. Then when the windows service finished with its process it updated this db record. All the while my page is calling a webservice via javascript which checks the db to see if the flag on the record has been set to done processing. This allows me to determine when the windows service has finished processing my msg from the queue.
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.