Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Programmatically schedule a job to run a certain amount of time from now

$
0
0

The application I'm working on needs to interface with an external system that prints physical person identification cards. This interface is implemented through a writeable view. This view has 3 columns that are relevant here: the Status column (indicating whether the record is ready to print or whether it has been printed); the PrintCount column (initially NULL, but this gets incremented by the card printer when a card is printed), and the PrintDate column (the date/time when a card was printed).

From my end, I provide a set of all records that are ready to be printed in this view, based on a particular value in the Status column (e.g. ReadyToPrint). On the other end, the card printing system picks up the records from this view and sends them to a card printer. When the card printing system processes a given record, it increments the PrintCount column of the view before actually printing that record. Because of this, I have no idea when the card printing system is actually done processing a record. In other words, the card printing system only signals that it started processing a record in the view but not that it finished processing that record.

My initial solution for this was to set a trigger on the view that sets the PrintDate column equal to GETDATE when there is an update to the PrintCount column. Separate from this trigger, I also have a scheduled job that runs every 10 seconds and updates the Status column to the value Printed for any record that has a status of ReadyToPrint and a PrintCount > 0 whose PrintDate is within the last 10 seconds. So the scheduled job looks like this:

UPDATE Mydata
SET Status = Printed
WHERE PrintCount > 0
AND Status = ReadyToPrint
AND PrintDate >= DATEADD(SECOND, -10, GETDATE())

This job is necessary to clean up records that have already been printed from the view. This cleanup cannot take place in the trigger itself because otherwise the record being processed will disappear from the view right when the card printer fetches it (remember, the card printer updates the PrintCount column before actually printing). While it's a kludge, this approach works; the card printer is a completely closed system and it cannot be changed to signal when it's done processing a record. The 10-second interval was deemed to be the right amount of time by trial and error. It cannot be increased because then the same record could be printed multiple times.

The problem is that running this scheduled job every 10 seconds has a significant performance impact on the server when doing other processing, so I'm basically looking for a way for the trigger to request that the cleanup action take place one time only in the next 10 seconds. I've looked at creating a new one-time scheduled job and scripting it, but scheduled jobs appear to belong in the msdb database and the USE keyword is not allowed in a trigger. Any suggestions on how to do this in a trigger? Perhaps with dynamic SQL?


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>