简体   繁体   English

在服务器上执行作业并将用户重定向到其他页面

[英]Execute job on server and redirect user to different page

I have a process that users run from my website. 我有一个用户从我的网站运行的过程。 Sometimes they select a small amount of data and other times it's a large amount. 有时他们选择少量数据,而其他时候则选择大量数据。 What I want to do is if its a large amount when the user submits the job I want to start the process of creating the data set on the web server and when the job finishes I have a function to check for errors. 我想要做的是,当用户提交作业时,它是否大量?我想开始在Web服务器上创建数据集的过程;当作业完成时,我具有检查错误的功能。 At the same time that this process starts I want to redirect the user to another page that says they will receive an email when the job is complete. 在此过程开始的同时,我想将用户重定向到另一个页面,该页面上说他们将在作业完成后收到一封电子邮件。
Currently I'm doing it in a synchronously which only works on the small jobs. 目前,我正在同步执行此操作,仅适用于小型工作。 The big jobs time out after 5 minutes on firefox 30. Here is my process code 大作业在firefox 30上5分钟后超时。这是我的过程代码

try
{
  Process p = new Process();
  p.StartInfo.Arguments = " -config 'C:\\temp\\Web.cfg'" ;
  p.StartInfo.CreateNoWindow = true;
  p.StartInfo.FileName = "sas.exe";
  p.StartInfo.RedirectStandardOutput = true;
  p.StartInfo.UseShellExecute = false;
  p.Start();
  p.WaitForExit();
  p.Close();
}
catch (Exception ex)
{
  Console.WriteLine(ex.Message);
}

errDesc = clsUtil.CheckErrors("test.log", "ERROR");

I'm very new to this type of web programing so I apologize if I miss something obvious. 我对这种类型的Web编程非常陌生,所以如果我错过明显的事情,我深表歉意。

Thank you 谢谢

EDIT: 编辑:

Here is the code i'm currently using and it seems to work. 这是我当前正在使用的代码,它似乎可以工作。 Do you see any issues arising from this code? 您看到此代码有任何问题吗? I tested it with 4 browsers running big jobs and they are redirected after submit and I received the emails I expected that the jobs are complete. 我用运行大量任务的4种浏览器对其进行了测试,提交后它们便被重定向,并且收到了我希望任务完成的电子邮件。 Do I need to dispose of the thread? 我是否需要处理线程?

if ((nCols > nColsMax) || (ckbEmail.Checked == true))
{
 Thread tr = new Thread(() => MakeDataThread(cartID));
 tr.Start();
 Response.Redirect("../EmailID.aspx?Email=" + Session["User"].ToString());
}

public static void MakeDataThread(int iCartID) 
{
 clsUtil uUtil = new clsUtil();
 uUtil.RunAJobThread(iCartID);
}

Within uUtil.RunAJobThread I take the cartId and go to the oracle database and select the data I think create a document then I send the user an email. 在uUtil.RunAJobThread中,我选择cartId并转到oracle数据库,然后选择我认为创建文档的数据,然后向用户发送电子邮件。

As long as the jobs aren't stepping on top of each other for a shared resource (like a file etc...) You have to do this with care and test it but yes what you need to do is turn your method into an async or make your method thread out the call that invokes this process code. 只要共享资源(例如文件等)的作业没有彼此重叠,您就必须谨慎进行并测试它,但是是的,您需要做的就是将方法转换为异步处理或使您的方法退出调用此过程代码的调用。

You would probably want to make a db table to support this, upon the web request generate a row in this new table (eg Task). 当Web请求在此新表中生成一行(例如Task)时,您可能希望创建一个db表来支持此功能。 Generate a TaskId, use this id to update/report status. 生成一个TaskId,使用此ID更新/报告状态。

The taskId would be sent back to the web client via a redirect and that TaskId could be used to query status. taskId将通过重定向发送回Web客户端,并且该TaskId可用于查询状态。 Or you could use session if this is a single server or you don't want to put the back end to it. 或者,如果这是一台服务器,或者您不想将后端放置在服务器上,则可以使用会话。

To summarize: 总结一下:

  • Receive request 收到要求
  • Generate a TaskId (DB or some static factory of your own) 生成TaskId(数据库或您自己的一些静态工厂)
  • Send that TaskId to the AsyncTask so it can use it to report progress. 将该TaskId发送到AsyncTask,以便它可以使用它报告进度。 (This will execute under a new thread) (这将在新线程下执行)
  • Send a redirect with the TaskId to client ( this will happen almost immediately..) 发送带有TaskId的重定向到客户端(这几乎会立即发生。)

Now your client can call a page with the taskId and you can reply with status while the AsyncTask is processing/completing 现在,您的客户端可以使用taskId调用页面,并且您可以在AsyncTask处理/完成时以状态回复

What I've done in the past for long running processes executed from a web page is to: 过去,对于从网页执行的长时间运行的流程,我要做的是:

1) Write the user's data input values to a table. 1)将用户的数据输入值写入表格。

2) Notify the user that they will be notified when the data is ready. 2)通知用户数据准备就绪时将通知他们。 When they confirm this, they are redirected wherever you want them to go. 当他们确认这一点时,他们将被重定向到您希望他们去的任何地方。

3) Now, you can perhaps write a console application that performs the long-running process. 3)现在,您也许可以编写一个执行长时间运行的控制台应用程序。 Run the console application from the server however often you need to. 从服务器运行控制台应用程序,但是您经常需要这样做。

4) Finally, when the data is processed, notify the user via email. 4)最后,在处理数据后,通过电子邮件通知用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果用户已经登录,请重定向到其他页面 - If user is already logged in, redirect to a different page 服务器端重定向到用户界面中的其他选项卡 - Server Side Redirect To Different Tab In User Interface 如何将用户重定向到其他服务器并包含HTTP基本身份验证凭据? - How to redirect a user to a different server and include HTTP basic authentication credentials? 如果用户已经登录 Blazor 服务器,如何将身份登录页面设置为启动页面并将用户重定向到仪表板 - How To Make Identity Login Page As Startup Page And Redirect User to Dashboard If User Is Already Logged-in In Blazor Server 重定向到新窗口中的其他页面 - Redirect to a different page in a new window 将页面重定向到其他Web浏览器 - Redirect a page to a different web browser 使用消息将用户重定向到登录页面 - Redirect user to Login page with message 将用户重定向到自己的市场页面 - Redirect user to own Marketplace page 登录后,用户根据asp.net中的角色重定向其他页面 - After Login User redirect different Page according to Role In asp.net 如何在 .ASP.NET Core MVC 中使用多个登录页面和未经授权的用户重定向不同的登录页面 - How to use multiple login pages in .ASP.NET Core MVC and unauthorized user redirect different login page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM