How to start clock task using passenger & nginx on production - ruby-on-rails

I deploy my project using passenger & nginx & Amazon EC2.
How can i run clock task on server. I used Clockwork gem.
This is my clock task (lib/clock.rb) :
# require boot & environment for a Rails app
require_relative "../config/boot"
require_relative "../config/environment"
require 'clockwork'
module Clockwork
handler do |job|
puts "Running #{job}"
end
every 10.minutes, 'send detail' do
::User.periodically_send_detail
end
end
Locally i can run bundle exec clockwork lib/clock.rb and it's working. It send detail on every 10 seconds.
On heroku i add bundle exec clockwork lib/clock.rb to Procfile like (NOTE: extra charge will be applied for clock) :
clock: bundle exec clockwork lib/clock.rb
Works on heroku also.
But how can i run this clock file with passenger and nginx and Amazon EC2 server on production startup ?
Any extra information required then tell me i will provide.

I access server via SSH.
I change gem to whenever gem and it doing job well.
Add to gem file :
gem 'whenever', :require => false
Then run wheneverize .
Then add task to config/schedule.rb fiile :
every 10.minutes do
runner "User.periodically_send_student_detail", :output => 'cron.log'
end
Then update your crontab in ubuntu server using this command :
$ whenever --update-crontab --set environment='production'
and that's it. It's done.
You can check cronjob using this command crontab -e OR crontab -l

Related

Clockwork gem is trying to access development db in production

I am trying to start the clockwork with following command sudo bundle exec clockwork config/clock.rb in production. But it throws the following error
ActiveRecord::AdapterNotSpecified: 'development' database is not configured. Available: ["production"].
It works correctly in local. We have Puma server and JRuby setup in server.
Add the RAILS_ENV variable:
RAILS_ENV=production sudo bundle exec clockwork config/clock.rb
Or, set the RAILS_ENV variable in your .bashrc:
echo 'export RAILS_ENV=production' >> ~/.bashrc
And then exit the shell, and login again. From then on, clockwork (and all other rails related things) should be in production mode. You can use the command you were using originally.

Sidekiq with Capistrano (2.x) - is needed to set up restarting Sidekiq after every delpoyment?

I am playing with Sidekiq and using Capistrano for deploying the application.
So far I am using the gem 'capistrano-sidekiq' , group: :development gem and as I was previously using DelaydedJob, I needed to remove from my current deploy file these lines:
set :delayed_job_command, "bin/delayed_job"
after "deploy:start", "delayed_job:start"
after "deploy:stop", "delayed_job:stop"
What do I need to add to Capistrano for Sidekiq to make sure Sidekiq will run all the time (means it will not be interrupted/stopped after I deploy some code to server?
Or does the gem automatically (re)starts Sidekiq after every deployment that has been made?
Sidekiq is restarted automatically in Capistrano 2.x after deployment.
The capistrano-sidekiq gem which manages it says:
if fetch(:sidekiq_default_hooks)
before 'deploy:update_code', 'sidekiq:quiet'
after 'deploy:stop', 'sidekiq:stop'
after 'deploy:start', 'sidekiq:start'
before 'deploy:restart', 'sidekiq:restart'
end
On lines 26-31 of the gem file lib/capistrano/tasks/capistrano2.rb.
:sidekiq_default_hooks is set in the block
Capistrano::Configuration.instance.load do
_cset(:sidekiq_default_hooks) { true }
Above it so it is a Capistrano instance variable loaded by the configuration for this particular environment.
The sidekiq:quiet command runs
run_as "if [ -d #{current_path} ] && [ -f #{pid_file} ] && kill -0 `cat #{pid_file}`> /dev/null 2>&1; then cd #{current_path} && #{fetch(:sidekiqctl_cmd)} quiet #{pid_file} ; else echo 'Sidekiq is not running'; fi"
And I'm assuming by using the default Capistrano 2 deploy configuration your deployment process would look like:
So - based on that you can confirm that it will restart every time. You can manually alter your deployment process using Capistrano 2 if you so choose to do so or you can also alter the :sidekiq_default_hooks variable and define your own Sidekiq process too that way.

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

How can I use clockwork to input data every hour?

My environment is Macbook OSX 10.7 Lion with Ruby 2.0 and Rails 4.
I am new to RoR. I built a website with one column database using a scaffold.
It's just a little website about weather.
I want to input degrees to database every hour.
And I found the gem clockwork, but I have no idea how to use it with my project.
I wrote clock.rb and put it in my project file and ran rails s but nothing happened.
Here is myproject/clock.rb
myproject/clock.rb
require 'clockwork'
module Clockwork
handler do |job|
puts "Running #{job}"
every(1 hours, ''){
Mydata.create(:degree => input_data)
}
end
What should I do with it or where should I put the file?
They say that I need to use $ clockwork clock.rb, but when I run rails s, there's no way to use that...
Thanks a lot.
As stated in the official docs you need a couple of things.
setup the clock file, I've set it in app/clock.rb
require 'clockwork'
module Clockwork
handler do |job|
case job
when 'weather.input_degree'
Mydata.create degree: input_data
# when 'some_other_task'
# ...
else
puts "Couldn't find your job!"
end
end
every(1.hour, 'weather.input_degree') # the string indicates an arbitrary job name
# every(20.seconds, 'weather.some_other_task')
end
The starting process. The key is to use something like Foreman
$ gem install foreman
Create a file named Procfile in root of your app:
web: bundle exec rails -s
clock: bundle exec clockwork app/clock.rb
# any_other_service_you_want: bash script here
start your server
$ foreman start
Add to Gemfile ­
gem 'clockwork'
after adding upper listed gem in gemfile, use bundle install to add this gem into project.
Example ­
Create a file clock.rb under /lib directory
clock.rb
require File.expand_path('../../config/boot', __FILE__)
require File.expand_path('../../config/environment', __FILE__)
require 'clockwork'
include Clockwork
module Clockwork
## Here Student is a domain class, having a method insertRecord to
## insert a record in DB
every(20.seconds, 'job ­ Inserting Record in DB') { Student.insertRecord }
end
Command to Run the clockword
bundle exec clockwork lib/clock.rb
This code needs stay outside of handler block:
every(1.hour, ''){ Mydata.create(:degree => input_data) }
You can execute the clock like that:
bundle exec clockwork clock.rb
You can make use of Cron. Write a rake task and call the rake task from the Cron for repeating processes.
Example (Add this to cron to run the task every hour):
0 * * * * cd /home/projectdir;rake do:task

Whenever CRON tab doesn't work on Amazon EC2

I am trying to set up CRON jobs on Amazon EC2 with using the Whenever gem.
In the schedule.rb is following:
set :output, "/home/my_deploy_name/my_deploy_name/current/log/cron_log.log"
every 2.minutes do
puts "It's working !!!"
end
and in the deploy.rb this:
...
set :whenever_command, "bundle exec whenever"
require "whenever/capistrano"
after 'deploy:create_symlink', 'whenever:update_crontab'
after 'deploy:rollback', 'whenever:update_crontab'
When I deploy this code to EC2 and check crontab -l, the output is:
no crontab for ubuntu
When I run crontab -e, the file is not edited.
What is wrong here? What the CRON job doesn't run on EC2 every 2 minutes?
Some options to try:
1) Log into the server and run:
bundle exec whenever --update-crontab
and check your crontab.
2) You don't need to set the before and after callbacks. The capistrano recipe does that for you:
https://github.com/javan/whenever/blob/master/lib/whenever/capistrano.rb
Capistrano::Configuration.instance(:must_exist).load do
# Write the new cron jobs near the end.
before "deploy:finalize_update", "whenever:update_crontab"
# If anything goes wrong, undo.
after "deploy:rollback", "whenever:update_crontab"
end

Resources