Gem 'whenever' in rails 4 - ruby-on-rails-4

I try to send message automatically by using whenever gem. I am in initial step. I install the gem 'whenever'. I done the following step.
1. Add "gem 'whenever', :require => false" to the gemfile.
2. bundle install.
3. wheneverize .
4. in schedule.rb add the following code,
set :output, "#{path}/log/cron.log"
#every 1.day, :at => '4:30 am' do
every 5.minutes do
runner "Payment.sendMessage", :environment => "development"
end
5.And model likes,
class Payment < ActiveRecord::Base
def sendMessage
puts"Hello"
end
end
6. When I use bundle exec whenever, I get like the following issue as
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c
'cd /home/prabha/rails_job && bundle exec bin/
rails runner -e development '\''Payment.sendMessage'\'' >>
/home/prabha/rails_job/log/cron.log 2>&1'
## [message] Above is your schedule file converted to cron syntax;
your crontab file was not updated.
## [message] Run `whenever --help' for more options.
I am stuck with this step. what I want to do the further proceed? Anyone guide me.
Thanks.

You need to update you crontab file.
Do the following -
whenever --update-crontab
For more information, please check whenever gem's Github ReadMe page.

1) sendMessage should be a class method.
2) You can use "whenever" command in your project directory to see the cron configuration and then copy into your crontab.

Related

Cron job openshift error

I have a rails 4 openshift application. I am trying to run a cron job. The script runs completely fine when I run it by itself. The script is:
#!/bin/bash
/bin/bash -l -c 'cd $OPENSHIFT_REPO_DIR && bundle exec bin/rails runner -e production "Payment.charge_customers_pay_experts"'
The problem is the log file gives me the following error
Wed Feb 3 22:57:05 EST 2016: START minutely cron run
__________________________________________________________________________
/var/lib/openshift/56a438107628e18b30000111/app-root/runtime/repo//.openshift/åcron/minutely/charge_customers_pay_experts:
Warning: You're using Rubygems 2.0.14 with Spring. Upgrade to at least Rubygems 2.1.0 and run `gem pristine --all` for better startup performance.
/var/lib/openshift/56a438107628e18b30000111/app-root/runtime/repo/vendor/bundle/ruby/gems/spring-1.6.2/lib/spring/sid.rb:39:in `getpgid': Permission denied (Errno::EACCES)
from /var/lib/openshift/56a438107628e18b30000111/app-root/runtime/repo/vendor/bundle/ruby/gems/spring-1.6.2/lib/spring/sid.rb:39:in `pgid'
from /var/lib/openshift/56a438107628e18b30000111/app-root/runtime/repo/vendor/bundle/ruby/gems/spring-1.6.2/lib/spring/server.rb:78:in `set_pgid'
from /var/lib/openshift/56a438107628e18b30000111/app-root/runtime/repo/vendor/bundle/ruby/gems/spring-1.6.2/lib/spring/server.rb:34:in `boot'
from /var/lib/openshift/56a438107628e18b30000111/app-root/runtime/repo/vendor/bundle/ruby/gems/spring-1.6.2/lib/spring/server.rb:14:in `boot'
from -e:1:in `<main>'
__________________________________________________________________________
Wed Feb 3 22:57:06 EST 2016: END minutely cron run - status=0
__________________________________________________________________________
I have made sure the script was executable. I'm not sure if I am missing something. Does anyone have any thoughts?
I don't know that the script being executable necessarily has anything to do with this. It looks like a permissions error more than anything. Does the system user that runs the cron job have the correct permissions to run? You can test this by logging into that user (or sudo su - <user>) and then execute the command in the script manually.
/bin/bash -l -c 'cd $OPENSHIFT_REPO_DIR && bundle exec bin/rails runner -e production "Payment.charge_customers_pay_experts"'
Be sure to replace your $OPENSHIFT_REPO_DIR variable with the correct path to your OpenShift repo directory.
You may just need to either add the user your cronjob runs as to the group that has permissions over the files, or perhaps run the cronjob as a more privileged user (privileged in that it has permissions over the required files).
BTW, I could only post this as an answer as Stack Overflow is telling me I need 50 reputation points to comment.
I fixed this by commenting out the 'spring' gem in my gemfile. But apparently this is a known issue. https://bugzilla.redhat.com/show_bug.cgi?id=1305544.
There is a workaround for the time being until this issue is resolved. You can edit the /usr/libexec/openshift/cartridges/cron/bin/cron_runjobs.sh to add setsid in front of timeout so that it runs setsid timeout ... as this allows for the timeout command to actually change the sid.

Cron Job issu with rails - gem whenever

I am trying to do a simple Cron task using the gem whenever for rails.
How can I tell whenever to trigger a controller action ?
What I wan to do :
every 1.minute do
runner "Mycontroller.index", :environment => 'development'
end
What i want to do is to trigger the action index in my DataController every minute. The index action trigger a mailer.
I run : whenever --update-crontab football
also when I start/restart my server I get a tinny message as follow:
You have new mail in /var/mail/Antoine
[2015-04-12 18:38:17] INFO WEBrick 1.3.1 [2015-04-12 18:38:17] INFO
ruby 2.1.3 (2014-09-19) [x86_64-darwin14.0] [2015-04-12 18:38:17] INFO
WEBrick::HTTPServer#start: pid=22476 port=3000 ^C[2015-04-12 18:38:52]
INFO going to shutdown ... [2015-04-12 18:38:52] INFO
WEBrick::HTTPServer#start done. Exiting
You have new mail in
/var/mail/Antoine
Okay I figured it out:
every 1.day, :at => '4:30 am' do
command "curl http://localhost:3000", :environment => 'development'
end
I use the command curl to got to the route where I wish to trigger a controller action.
I also understood that I need to run whenever -w to write the cron task and then I can do crontab -l to see my ongoing cron tasks and if I wan to remove cron taks I just need to run crontab -r

Execute system command with Rake outside Bundle scope

Let say I have this Rake task:
namespace :db do
namespace :dump do.
desc 'Backup database dump to s3'
task :backup => :environment do
cmd = ['backup', 'perform', '-t project_backup', "-c #{Rails.root.join 'lib', 'backup', 'config.rb'}"]
system(*cmd) # ...I've tried `` & exec() sa well, same thing
end
end
end
Backup gem is stand alone ruby gem application which dependencies needs to be isolated from application bundler. In other words it cannot be part of Gemfile. This gem is simply installed over gem install backup
When I run backup command over bash console, it successfully run:
$ backup perform -t validations_backup -c /home/equivalent/my_project/lib/backup/config.rb
When I execute rake db:dump:backup I will get
backup is not part of the bundle. Add it to Gemfile. (Gem::LoadError)
...which is the same thing when I run backup command with bundle exec from bash
$ bundle exec backup perform -t validations_backup -c /home/equivalent/my_project/lib/backup/config.rb
...meaning that the backup command is executed over bundler when run as part of rake task.
my question: How can I run rake db:dump:backup outsite the bundle scope, meaning that backup command won`t be executed over bundler?
Thank you
I found a workaround for this problem here:
namespace :db do
namespace :dump do
desc 'Backup database dump to s3'
task :backup do
Bundler.with_clean_env do
sh "backup perform -t project_backup -c #{Rails.root.join 'lib', 'backup', 'config.rb'}"
end
end
end
end
The key here is to enclose the code that must not run under bundler's environment in a block like this:
Bundler.with_clean_env do
# Code that needs to run without the bundler environment loaded
end
Here is the Capistrano solution I was mentioning for those who need it while we figure out how to fix Rake.
class BackupDatabaseCmd
def self.cmd
# some logic to calculate :
'RAILS_ENV=production backup perform -t name_of_backup_task -c /home/deploy/apps/my_project/current/lib/backup/config.rb'
# in the configuration file I'm loading `config/database.yml`
# and passing them to backup gem configuration
end
end
namespace :backup do
namespace :database do
task :to_s3 do
on roles(:web) do
within release_path do
with rails_env: fetch(:rails_env) do
execute(BackupDatabaseCmd.cmd)
end
end
end
end
end
end
# cap production backup:database:to_s3

Why doesn't my custom recipes run on AWS OpsWorks?

I've created a GitHub repo for my simple custom recipe:
my-cookbook/
|- recipes/
|- appsetup.rb
I've added the repo to Custom Chef Recipes as https://github.com/my-github-user/my-github-repo.git
I've added my-cookbook::appsetup to the Setup "cycle".
I know it's executed, because it fails to load if I mess up the syntax.
This is my appsetup.rb:
node[:deploy].each do |app_name, deploy|
script "install_composer" do
interpreter "bash"
user "root"
cwd "#{deploy[:deploy_to]}/current"
code "curl -sS https://getcomposer.org/installer | php && php composer.phar install --no-dev"
end
end
When I log into the instance by SSH with the ubuntu user, composer isn't installed.
I've also tried the following to no avail (A nodejs install):
node[:deploy].each do |app_name, deploy|
execute "installing node" do
command "add-apt-repository --yes ppa:chris-lea/node.js && apt-get update && sudo apt-get install python-software-properties python g++ make nodejs"
end
end
Node doesn't get installed, and there are no errors in the log. The only references to the cookbook in the log just says:
[2014-03-31T13:26:04+00:00] INFO: OpsWorks Custom Run List: ["opsworks_initial_setup", "ssh_host_keys", "ssh_users", "mysql::client", "dependencies", "ebs", "opsworks_ganglia::client", "opsworks_stack_state_sync", "mod_php5_apache2", "my-cookbook::appsetup", "deploy::default", "deploy::php", "test_suite", "opsworks_cleanup"]
...
2014-03-31T13:26:04+00:00] INFO: New Run List expands to ["opsworks_initial_setup", "ssh_host_keys", "ssh_users", "mysql::client", "dependencies", "ebs", "opsworks_ganglia::client", "opsworks_stack_state_sync", "mod_php5_apache2", "my-cookbook::appsetup", "deploy::default", "deploy::php", "test_suite", "opsworks_cleanup"]
...
[2014-03-31T13:26:05+00:00] DEBUG: Loading Recipe my-cookbook::appsetup via include_recipe
[2014-03-31T13:26:05+00:00] DEBUG: Found recipe appsetup in cookbook my-cookbook
Am I missing some critical step somewhere? The recipe is clearly recognized and loaded, but doesn't seem to be executed.
(The following are fictitious names: my-github-user, my-github-repo, my-cookbook)
I know you've abandoned the cookbook but I'm almost 100% sure it's because you don't have a metadata.rb file in the root of your cookbook.
Your cookbook name should not contain a dash. I had the same problem, replacing by '_' solved it for me.
If those commands are failing silently, it could be that your use of && is obscuring a failure.
As for add-apt-repository, that is an interactive command. Try using the "--yes" option to answer yes by default, making it no longer interactive.
If you do not execute your command successfully, you will not find the files in the current directory. Check inside the last release folder to see if it had been put there.
It maybe prudent to check if you got the right directory etc setup by changing the CWD to : /tmp

In capistrano 3, is it possible to bundle & asset precompile locally and copy the results to application server?

At present, I have it setup so that capistrano git pulls the latest code on production servers, bundle installs and asset precompiles it individually on each web server.
The problem that I am running into is that occationally it will take a long time and take up a lot of resources that impacts the performance on the production servers.
I am looking for guidelines on how best to do this.
If anyone has experience with this and can share their opinions, I would really appreciate it.
I am looking to see if this is a good/bad idea and what are common pitfalls I should watch out for.
I would also appreciate any link to blog post/tutorial/documentation that could help with this.
Thanks for reading.
Ankit.
Here is my work around. Try adding it in namespace :deploy
namespace :assets do
desc 'Run the precompile task locally and rsync with shared'
task :precompile, :roles => :web, :except => { :no_release => true } do
unless skip_assets
%x{bundle exec rake assets:clean RAILS_ENV=#{rails_env}}
run_local "bundle exec rake assets:precompile RAILS_ENV=#{rails_env}"
servers = find_servers_for_task(current_task)
port_option = port ? "-e 'ssh -p #{port}'" : ''
servers.each do |server|
%x{rsync --recursive --times --rsh=ssh --compress --human-readable --progress #{port_option} public/assets #{user}##{server}:#{shared_path}}
end
%x{bundle exec rake assets:clean RAILS_ENV=#{rails_env}}
end
end
end
def run_local(cmd)
system cmd
if($?.exitstatus != 0) then
puts 'exit code: ' + $?.exitstatus.to_s
exit
end
end