Architecture - Task Scheduling (Data File Processing) - Windows Service

330 views Asked by At

Task Scheduling architecture question - C# .net.

I have a requirement to extract data from a database and send the results by XML. The process also requires the import of incoming files and the production of corresponding Acknowledgement files. A high lever outline of the process follows the following steps:


  • Creation of a file X (start at 22:00 hours) for SFTP transfer

  • Poll for an Acknowledgement file for X, 5 hours after file X has been transferred.

    • If a file has been received within the 5 hours process it.

    • If no Acknowledgement for X has been received 60 minutes after file X was sent (X processing takes varying amount of time to run), send an email saying 'no file received'.

    • If no Acknowledgement for X has been received 4 and half hours after file X was sent, send an email saying 'no file received'.

    • If no Acknowledgement for X is received after 5 hours of sending X then reject the Acknowledgement.


  • Poll for an incoming file Y which will be received after Acknowledgement for X for 4 hours.

    • If file Y is received within 4 hours process it (multiple revised copies of file Y can be received for processing). Also product an Acknowledgement of Y file for SFTP transfer.

    • If file Y is received after 4 hours of receiving the Acknowledgement for X file then send an Acknowledgement for Y back saying file Y is 'too early'. Also product a 'too early Acknowledgement of Y file for SFTP transfer.

    • If file Y is received after 4 hours of receiving the Acknowledgement for X file then send an Acknowledgement for Y back saying file Y is 'too late'. Also product a 'too late' Acknowledgement of Y file for SFTP transfer.

    • If no file Y has been received 3 and half hours after Acknowledgement for X was received, send an email saying 'no file Y received'.


  • Poll for an incoming file Z which will be received after Acknowledgement for X for 4 hours.

    • If file Z is received within 4 hours process it (multiple revised copies of file Z can be received for processing). Also product an Acknowledgement of Z file for SFTP transfer.

    • If file Z is received after 4 hours of receiving the Acknowledgement for Z file then send an Acknowledgement for Z back saying file Z is 'too early'. Also product a 'too early Acknowledgement of Z file for SFTP transfer.

    • If file Z is received after 4 hours of receiving the Acknowledgement for X file then send an Acknowledgement for Z back saying file Z is 'too late'. Also product a 'too late' Acknowledgement of Z file for SFTP transfer.

    • If no file Z has been received 3 and half hours after Acknowledgement for X was received, send an email saying 'no file Z received'.


Main Question:

Since the processing is not at static times and polling is required 24 hours a day for all file what would be the best approach for architectonic this. A Windows Service seems like a logical solution but are there any better approaches? Is it better to script this through Autosys and call an Executable?

Looking at this approach, [http://www.codeproject.com/Articles/832979/Windows-Services-in-Csharp-with-Timers-Jump-start][1], It appears from the 'Background' section that Timers in Windows Services are not envouraged? ([http://weblogs.asp.net/jongalloway//428303][1])

Many thanks.

1

There are 1 answers

0
Maksym Strukov On

That article from the Background section is a bit strange to me when it comes to using a Timer in a windows service:

A Windows Service which runs a timer needs to run Automatically, which means it will start up before you can log onto the machine and churn away until the machine is shut down. If they hang or have a slow memory leak, they'll continue to be a drag on the system until the service is restarted (or they crash the machine).

Well, if there's a memory leak or your logic just hangs at a particular point in time it's not about Timer or Windows Service it's about a developer who made a bug which has to be fixed. So I can't agree with the author on timers but I do agree that sometimes it's better to use Task Scheduler to run a job. Task Scheduler already has a lot of nice features and you can easily set up an advanced shedule for your job while you might have to reenvent a bycicle with a Windows Service.

I can't say for sure what is best in your case as I don't know all the ins and outs of the solution but I think it's possible to use both Task Scheduler and Windows Service.

Task Scheduler:
1. Create console command to create file X and schedule it to run daily at 22:00. You should save the file creation datetime to a db for example.
2. Create one more job to run every 20 seconds or what is you poll period should be. Then just read the X file creation datetime from the db and perfom the checks from above. For example: Check if the Acknowledgement file for X exists then process it if less than 5 hours passed since X file creation. If the file doesn't exist yet check if 60 minutes already passed and send a notification and so on. One job is enough to place all the polling logic you described for all files.

Windows Service:
1. You can add a timer to create file X but you will have to implement the schedule on your own this is why it's better to use Task Scheduler in this case.
2. However, with a windows service you can listen to File System events instead of polling. Take a look at the FileSystemWatcher class. This could be very handfull especially if the file names are not striclty defined. In this case you can just subscribe to an event and deal with new files only. If you just poll with a Task Scheduler you will have to mantain the file system folder state to check which file are new.

So from what I know from your question I would just create two jobs and shcedule them with Task Scheduler but your solution might be more complex and Task scheduler might not cover all your requirements but I hope I gave you enough info so you can make the right choice.

Hope it helps!