How to update the database once when the timer expires? - c#

I am using an ASP.NET web form and I have two fields Start_date and End_date in it.
I want to update a particular field in SQLServer once when the time goes beyond the End_date (i.e. the deadline is expired).
e.g. if the End_date is on 06/09/2015, then the SQLServer field should be updated as Expired at 12.01 AM on 06/10/2015.
I am new to ASP.NET and please let me know if you have any ideas. Thank you

There are way too many ways to do such thing. I.e:
making a SQL script that does that and adding that script as scheduled job in SqlServer
or, creating a WindowsService that sleeps and wakes up every hour or six and checks for expired records
or, adding a module like Hangfire to your ASP application and adding such scheduled task there
..
Those three are probably the most often used.
Anyways, it's always about some scheduler and some job to do periodicaly. Think how to write one "run" of the job (find expired, mark/delete them), then read about what schedulers you can use, then glue it together and configure the scheduler and done.

There are several ways to do this, but if all you need to do is make database changes at a given time, then I recommend using the SQL Server Agent. You can set it up to run just once or on a recurring schedule and you can set it up to run a stored procedure or you can feed it straight SQL commands.

Related

Reschedule already planned jobs. Despite changing the EnqueueAt value, the task is executed according to the initially specified date

I’m in the process of deploying an application that uses Hangfire’s solutions in 1.7.24 version with the MySQL database.
I am struggling with one problem. The application automatically schedules actions and adds these calls as BackgroundJob in hangfire. The problem is that these jobs cannot run on user-defined holidays or weekends. This means that if a job that is to be processed on a weekend or holiday should be moved to the next possible date.
Is it possible to move an already scheduled job, or is the only solution (which I don’t quite like and want to do) to delete the current scheduled job and add a new one with the updated date?
I tried to deserialize the data available in the table “hangfire.hangfirestate” for the corresponding scheduled job, I changed the value of the “Data” column for “EnqueueAt”.
The Hangfire dashboard correctly shows in this case that the job is to be performed on the date and time I changed manually, but unfortunately for some reason the Hangfire server does not take these changes and procced the job execution on this initial date.
Can you help with this topic? Thank you in advance.

Update db table with some schedule

I am developing simple auction app with c# and SqlServer.
I have an Auction entity. For simplicity it has next structure:
AuctionId
ItemId
StartPrice
StartDate
Status
CurrentPrice
BidderId
LastBiddedat
A know that it is not in normal form, it does not matter here. Not production code.
What I need is to update Auction Status if there have not been any bids in last 2 minutes. No bids - auctions status sets to Inactive.
I can track db changes in c# code via SqlDependency and it works perfectly.
Now I need a mechanism to update db when auction time is up. I would like to do it on database layer for simplicity.
It can be done via SQL Agent, there even is an example how to do it how to schedule a job for sql query to run daily? .
Maybe you have another options?
Just curious.
Common solutions are polling or implementing some sort of scheduler. Creating a SQL Dependency won't really help as this is triggered from a database data change. Your trigger is 'the absence of a change'.
Polling from the application is generally a bad idea as this creates unwanted network traffic amongst other issues.
As you mention using the built in SQL Agent Job Scheduler to perhaps run a Stored procedure and update statuses for auctions that are 2+ minutes old and inactive is the best idea. This is part of the database and therefore will create minimal network traffic (if any). It's part of SQL Server and hence is designed to run quickly and smoothly. It's also easy to set up. This would be my recommendation.

ASP.NET Update database every hour

I want to copy one row from table A to table B per hour on my asp.net website. How can I do that? Do I need win32 application? I would like to avoid that.
This sounds like you want to create a stored procedure and use the Sql Server Jobs scheduler to run it every hour. See here for details of setting that up
Or if you are using Sql Express see this question "How to run a stored procedure every day in SQL Server Express Edition?"
Ideally you wouldn't do this using IIS etc, unless there is some reason you don't have access to the database.
You can use Timer.
If you use SQL Server you can also schedule a job on the DB directly.
I would agree with brodie. Alternativly create a SSIS package and run either as a SQL Job or a scheduled task from command line(using DTEXEC).
http://msdn.microsoft.com/en-us/library/ms365302.aspx - Shows you how to create a basic solution.
http://decipherinfosys.wordpress.com/2008/09/17/scheduling-ssis-packages-with-sql-server-agent/ - Shows you how to schedule your task.
For your exercise either would do the trick. I would suggest using either that you feel more familiar with. But certainly wouldnt suggest creating a timer driven service.
You could create a webpage that accomplishes this and use a free service like pingdom.com to call that page every hour.
EDIT:
Mine is the easy route, probably not the best solution but quick and simple
You can check this out also:
https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/
see this example for creating a new thread in global asax, http://www.mikesdotnetting.com/Article/129/Simple-task-Scheduling-using-Global.asax
Be aware though if the website goes down the process will not continue to run, if this is a problem a sql batch job or windows service would be better suited, and if the task is resource intensive it would be better to do this on another machine anyway as the global asax method will take away processing resources from your website

Database Scheduled manipulation

I've wrote a Restaurant Management application.
I have a Database based on SQL Server 2005 which has one table named OrdersItems. Every 5 minutes I want to read all rows of this table and based on a specific criteria Update some fields.
I don't want to do that in my main application and I prefer to have an Alternative engine to perform this.
Which method is the best method to perform such task ? Also note that this Table (OrdersItems) is under process every time because main application must be always running and get new Restaurant Orders.
You can create a SQL Server Agent job that does the update every five minutes.
If you are using SQL Server Express edition, you can't use SQL Server Agent because it's only included in the "bigger" versions of SQL Server.
In this case, you can create your jobs manually using batch files and Windows Task Scheduler.
I definitely agree with Christian and dougajmcdonald's points about using SQL Task/ Maintenance. However since you included c# in your tags an alternative is to create a Windows Service.
The benefits of this approach
Can run on a machine that doesn't have the SQL Server Agent installed (including express editions)
Can be run outside the context of a user login.
Has standard stop start pause continue mechanism that's well understood.
If the service itself fails will likely result in an event log
This answer contains a template for a windows service that periodically gets data and executes. You may simply want to change the DoStuff method to execute a stored procedure
Create a dialog timer and let it activate a stored procedure. This has the advantage of being fully contained inside the database (no external process), it does not require SQL Agent (runs on Express) and is completely crash resilient at the point it will survive database detach/attach and backup/restore operations (the scheduled job will run after recovery on the new restored database).
I would expect a SQL Task / Maintenance plan would be the best for this.
You can set them up for whatever interval you want, specifying a SQL statement, maintenance task etc you want to run.
You can also setup alerts etc if you want to know when it fails for example.
Deploy a cron job on a server with access to the database which is started every 5 minutes and processes your data, using transactions. I see one problem there: If the amount of data to be processed is large, it could quite work more than five minutes.

How to run periodic processes on SQL Server 2008

I've built a very simple chatroom-like ASP.NET application which displays current Online/Offline users:
I have a Table with a DateTime column used as a TimeStamp. Every time a user causes a Postback or similar Get event, I update the TimeStamp. I want to, on the server, create a periodic process of some sort that I can use to check how long a user has been inactive given that I know the last time they were active. Once they have been deemed inactive (after a few minutes lets say), I want to set the value of another column to mark them as "Offline".
Any suggestions would be appreciated.
You could create a Sql Server Agent Job that runs periodically.
http://msdn.microsoft.com/en-us/library/ms187910.aspx
http://msdn.microsoft.com/en-us/library/ms186273.aspx
While the answer given above for using SQL Server Agent is a very good solution, have you considered having a component (a WCF component maybe) on the server which is doing all the state management, instead of managing the state in the database directly?
We do something similar by adding a simple webservice to the Asp.Net application, and calling it at a configurable time interval from a windows service running on the same web-server. This allows us to use our business logic, while keeping it all in one place - the web application.

Categories