0

I am using whenever gem. I wanted to set my env variables prior to the scheduled task. The 2nd line in the code below was the command I wanted to run in my schedule.rb file. But looks like I can't get it done so I commented it out and tried in different ways. Still nothing seems to work. Printing env inside the ruby code seems to show the needed variable correctly, but after running 'bundle exec whenever' and checking in the terminal with env command, the variables do not seem to be there. What is wrong here ?

every :hour do
  # Run shell script to assign variables and continue the rake task
  #system "for line in `cat config/myEnvFile.env` ; do export $line ; done"
  f = File.open("config/myEnvFile.env", "r")
  f.each_line do |line|
    j = line.split(" ")
    arr = j.first.split("=")
    system "export #{arr[0]}=#{arr[1]}"
    system "env"
    # This line below is also another way but still nothing works
    ENV[arr[0]] = arr[1]
  end
  rake "task:continue_doing_my_task"
end
  • this is not the way to do this, there is no reason to load the same environment variables every hour. Instead, boot those variables when your app starts in it's environment via something like https://github.com/bkeepers/dotenv – Anthony Feb 15 '16 at 11:11
  • every 1 hour is for testing, when deployed it is every 1 day, but that's not the issue here. – underScorePassionFruit Feb 15 '16 at 11:32

1 Answers1

0

The code in your every-block is executed when whenever --update-crontab runs, not every hour.

You need to make the command generated by rake "task:continue_doing_my_task" load these variables, either exporting them in-line using bash syntax (which gets ugly) or using dotenv as suggested in the comment by @Anthony.

See my answer to your follow-up question, which explains this in more detail.

Community
  • 1
  • 1
Isaac Betesh
  • 2,935
  • 28
  • 37