如何确保rake任务一次只运行一个进程

时间:2021-10-19 01:15:49

I use crontab to invoke rake task at some time for example: every 3 hour

我使用crontab在某个时间调用rake任务,例如:每3小时一次

I want to ensure that when crontab ready to execute the rake task it can check the rake task is running. if it is so don't execute.

我想确保当crontab准备好执行rake任务时,它可以检查rake任务是否正在运行。如果它是这样不执行。

how to do this. thanks.

这个怎么做。谢谢。

3 个解决方案

#1


3  

You could use a lock file for this. When the task runs, try to grab the lock and run the rake task if you get the lock. If you don't get the lock, then don't run rake; you might want to log an error or warning somewhere too or you can end up with your rake task not doing anything for weeks or months before you know about it. When rake exits, unlock the lock file.

您可以使用锁定文件。任务运行时,如果获得锁定,请尝试抓住锁并运行rake任务。如果你没有锁,那就不要跑耙;你可能想在某个地方记录错误或警告,或者在你知道它之前,你可能会在几周或几个月内没有做任何事情。当rake退出时,解锁锁定文件。

Something like RAA might help but I haven't used it so maybe not.

像RAA这样的东西可能会有所帮助,但我没有使用它,所以也许没有。

You could also use a PID file. You'd have a file somewhere that holds the rake processes process ID. Before starting rake, you read the PID from that file and see if the process is running; if it isn't then start up rake and write its PID to the PID file. When rake exists, delete the PID file. You'd want to combine this with locking on the PID file if you want to be really strict but this depends on your particular situation.

您也可以使用PID文件。你有一个文件存放rake进程进程ID。在开始rake之前,从该文件中读取PID并查看进程是否正在运行;如果不是那么启动rake并将其PID写入PID文件。当rake存在时,删除PID文件。如果您想要非常严格,那么您希望将其与PID文件上的锁定相结合,但这取决于您的具体情况。

#2


18  

I'll leave this here because I think it's useful:

我会把它留在这里,因为我觉得它很有用:

task :my_task do
    pid_file = '/tmp/my_task.pid'
    raise 'pid file exists!' if File.exists? pid_file
    File.open(pid_file, 'w'){|f| f.puts Process.pid}
    begin
        # execute code here
    ensure
        File.delete pid_file
    end
end

#3


1  

All you need is a gem named pidfile.

你需要的只是一个名为pidfile的宝石。

Add this to your Gemfile:

将其添加到您的Gemfile:

gem 'pidfile', '>= 0.3.0'

And the task could be:

任务可能是:

desc "my task"
task :my_task do |t|
  PidFile.new(piddir: "/var/lock", pidfile: "#{t.name}.pid")
  # do something
end

#1


3  

You could use a lock file for this. When the task runs, try to grab the lock and run the rake task if you get the lock. If you don't get the lock, then don't run rake; you might want to log an error or warning somewhere too or you can end up with your rake task not doing anything for weeks or months before you know about it. When rake exits, unlock the lock file.

您可以使用锁定文件。任务运行时,如果获得锁定,请尝试抓住锁并运行rake任务。如果你没有锁,那就不要跑耙;你可能想在某个地方记录错误或警告,或者在你知道它之前,你可能会在几周或几个月内没有做任何事情。当rake退出时,解锁锁定文件。

Something like RAA might help but I haven't used it so maybe not.

像RAA这样的东西可能会有所帮助,但我没有使用它,所以也许没有。

You could also use a PID file. You'd have a file somewhere that holds the rake processes process ID. Before starting rake, you read the PID from that file and see if the process is running; if it isn't then start up rake and write its PID to the PID file. When rake exists, delete the PID file. You'd want to combine this with locking on the PID file if you want to be really strict but this depends on your particular situation.

您也可以使用PID文件。你有一个文件存放rake进程进程ID。在开始rake之前,从该文件中读取PID并查看进程是否正在运行;如果不是那么启动rake并将其PID写入PID文件。当rake存在时,删除PID文件。如果您想要非常严格,那么您希望将其与PID文件上的锁定相结合,但这取决于您的具体情况。

#2


18  

I'll leave this here because I think it's useful:

我会把它留在这里,因为我觉得它很有用:

task :my_task do
    pid_file = '/tmp/my_task.pid'
    raise 'pid file exists!' if File.exists? pid_file
    File.open(pid_file, 'w'){|f| f.puts Process.pid}
    begin
        # execute code here
    ensure
        File.delete pid_file
    end
end

#3


1  

All you need is a gem named pidfile.

你需要的只是一个名为pidfile的宝石。

Add this to your Gemfile:

将其添加到您的Gemfile:

gem 'pidfile', '>= 0.3.0'

And the task could be:

任务可能是:

desc "my task"
task :my_task do |t|
  PidFile.new(piddir: "/var/lock", pidfile: "#{t.name}.pid")
  # do something
end