微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sql – cron脚本充当队列还是cron的队列?

我打赌有人已经解决了这个问题,也许我正在使用错误的谷歌搜索条件来告诉我答案,但这是我的情况.

我有一个我想要运行的脚本,但我希望它只在计划时运行,一次只运行一个. (无法同时运行脚本)

现在粘性部分就是说我有一个名为“myhappyschedule”的表,它有我需要的数据和预定的时间.此表甚至可以同时具有多个计划时间,每个表都可以运行此脚本.所以基本上我需要每次脚本触发时都有一个队列,他们都需要等待每个脚本才能完成. (有时这可能需要一分钟才能让脚本有时执行很多分钟)

我正在考虑做的是创建一个脚本,每隔5分钟检查一次myhappyschedule并收集那些已调度的脚本,将它们放入队列,其中另一个脚本可以按顺序执行每个“作业”或队列中的出现.所有这一切听起来都很混乱.

为了延长时间 – 我应该说我允许用户在myhappyschedule中安排事情而不是编辑crontab.

关于这个还能做什么?文件锁和脚本调用脚本?

解决方法

一个列exec_status添加到myhappytable(也可能是time_started和time_finished,请参阅伪代码)

每隔x分钟运行以下cron脚本

cron脚本的伪代码

[create/check pid lock (optional,but see "A potential pitfall" below)]
get number of rows from myhappytable where (exec_status == executing_Now)
if it is > 0,exit
begin loop
  get one row from myhappytable
    where (exec_status == not_yet_run) and (scheduled_time <= Now)
    order by scheduled_time asc
  if no such row,exit
  set row exec_status to executing_Now (maybe set time_started to Now)
  execute whatever command the row contains
  set row exec_status to completed
  (maybe also store the command output/return as well,set time_finished to Now)
end loop
[delete pid lock file (complementary to the starting pid lock check)]

这样,脚本首先检查是否所有命令都在运行,然后先运行not-yet run命令,直到在给定时刻没有其他命令运行.此外,您可以通过查询数据库来查看正在执行的命令.

潜在的缺陷:如果cron脚本被杀死,则计划任务将保持“execution_Now”状态.这就是开始和结束时的pid锁定:查看cron脚本是否正确终止.伪代码创建/检查pidlock:

if exists pidlockfile then
  check if process id given in file exists
  if not exists then
    update myhappytable set exec_status = error_cronscript_died_while_executing_this   
      where exec_status == executing_Now
    delete pidlockfile
  else (prevIoUs instance still running)
    exit
  endif
endif
create pidlockfile containing cron script process id

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐