run whenever gem in mongrel development environment - ruby-on-rails

I want run whenever gem and I have this task in schedule.rb file:
every 5.minutes do
rake "directory:cleanup"
end
I'm in environment development with Mongrel.
Why this task is not fired every 5 minutes?
What am I doing wrong?
PD: The task works fine from console.

You need to run the whenever --update-crontab command from the root directory of your application. This will add the task to your crontab.
However, if you're using OSX, there probably isn't a crontab to begin with so this won't work.
If you are constantly needing to cleanup some directories in development, I think there might be a better solution. Look into using something like Guard perhaps.

Related

How to test rake tasks with Whenever Gem?

I'm trying to execute a simple rake task using whenever gem but the code isn't being executed.
I already set the environment to development, I updated the cron using the whenever --update-crontab command and the rake task works well if I run the command on console. But, when I run the server the log file is not being generated.
I saw a question here too with the same problem but it was solved setting the environment to development, but didn't work out for me.
My rake task:
namespace :testando do
task :consulta => :environment do
produto = Produto.first
puts produto.nm_produto
end
end
My schedule.rb:
set :output, "#{path}/log/cron_log.log"
set :environment, 'development'
every 1.minute do
rake "testando:consulta"
end
I'm using rails 5.0.0.1 and I'm programing in Cloud9, so I think the OS is Ubuntu.
What's missing ?
Update:
I followed the instructions of the main answer in this topic Cron job not working in Whenever gem
And it worked! The task is running even with the server not being started (with "rails s" command).
please run crontab -l to see if you have updated the crontab successfully

Cron job using "whenever" ruby gem is not working

On my local I am generating sitemaps with the help of sitemap_generator gem. I have set up everything and now when I run rake sitemap:generate it is generating sitemap. Now I am trying to schedule this process using whenever gem. My schedule.rb is as follows:
schedule.rb
every 1.minutes do
rake "sitemap:refresh"
end
I keep my rails server running but nothing seems to happen after 1 minute. I might be missing something here. Before I push it on production server I wanted to check it. Can someone please tell me what is the issue here?
Can you please run crontab -l and check if cronjob is scheduled.
You need to convert your schedule.rb to cron syntax.
Try
whenever --update-crontab
And then check crontab.
crontab -l
Whenever command

Ruby on Rails, Capistrano, Whenever: Cron jobs not getting executed at production server

Ruby on rails + Capistrano + Whenever gem
I executed whenever --update-crontab but still cron job is not getting executed at production server. There are no logs in the log file. Though everything works well at development where capistrano is not required.
schedule.rb
set :output, "../dev/log/cron.log"
every 1.minute do
runner "SOME_TASK"
end
deploy.rb
set :whenever_identifier, ->{ "#{fetch(:application)}_#{fetch(:stage)}" }
capfile
require "whenever/capistrano"
What's the issue? How to debug?
I've had similar issues when deploying our apps.
If you check the log files of crontab you'll see that it does execute but its being executed in the wrong context.
For example:
You think this code should execute but it doesn't:
every 1.minute do
runner "bundle exec rake db:seed"
end
Instead you should supply the absolute path tot the executable. Cron doesn't know in what kind of context it should be run, it just executes.
We use rbenv in our deployment and we use shims of gems. So I just supplied cron with the absolute path to the executable.
This code does run:
every 1.minute do
runner "/usr/bin/shims/bundle exec rake db:seed"
end

Ruby on Rails Delayed Job Local Wont Run

I'm working with delayed job for active record gem https://github.com/collectiveidea/delayed_job I'm trying to set up a job to run five minutes after an event occurs in my application. After five minutes passes, I need to make some database updates. I've tried rake jobs:work and RAILS_ENV=development script/delayed_job start. Prior to this all, I have run bundle install, rails generate delayed_job:active_record, and rake db:migrate. I have a lottery website that needs to check winners every five minutes and update tokens for winning players.
I wait five minutes, but no updates are made in my local application.
Here's what I have so far:
Gem File:
gem 'delayed_job_active_record'
gem "daemons"
Job (located in lib)
class WinnersJob < Struct.new(:blast_id)
def perform
...
end
Controller
require 'winners'
Delayed::Job.enqueue(WinnersJob.new(blast.id), 1, 5.minutes.from_now)
end
I think you have to launch the background workers locally using foreman. The following worked on my Mac.
From Heroku docs:
You then need to tell your application to process jobs put into your job queue, you can do that by adding this to your Procfile:
worker: bundle exec rake jobs:work
Now when you start your application using Foreman it will start processing your job queue.
foreman start
Having said all that, unless you are deploying on a Mac, it doesn't really matter if they run locally. (I noticed this after I got it working.) It only matters if it works on your servers. If you are deploying on Heroku, then Delayed Job works well.
Reference:
https://devcenter.heroku.com/articles/delayed-job
https://devcenter.heroku.com/articles/procfile

How should I run delayed_job in production environment?

I have no problems with running it in development mode via rake jobs:work. However, I'm somehow unable to figure out how to use it in production. I'm using Capistrano for deployment.
Thanks for any advice!
If you install delayed_job as a gem you need to run the generator in order to create the script scripts/delayed_job and set run permissions.
Then you can follow the instructions on How to configure Capistrano for Delayed Job to hook it up in your Capistrano file.
See this answer. In a nutshell, use the Collective Idea fork of delayed_job. It contains a script called delayed_job that can be used.
You can run the generated delayed_job script as follows:
RAILS_ENV=production script/delayed_job start
Hope this helps
My first thought will be to add a after deploy task in capistrano to run the rake jobs:work task. you might need to check if the process is already running and restart it.
If you are running it via rake then couldn't you just run however often you wanted via cron? The whenever gem is a great interface to this from ruby.

Resources