0

I have a Rails application which uses Resque for a job queue. The Resque worker class is "JobDo" and is in app/workers/job_do.rb

I have a script (jobdo.sh) which will put a job onto the queue:

jruby -e 'load "/home/abc/myapp/app/workers/job_do.rb"; Resque.enqueue(JobDo, Time.now.utc)' >> /home/abc/logs/resque.log 2>&1

However...I would like to be able to run this in a rails environment which doesn't use the same log file, user, or rails application root. I see doing something like this:

export $USER_HOME=/home/abc
export $LOGFILE=$USER_HOME/logs/resque.log
export $SERVER_HOME=/home/abc/myapp

jruby -e 'load "$SERVER_HOME/app/workers/job_do.rb"; Resque.enqueue(JobDo,Time.now.utc)' >> $LOGFILE 2>&1

This would let any user edit $USER_HOME and $SERVER_HOME and run the code in their environment.

However, this doesn't work because I cannot figure out how to twiddle the single and double quotes to use the environment variables. How do I do this?

Jay Godse
  • 15,163
  • 16
  • 84
  • 131

2 Answers2

1

There are several problems here. First, when assigning to variables, don't use a $. Second, you don't need to export the variables since they're being used in the shell, not passed (as environment variables) to the programs it runs. Third, the shell doesn't replace variables inside single-quotes, so you need to switch to double-quotes (and then escape the double-quotes you want passed to jruby). Finally, I'd wrap $USER_HOME and $LOGFILE in double-quotes, just in case it contains spaces or any other funny characters. Here's what I get:

USER_HOME=/home/abc
LOGFILE="$USER_HOME/logs/resque.log"
SERVER_HOME=/home/abc/myapp

jruby -e "load \"$SERVER_HOME/app/workers/job_do.rb\"; Resque.enqueue(JobDo,Time.now.utc)" >> "$LOGFILE" 2>&1
Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
0

I think that you can replace the single quotes with double quotes, and remove the inner set of quotes:

export USER_HOME=/home/abc
export LOGFILE=$USER_HOME/logs/resque.log
export SERVER_HOME=/home/abc/myapp

jruby -e "load $SERVER_HOME/app/workers/job_do.rb; Resque.enqueue(JobDo,Time.now.utc)" >> $LOGFILE 2>&1

This should work as long as none of the variables contain spaces.

Barton Chittenden
  • 4,238
  • 2
  • 27
  • 46