使用 ajax 处理长时间的服务器端操作?

发布于 2024-07-27 22:49:05 字数 447 浏览 2 评论 0原文

我有一个特别长的操作,当 用户按下界面上的按钮,我想知道什么是最好的 方式将此信息返回给客户端。

该操作正在填充事实表中多年的数据 这大约需要 20 分钟,所以我不打算让界面变成这样 同步。 即使服务器端产生大量数据, 我仍然希望一切都能保持响应,因为本月的数据 用户当前正在查看的内容将很快更新,这不是问题。

我考虑过在操作完成后设置一个会话变量 并轮询该会话变量。 这是执行此类操作的可行方法吗 事物? 不过我特别关心的是 关于用户离开/关闭浏览器以及所有状态 关于长时间运行的工作丢失。

最好在处理开始和完成时在某处插入一条记录来存放处理记录。 然后创建一些其他类型的界面,以便用户(或多个用户)可以监视当前正在执行/完成/失败的作业?

有谁有我可以看的资源吗?

你是怎么做到的?

I've a particularly long operation that is going to get run when a
user presses a button on an interface and I'm wondering what would be the best
way to indicate this back to the client.

The operation is populating a fact table for a number of years worth of data
which will take roughly 20 minutes so I'm not intending the interface to be
synchronous. Even though it is generating large quantities of data server side,
I'd still like everything to remain responsive since the data for the month the
user is currently viewing will be updated fairly quickly which isn't a problem.

I thought about setting a session variable after the operation has completed
and polling for that session variable. Is this a feasible way to do such a
thing? However, I'm particularly concerned
about the user navigating away/closing their browser and then all status
about the long running job is lost.

Would it be better to perhaps insert a record somewhere lodging the processing record when it has started and finished. Then create some other sort of interface so the user (or users) can monitor the jobs that are currently executing/finished/failed?

Has anyone any resources I could look at?

How'd you do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半夏半凉 2024-08-03 22:49:13

根据我的经验,最好的方法是在服务器端保存为每个用户运行的报告及其状态。 然后,客户端会定期轮询此状态。
基本上,让客户端询问服务器 getRunningJobsFor(int userId) 返回所有正在运行的作业和状态,而不是 checkStatusOf(int session)。

From my experience the best way to do this is saving on the server side which reports are running for each users, and their statuses. The client would then poll this status periodically.
Basically, instead of checkStatusOf(int session), have the client ask the server of getRunningJobsFor(int userId) returning all running jobs and statuses.

淡淡離愁欲言轉身 2024-08-03 22:49:13

您可以使用 JS 计时器定期 ping 您的服务器以查看是否有任何作业完成。 如果用户离开并回来,您将重新启动计时器。 工作完成后,您向用户指示,以便他们可以单击链接并打开报告(我不建议强制加载某些内容,尽管可以完成)

You can have JS timer that periodically pings your server to see if any jobs are done. If user goes away and comes back you restart the timer. When job is done you indicate that to the user so they can click on the link and open the report (I would not recommend forcefully load something though it can be done)

維他命╮ 2024-08-03 22:49:12

会话不太现实,我可能会设计某种任务列表。 这样我就可以保存每个用户的任务记录。 通过这种设计,我将能够显示“已完成”的任务,以让用户了解。

我还将把长时间操作移出工作进程。 这是必需的,因为网络服务器可以重新评级。

而且,是的,我将每隔几十秒通过 ajax 调用从服务器请求状态。

Session are not that realible, I would probably design some sort of tasks list. So I can keep records of tasks per user. With this design I will be able to show "done" tasks, to keep user aware.

Also I will move long operation out of the worker process. This is required because web-servers could be restrated.

And, yes, I will request status every dozens of seconds from server with ajax calls.

七禾 2024-08-03 22:49:11

你不能指望他们会停留 20 分钟。 即使是世界上最合作的用户也必然会去做其他事情、忘记并关闭窗口。 允许如此长的连接时间会破坏任何合理的 HTTP 超时的机会,并且也会让您面临琐碎的 DOS。

正如 Spencer 建议的那样,使用第一个请求启动一个独立于 http 请求的进程,在 AJAX 响应中传递回 id,将 id 存储在会话中或针对该用户的数据库中,或者任何你想要的。 然后用户可以做任何他们想做的事情,并且不会中断任务。 该 ID 可用于轮询状态。 如果将其保存到数据库,用户可以注销,清除他们的 cookie,并且当他们重新登录时,您仍然可以检索任务的状态。

You can't expect them to hang around for 20 minutes. Even the most cooperative users in the world are bound to go off and do something else, forget, and close the window. Allowing such long connection times screws up any chance of a sensible HTTP timeout and leaves you open to trivial DOS too.

As Spencer suggests, use the first request to start a process which is independent of the http request, pass an id back in the AJAX response, store the id in the session or in a DB against that user, or whatever you want. The user can then do whatever they want and it won't interrupt the task. The id can be used to poll for status. If you save it to a DB, the user can log off, clear their cookies, and when they log back in you will still be able to retrieve the status of the task.

故笙诉离歌 2024-08-03 22:49:10

代码的服务器端部分应该生成 Web 服务器外部的进程或与该进程进行通信。 使用网页代码来运行应该由守护进程处理的任务只是草率的工作。

The server side portion of code should spawn or communicate with a process that lives outside the web server. Using web page code to run tasks that should be handled by a daemon is just sloppy work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文