Sha256: a2339ce462702207cfe91da5050607054b01f2e3650811248efa9d280cf9c431

Contents?: true

Size: 1.27 KB

Versions: 4

Compression:

Stored size: 1.27 KB

Contents

class WorkerCmd
  OK_MESSAGE = {'start' => 'started', 'stop' => 'stopped'}
  def initialize(name, script)
    @name   = name
    @script = script
  end

  def execute(cmd)
    res = `cd #{RAILS_ROOT} && #{@script} RAILS_ENV=#{RAILS_ENV} #{cmd}`
    if $? == 0
      puts "#{@name}: #{OK_MESSAGE[cmd]} (pid #{pid})"
    else
      puts "#{@name}: failed to #{cmd}\n#{res}"
    end
    @pid = nil
  end

  def start
    if running?
      puts "#{@name}: already started (pid #{pid})"
    else
      execute('start')
    end
  end

  def stop
    if !running?
      puts "#{@name}: already stopped"
    else
      execute('stop')
    end
  end

  def pid_file
    @pid_file ||= File.expand_path(File.join(RAILS_ROOT, 'log', "#{@name}.pid"))
  end

  def pid
    @pid ||= File.exists?(pid_file) ? File.read(pid_file)[/\d+/] : nil
  end

  def running?
    pid && Process.kill(0, pid.to_i) rescue false
  end
end


namespace :worker do
  worker = WorkerCmd.new('worker', File.expand_path(File.join(File.dirname(__FILE__), 'worker')))

  desc "Start the delayed jobs worker"
  task :start do
    worker.start
  end

  desc "Stop the delayed jobs worker"
  task :stop do
    worker.stop
  end

  desc "Restart the delayed jobs worker"
  task :restart do
    worker.stop
    sleep(1)
    worker.start
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
zena-0.16.4 bricks/worker/misc/tasks.rb
zena-0.16.3 bricks/worker/misc/tasks.rb
zena-0.16.2 bricks/worker/misc/tasks.rb
zena-0.16.1 bricks/worker/misc/tasks.rb