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 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
