froy001
By:
froy001

running rake tasks involving DB using cron returns wrong nil values

August 24, 2015 711 views
Configuration Management DigitalOcean System Tools Ubuntu

I have been searching for this for a couple of hours now:

I have a crontab file:

PATH=/usr/sbin:/usr/bin:/sbin:/bin
# Begin Whenever generated tasks for: bandmanage_backend_export_handler
* * * * * /bin/bash -l -c 'cd /home/froy001/apps/bandmanage_backend_export_handler/releases/20150824081934 && RAILS_ENV=production bundle exec rake bandmanage:request_user_message_export --silent >> log/cron_log.log 2>&1'

56 * * * * /bin/bash -l -c 'cd /home/froy001/apps/bandmanage_backend_export_handler/releases/20150824081934 && RAILS_ENV=production bundle exec rake bandmanage:get_requested_exports --silent >> log/cron_log.log 2>&1'

# End Whenever generated tasks for: bandmanage_backend_export_handler

and a rake task:


namespace :bandmanage do

  desc "request an export from SAMI"
  task request_user_message_export: :environment do
    puts "requesting message export"
    MessageExportService.new.message_export_request_hourly
    puts ".....DONE requesting message exports"
  end


  desc "request the exported result from SAMI a test for us to see"
  task request_user_message_export_test: :environment do
    puts "requesting message export"
    MessageExportService.new.message_export_request_test
    puts ".....DONE requesting message exports"
  end

  desc "request the exported result from SAMI"
  task get_requested_exports: :environment do
    puts "getting message export"
    MessageExportService.new.periodic_export_request_query
    puts ".....DONE getting message exports"
  end


end

When I run /bin/bash -l -c 'cd /home/froy001/apps/bandmanage_backend_export_handler/releases/20150824081934 && RAILS_ENV=production bundle exec rake bandmanage:request_user_message_export --silent >> log/cron_log.log 2>&1' from the command line It runs great.

When it is run by cron I get an error saying that a call to my DB requesting all registered users returns nil. And the job aborts.

I can't figure out why. Please help!!

1 Answer

Hello,

While we have alot of information here, there is some missing that makes it hard to advise. What sorta of DB are you using? What are the commands being run in the rake doing? What is trying to get exported?

Could it be that the cron is running as a different user than your froy001 user? This seems to be what I think it might be without a bit more information. Perhaps something is set in the froy001 user environment that doesn't exist in the cron's user environment.

If you can provide more detail on what exactly these crons are trying to do, and how they interact with the various services; more information might be available to provide.

Have another answer? Share your knowledge.