任务 API - 处理已经完成的任务

发布于 2024-10-09 19:08:26 字数 242 浏览 0 评论 0原文

我正在制作一个 API,并有一个接受任务并运行它的函数。任务成功完成后,其状态将设置为“已完成”。现在,假设 API 用户意外地(或出于任何原因)将相同的任务(或任何其他已完成的任务)发送回相同的函数。 API 应该做什么?

  • 抛出异常
  • 假装我已经重新运行任务并告诉用户(通过事件或其他方式)它已完成/完成(再次)。
  • 什么都不做,直接忽略它。

对于这样的事情有标准或最佳实践吗?

I'm making an API and have a function which takes a task and runs it. When the task is finished successfully, it's status is set to 'Completed'. Now, lets say the user of the API accidentally (or for whatever reason) sends that same task (or any other already completed task) back into the same function. What should the API do?

  • Throw an exception
  • Pretend as if I've rerun the task and tell the user (through events or whatever) that it is done/completed (again).
  • Do nothing and just ignore it.

Is there a standard or best practice for something like this?

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

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

发布评论

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

评论(1

请别遗忘我 2024-10-16 19:08:26

假装重新运行隐藏了可能是用户错误的内容 - 这可能会导致死锁或其他逻辑错误(即 - 我创建一个事件,等待它并运行一个应该在某个时刻重置它的任务 - 它永远不会发生,死锁)。如果每个成功的任务运行调用两次,完成处理程序也可能会失败。

不执行任何操作或多或少是相同的 - 完成处理程序现在不会失败:),但它们根本不会被调用 - 如果完成处理程序与生成线程执行必要的通信,则更有可能出现错误。

最糟糕的是——这些可能发生也可能不发生,具体取决于时间。即,当用户第二次调用该函数时,该任务可能仍在运行(顺便问一下,然后你会做什么?)

因此,除非任务状态为“未启动”,否则请抛出异常。用户可以随时检查状态并在不太可能需要的情况下执行必要的处理。

Pretending to rerun hides what's probably a user error - this can lead to deadlocks or other logic bugs (i.e. - I create an event, wait on it and run a task that should reset it at some point - it never happens, deadlock). Also done handlers may fail if invoked twice per one successful task run.

Doing nothing is more or less the same - done handlers can't fail now :), but they are not invoked at all - a bug is more probable if done handler performed necessary communication with the spawning thread.

The worst thing is - these may happen or not happen, depending on the timing. I.e. the task may still be running by the time the user calls the function the second time (what do you do then, by the way?)

So, do throw an exception unless task status is "not started". The user can always check the status and perform the necessary processing in the unlikely case she needs it.

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