bin/swf in simpler_workflow-0.1.12 vs bin/swf in simpler_workflow-0.2.0

- old
+ new

@@ -1,50 +1,45 @@ #!/usr/bin/env ruby # encoding: utf-8 +# Mostly based on @ahoward's https://gist.github.com/3098500 with a few fixes and +# adjustments. # setup # require 'fileutils' + pwd = ENV['PWD'] script = File.expand_path(__FILE__).gsub(%r|\breleases/\d+\b|, 'current') - script_dir = File.dirname(script) - rails_root = File.dirname(script_dir) + rails_root = pwd.gsub(%r|\breleases/\d+\b|, 'current') Dir.chdir(rails_root) - require File.join(rails_root, 'config', 'env.rb') + FileUtils.mkdir_p(File.join(rails_root, 'log', 'workflow')) + FileUtils.touch(File.join(rails_root, 'log', 'workflow', 'pid')) - FileUtils.mkdir_p(File.join(rails_root, 'log', 'jobs')) - FileUtils.touch(File.join(rails_root, 'log', 'jobs', 'pid')) - - %w( - BACKGROUND PIDFILE QUEUE VVERBOSE INTERVAL COUNT - ).each{|key| ENV.delete(key)} - quiet = ARGV.delete('-q') || ARGV.delete('--quiet') mode = ARGV.shift || 'run' - pidfile = './log/jobs/pid' + pidfile = './log/workflow/pid' + lockfile = File.open('./log/workflow/lock', "w") + pid = Integer(IO.read(pidfile).strip) rescue nil running = begin; Process.kill(0, pid); true; rescue Object; false; end # go # case mode when 'pid' puts(pid) if running when 'run' - exit(42) unless DATA.flock(File::LOCK_EX | File::LOCK_NB) + exit(42) unless lockfile.flock(File::LOCK_EX | File::LOCK_NB) - ENV['PIDFILE'] = pidfile - ENV['QUEUE'] = 'jobs' - ENV['VVERBOSE'] = '1' - exec "rake environment resque:work" + exec "rake simpler_workflow:work" when 'start' - exit(42) unless DATA.flock(File::LOCK_EX | File::LOCK_NB) + exit(42) unless lockfile.flock(File::LOCK_EX | File::LOCK_NB) unless running FileUtils.rm_f(pidfile) pid = nil @@ -52,10 +47,11 @@ if fork b.close pid = Integer(a.read.strip) a.close puts(pid) unless quiet + File.open(pidfile, "w") { |f| f.write(pid) } exit end exit!(0) if fork a.close b.puts(Process.pid) @@ -64,33 +60,28 @@ { 'stdin' => STDIN, 'stdout' => STDOUT, 'stderr' => STDERR, }.each do |basename, io| - path = File.join("log/jobs/#{ basename }") + path = File.join("log/workflow/#{ basename }") begin open(path, 'a+'){|fd| io.reopen(fd)} rescue open(path, 'w+'){|fd| io.reopen(fd)} end end Process.setsid rescue nil File.umask(0) rescue nil -# This is where Resque is started... -# Looks like a good place to load and run a task that launches a workflow... - ENV['PIDFILE'] = pidfile - ENV['QUEUE'] = 'jobs' - ENV['VVERBOSE'] = '1' if ENV['RAILS_ENV'] != 'production' - exec "rake environment resque:work" + exec "rake simpler_workflow:work" end when 'stop' if running begin - Process.kill('-QUIT', pid) + Process.kill('QUIT', pid) rescue Errno::ESRCH nil end end @@ -133,12 +124,13 @@ exit! if fork system "#{ script } shutdown -q >/dev/null 2>&1" exec "#{ script } start -q >/dev/null 2>&1" when 'tail' - exec "tail -F ./log/jobs/*" + exec "tail -F ./log/workflow/*" when 'pstree' exec "pstree #{ pid }" if running end - + __END__ +