Sha256: 676646dbd7e5a8fbb91cfc61a33fdd15bb9c92eaf4a1781192040c1307e7a6bd

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

# This file based the file of the same name in the delayed_job gem by
# Tobias Luetke (Coypright (c) 2005) under the MIT License.

require 'benchmark'

module Updater

  #This class repeatedly searches the database for active jobs and runs them
  class Worker
    cattr_accessor :logger
    attr_accessor :pid
    attr_accessor :name
    
    def initialize(options={})
      @quiet = options[:quiet]
      @name = options[:name] || "host:#{Socket.gethostname} pid:#{Process.pid}" rescue "pid:#{Process.pid}"
      @pid = Process.pid
    end
    
    def start
      say "*** Starting job worker #{@name}"
      t = Thread.new do
        loop do
          delay = Update.work_off(self)
          break if $exit
          sleep delay
          break if $exit
        end
        clear_locks
      end
      
      trap('TERM') { terminate_with t }
      trap('INT')  { terminate_with t }
      
      trap('USR1') do
        say "Wakeup Signal Caught"
        t.run
      end
      
      sleep unless $exit
    end
    
    def say(text)
      puts text unless @quiet
      logger.info text if logger      
    end
    
    def clear_locks
      Update.all(:lock_name=>@name).update(:lock_name=>nil) 
    end
    
  private
  
    def terminate_with(t)
      say "Exiting..."
      $exit = true
      t.run
      say "Forcing Shutdown" unless status = t.join(15) #Nasty inline assignment
      clear_locks
      exit status ? 0 : 1
    end
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
updater-0.2.1 lib/updater/worker.rb