/ Published in: Ruby
Example of how to use a cron like a daemon and avoid having the cron run two instances of the same script.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
begin Lockfile.new('cron_mail_fetcher.lock', :retries => 0) do config = YAML.load_file("#{RAILS_ROOT}/config/mail.yml") config = config[RAILS_ENV].to_options puts "Running MailFetcher in #{RAILS_ENV} mode" fetcher = Fetcher.create({:receiver => MailReceiver}.merge(config)) fetcher.fetch puts "Finished running MailFetcher in #{RAILS_ENV} mode" end rescue Lockfile::MaxTriesLockError => e puts "Another fetcher is already running. Exiting." end