I have here a script that has suddenly changed behavior and I can't work out why. As far as I know, no changes to the script have been made and neither have any changes to rubies or gems.
First of all, here is the script:
require 'rubygems'
require 'daemons'
require 'beanstalk-client'
require 'sequel'
require 'fileutils'
require 'chronic'
APP_DIR = File.dirname(File.expand_path(__FILE__))
ROOT_DIR = File.expand_path(File.join(APP_DIR,'..', '..'))
$LOAD_PATH.unshift(APP_DIR)
$LOAD_PATH.unshift(ROOT_DIR)
$LOAD_PATH.unshift(File.join(ROOT_DIR, 'lib'))
$LOAD_PATH.unshift(File.join(ROOT_DIR, 'db'))
$LOAD_PATH.unshift(File.join(ROOT_DIR, 'scripts'))
$LOAD_PATH.unshift(File.join(ROOT_DIR, 'models'))
$LOAD_PATH.unshift(File.join(ROOT_DIR, 'systems', 'shared', 'models'))
puts "Starting Daemon..."
Daemons.run_proc(File.join('var', 'background_jobs'), :log_output => true) do
require 'environment'
puts "renaming log file..."
FileUtils.mkdir_p('var')
begin
FileUtils.mv(File.join('var', 'background_jobs.output'), File.join('var', "#{Time.now.to_s.gsub(":","").gsub(" ", '_')}.log"))
rescue
# NOP
end
SequelOutput.prepare_database(MyDatabase::DB)
beanstalk = Beanstalk::Pool.new(["0.0.0.0:11300"])
loop do
beanstalk_job = beanstalk.reserve
begin
jh = beanstalk_job.ybody
puts "processing #{jh[:job_id]}"
job = MyDatabase::DB[:project__jobs].filter(:id => jh[:job_id]).first
script_name = MyDatabase::DB[:project__available_tasks].filter(:id => job[:script_id]).get(:name)
task = eval(camelize script_name).new(job)
puts "processing script: #{script_name} aka (#{camelize script_name}) with #{task.steps} steps"
task.process
puts "finished #{jh[:job_id]}"
rescue
# NOP
end
beanstalk_job.delete
end
end
The script is called 'run.rb' and has the path /var/www/main/projects/systems/background. This script is called on by two different bash scripts, located in the 'main' folder. The first, called 'background_restart.sh', calls the ruby interpreter and the fully qualified name as follows:
cd /var/www/main
/usr/local/rvm/rubies/ruby-1.8.7-p249/bin/ruby /var/www/main/projects/systems/background/run.rb stop
/usr/local/rvm/rubies/ruby-1.8.7-p249/bin/ruby /var/www/main/projects/systems/background/run.rb start
This now fails, outputting 'Terminated' only to the terminal. The other script, called 'deploy.sh', deploys the Webapps, and fails with the same output (its first and last commands are a stop and start respectively).
I have no explanation as to why this has suddenly started to fail.
Some other things to note:
- Calling the fully qualified path of the script in the terminal works if I am in the background directory.
- Changing the directory in the bash scripts to 'background' first appears to bandaid the problem, though it is somewhat unsatisfactory to me.
- A further problems presents itself when I deploy:
The deploy script basically moves the current production code to another directory and puts in its place the updated code. When it's restarted I would expect the 'run.rb' script to recreate the 'var' folder in 'background' (as per line 24), however the daemons gem errors out with the following:
Starting Daemon...
/usr/local/rvm/gems/ruby-1.8.7-p249/gems/daemons-1.1.0/lib/daemons/pidfile.rb:94:in `initialize': No such file or directory - /var/www/main/projects/systems/background/var/background_jobs.pid (Errno::ENOENT)
from /usr/local/rvm/gems/ruby-1.8.7-p249/gems/daemons-1.1.0/lib/daemons/pidfile.rb:94:in `open'
from /usr/local/rvm/gems/ruby-1.8.7-p249/gems/daemons-1.1.0/lib/daemons/pidfile.rb:94:in `pid='
from /usr/local/rvm/gems/ruby-1.8.7-p249/gems/daemons-1.1.0/lib/daemons/application.rb:254:in `start_proc'
from /usr/local/rvm/gems/ruby-1.8.7-p249/gems/daemons-1.1.0/lib/daemons/application.rb:294:in `start'
from /usr/local/rvm/gems/ruby-1.8.7-p249/gems/daemons-1.1.0/lib/daemons/controller.rb:70:in `run'
from /usr/local/rvm/gems/ruby-1.8.7-p249/gems/daemons-1.1.0/lib/daemons.rb:193
from /usr/local/rvm/gems/ruby-1.8.7-p249/gems/daemons-1.1.0/lib/daemons/cmdline.rb:112:in `call'
from /usr/local/rvm/gems/ruby-1.8.7-p249/gems/daemons-1.1.0/lib/daemons/cmdline.rb:112:in `catch_exceptions'
from /usr/local/rvm/gems/ruby-1.8.7-p249/gems/daemons-1.1.0/lib/daemons.rb:192:in `run_proc'
from run.rb:19
Now I can fix this simply by creating the 'var' folder in the terminal and running it again, so in case you're wondering why I'm bothering you folks, I'm sure some of you will appreciate that this is a problem that I don't want to simply hack. I want to work out what happened here, and try to get back to what I had before!