I have the following situation: I have a stored procedure that takes data from a bunch of tables and creates a record into a single table. After that the end user can get some graphics based on the data from the row in the resulting table. The problem is that the collecting of data from multiple tables into the resulting one can have a very long period of running. I would like from my Web application to give the possibility to the user to start this collection of data, and when it's done to inform him about this, so that he can access the graphics diagram based on resulting collected data. My question is how to implement this as best practice.
我有以下情况:我有一个存储过程,从一堆表中获取数据并在一个表中创建一个记录。之后,最终用户可以根据结果表中行的数据获取一些图形。问题是从多个表中收集数据到最终表中可能会有很长的运行时间。我希望从我的Web应用程序中为用户提供启动此数据集合的可能性,并在完成此操作后通知他,以便他可以根据生成的数据访问图形图表。我的问题是如何将其作为最佳实践来实现。
2 个解决方案
#1
Start a background thread on the server to do the processing. You can store the processing results in session state, the database, or some other location.
在服务器上启动后台线程以进行处理。您可以将处理结果存储在会话状态,数据库或其他位置。
On the client side, use an UpdatePanel and a Timer to regularly poll the server for results. When the results are there, disable the timer.
在客户端,使用UpdatePanel和Timer定期轮询服务器以获得结果。当结果出现时,禁用计时器。
#2
I would do something like Andomar suggests, but in a windows service instead of a thread in the asp.net process.
我会像Andomar建议的那样,但是在Windows服务中而不是asp.net进程中的线程。
First I would setup a table in my database that would be used as a work queue - maybe your results table would suffice? This table would contain a job unique id, a job description (used by your service to determine what to do) and a job status code (new, working, finished).
首先,我会在我的数据库中设置一个用作工作队列的表 - 也许你的结果表就足够了?此表将包含作业唯一ID,作业说明(由您的服务用于确定要执行的操作)和作业状态代码(新的,工作的,已完成的)。
Then I would setup a windows service application that would poll this table regularily. Maybe as often av every 5-30 seconds depending on your load. The service would be simple: If it found any new jobs it would mark the first job as "working" and then run the data collection process. When the process was done the service would mark the job as finished.
然后我会设置一个Windows服务应用程序,可以定期轮询这个表。也许通常每隔5-30秒进行一次,具体取决于您的负载。服务很简单:如果找到任何新工作,它会将第一份工作标记为“正常工作”,然后运行数据收集流程。完成该过程后,该服务会将该作业标记为已完成。
Finally, in my asp.net application, I would setup a job-order page where the user could request new data collection jobs. The request would insert a new record to the work queue table with status as new and the job description as needed. Then the page would redirect the user to a new page (or use ajax) that would check the status of the job and reload every XX seconds to check if the job is done. When the job status code is set to finished we display the page where the user can display the report or download the file or whatever.
最后,在我的asp.net应用程序中,我将设置一个作业订单页面,用户可以在其中请求新的数据收集作业。该请求将在工作队列表中插入一条新记录,状态为new,并根据需要输入作业描述。然后页面会将用户重定向到新页面(或使用ajax),该页面将检查作业的状态并每隔XX秒重新加载一次以检查作业是否完成。当作业状态代码设置为“完成”时,我们会显示用户可以显示报告或下载文件的页面。
What I suggest here is a simple implementation of a polling-based job queue. You could of course make a fancier version using MSMQ or something like that, but this should suffice for many if not most situations.
我在这里建议的是基于轮询的作业队列的简单实现。当然,您可以使用MSMQ或类似的东西制作一个更好的版本,但这应该足以满足许多情况(如果不是大多数情况)。
#1
Start a background thread on the server to do the processing. You can store the processing results in session state, the database, or some other location.
在服务器上启动后台线程以进行处理。您可以将处理结果存储在会话状态,数据库或其他位置。
On the client side, use an UpdatePanel and a Timer to regularly poll the server for results. When the results are there, disable the timer.
在客户端,使用UpdatePanel和Timer定期轮询服务器以获得结果。当结果出现时,禁用计时器。
#2
I would do something like Andomar suggests, but in a windows service instead of a thread in the asp.net process.
我会像Andomar建议的那样,但是在Windows服务中而不是asp.net进程中的线程。
First I would setup a table in my database that would be used as a work queue - maybe your results table would suffice? This table would contain a job unique id, a job description (used by your service to determine what to do) and a job status code (new, working, finished).
首先,我会在我的数据库中设置一个用作工作队列的表 - 也许你的结果表就足够了?此表将包含作业唯一ID,作业说明(由您的服务用于确定要执行的操作)和作业状态代码(新的,工作的,已完成的)。
Then I would setup a windows service application that would poll this table regularily. Maybe as often av every 5-30 seconds depending on your load. The service would be simple: If it found any new jobs it would mark the first job as "working" and then run the data collection process. When the process was done the service would mark the job as finished.
然后我会设置一个Windows服务应用程序,可以定期轮询这个表。也许通常每隔5-30秒进行一次,具体取决于您的负载。服务很简单:如果找到任何新工作,它会将第一份工作标记为“正常工作”,然后运行数据收集流程。完成该过程后,该服务会将该作业标记为已完成。
Finally, in my asp.net application, I would setup a job-order page where the user could request new data collection jobs. The request would insert a new record to the work queue table with status as new and the job description as needed. Then the page would redirect the user to a new page (or use ajax) that would check the status of the job and reload every XX seconds to check if the job is done. When the job status code is set to finished we display the page where the user can display the report or download the file or whatever.
最后,在我的asp.net应用程序中,我将设置一个作业订单页面,用户可以在其中请求新的数据收集作业。该请求将在工作队列表中插入一条新记录,状态为new,并根据需要输入作业描述。然后页面会将用户重定向到新页面(或使用ajax),该页面将检查作业的状态并每隔XX秒重新加载一次以检查作业是否完成。当作业状态代码设置为“完成”时,我们会显示用户可以显示报告或下载文件的页面。
What I suggest here is a simple implementation of a polling-based job queue. You could of course make a fancier version using MSMQ or something like that, but this should suffice for many if not most situations.
我在这里建议的是基于轮询的作业队列的简单实现。当然,您可以使用MSMQ或类似的东西制作一个更好的版本,但这应该足以满足许多情况(如果不是大多数情况)。