我打赌有人已经解决了这个问题,也许我正在使用错误的谷歌搜索条件来告诉我答案,但这是我的情况.
我有一个我想要运行的脚本,但我希望它只在预定时运行,一次只运行一个.(无法同时运行脚本)
现在粘性部分就是说我有一个名为"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)]
Run Code Online (Sandbox Code Playgroud)
这样,脚本首先检查是否没有命令正在运行,然后运行第一个尚未运行的命令,直到给定时刻没有更多命令可以运行。此外,您还可以通过查询数据库来查看正在执行什么命令。
一个潜在的陷阱:如果 cron 脚本被终止,计划任务将保持在“executing_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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
896 次 |
| 最近记录: |