0

I want to setup rake tasks to run via cron. That is easy, but what is not easy is ensuring that only one copy of that rake task is running at a time. I imagine I could use ps on the system to check and then exit the rake task if it is already running, or I could do a delayed job structure where I serialize the name of the task and upon completion I remove it form the DB, but what do you all recommend?

tesserakt
  • 3,231
  • 4
  • 27
  • 40

2 Answers2

0

You can use https://rubygems.org/gems/only_one_rake, like namespace :rails do desc "online user count" only_one_task :online_user_count => :environment do loop { OnlineUser.count; sleep 1 } end end

mvj3
  • 21
  • 4
0

Unix solved this issue with pid files. Pid files are located in /var/run and contain the programs process Id. Here is the man page: http://fuse4bsd.creo.hu/localcgi/man-cgi.cgi?pidfile+3

You might find it a bit old fashioned (and I agree), but it's an often used and proven method.

Johan
  • 1,958
  • 11
  • 20
  • That looks very good, actually! I can't find any good tutorials on how to to effectively use it, so I am going to take the DelayedJob method of checking PID files and use it for my purposes. Thank you. – tesserakt Sep 10 '10 at 19:45