lockfile with cron


/ Published in: Ruby
Save to your folder(s)

Example of how to use a cron like a daemon and avoid having the cron run two instances of the same script.


Copy this code and paste it in your HTML
  1. begin
  2. Lockfile.new('cron_mail_fetcher.lock', :retries => 0) do
  3. config = YAML.load_file("#{RAILS_ROOT}/config/mail.yml")
  4. config = config[RAILS_ENV].to_options
  5.  
  6. puts "Running MailFetcher in #{RAILS_ENV} mode"
  7. fetcher = Fetcher.create({:receiver => MailReceiver}.merge(config))
  8. fetcher.fetch
  9.  
  10. puts "Finished running MailFetcher in #{RAILS_ENV} mode"
  11. end
  12. rescue Lockfile::MaxTriesLockError => e
  13. puts "Another fetcher is already running. Exiting."
  14. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.