Designing an asynchronous task library for ASP.NET

后端 未结 4 669
谎友^
谎友^ 2021-01-05 20:52

The ASP.NET runtime is meant for short work loads that can be run in parallel. I need to be able to schedule periodic events and background tasks that may or may not run for

4条回答
  •  孤独总比滥情好
    2021-01-05 21:07

    I tell you what I have do.

    I have create a class called Atzenta that have a timer (1-2 second trigger). I have also create a table on my temporary database that keep the jobs. The table knows the jobID, other parameters, priority, job status, messages.

    I can add, or delete a job on this class. When there is no action to be done the timer is stop. When I add a job, then the timer starts again. (the timer is a thread by him self that can do parallel work). I use the System.Timers and not other timers for this.

    The jobs can have different priority.

    Now let say that I place a job on this table using the Atzenta class. The next time that the timer is trigger is check the query on this table and find the first available job and just run it. No other jobs run until this one is end.

    Every synchronize and flags are done from the table. In the table I have flags for every job that show if its |wait to run|request to run|run|pause|finish|killed|

    All jobs are all ready known functions or class (eg the creation of statistics).

    For stop and start, I use the global.asax and the Application_Start, Application_End to start and pause the object that keep the tasks. For example when I do a job, and I get the Application_End ether I wait to finish and then stop the app, ether I stop the action, notify the table, and start again on application_start.

    So I say, Atzenta.RunTheJob(Jobs.StatisticUpdate, ProductID); and then I add this job on table, open the timer, and then on trigger this job is run and I update the statistics for the given product id.

    I use a table on a database to synchronize many pools that run the same web app and in fact its work that way. With a common table the synchronize of the jobs is easy and you avoid 2 pools to run the same job at the same time.

    On my back office I have a simple table view to see the status of all jobs.

提交回复
热议问题