0

我有一个名为 program.rb 的脚本,并想编写一个名为 main.rb 的脚本来执行以下操作:

system("ruby", "program.rb")

constantly check if program.rb is running until it is done
if program.rb has reached completion
    exit main.rb
end
otherwise keep doing this until program.rb reaches completion{
if program.rb is not running and stopped before completing
    restart program.rb from where it left off
end}

我已经研究过 Pidify,但找不到一种方法来应用它以完全正确地适应这种方式......在如何处理这个脚本方面的任何帮助将不胜感激!

更新:如果在 main.rb 中没有办法,我可以弄清楚如何从它在 program.rb 中停止的地方继续运行脚本

4

2 回答 2

1

It's impossible to "restart script from where it left off" without full cooperation from the program.rb. That is, it should be able to advertise its progress (by writing current state to a file, maybe?) and be able to start correctly from a step specified in ARGV. There's no external ruby magic that can replace this functionality.

Also, if a program terminated abnormally, it means one of two things:

  • the error is (semi-)permanent (disk is full, no appropriate access rights to a file, etc). In this case, simply restarting the program would cause it to fail again. And again. Infinite fail loop.
  • the error is temporary (shaky internet connection). In this case, program should do better job with exception handling and retry on its own (instead of terminating).

In either case, there's no need for restarting, IMHO.

于 2013-03-19T17:51:28.437 回答
1

好吧,这是一种方法。

修改program.rb以采用可选的标志参数--restart或其他东西。

program.rb没有此参数启动时,它将初始化一个文件以记录其当前状态。它会定期将所需的任何内容写入此文件以记录某种检查点。

重新启动program.rb标志启动时,它将读取其检查点文件并在该点开始处理。为此,它必须检查点所有状态更改或安排检查点之间的所有处理是幂等的,以便可以重复它而不会产生不良影响。

有很多方法可以监控program.rb. 最好的方法是使用某种 ping,可能类似于GET /health_check通过套接字或管道的虚拟消息。您可以只拥有一个锁定文件来检测锁定是否仍然存在,或者您可以在启动时记录 PID 并检查它是否仍然存在。

于 2013-03-19T18:00:20.683 回答