Class: Navvy::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/navvy/worker.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.sleep_timeInteger

Sleep time of the worker.

Returns:

  • (Integer)

    sleep



15
16
17
# File 'lib/navvy/worker.rb', line 15

def self.sleep_time
  @sleep_time || Navvy.configuration.sleep_time
end

Class Method Details

.daemonize(*args) ⇒ Object

Daemonize the worker



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/navvy/worker.rb', line 56

def self.daemonize(*args)
  if defined?(ActiveRecord)
    # Sets ActiveRecord's logger to Navvy a new Logger instance
    ActiveRecord::Base.logger = Logger.new(STDOUT)
  end
  
  # If #daemonize does not receive any arguments, the options variable will
  # contain an empty hash, and the ARGV of the environment will be used instead
  # of the :ARGV options from Daemons#run_proc. However, if the *args param has been set
  # this will be used instead of the environment's ARGV for the Daemons.
  options = args.empty? ? {} : {:ARGV => args}
  
  # Finally, the directory store mode will be set to normal and the Daemons PID file
  # will be stored inside tmp/pids of the application.
  options.merge!({:dir_mode => :normal, :dir => 'tmp/pids'})
  
  # Ensures that the tmp/pids folder exists so that the process id file can properly be stored
  %x(mkdir -p tmp/pids)
  
  # Runs the Navvy Worker inside a Daemon
  Daemons.run_proc('navvy', options) do
    Navvy::Worker.start
  end
end

.fetch_and_run_jobsObject

Fetch jobs and run them.



42
43
44
45
46
47
48
49
50
51
# File 'lib/navvy/worker.rb', line 42

def self.fetch_and_run_jobs
  Job.next.each do |job|
    result = job.run
    Navvy::Log.info(
      "* #{job.object.to_s}.#{job.method_name}" <<
      "(#{job.args.join(', ')}) => #{(job.exception || result).to_s}",
      job.failed? ? 31 : 32
    )
  end
end

.startObject

Start the worker.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/navvy/worker.rb', line 22

def self.start
  Navvy::Log.info '*** Starting ***'
  trap('TERM') { Navvy::Log.info '*** Exiting ***'; $exit = true }
  trap('INT')  { Navvy::Log.info '*** Exiting ***'; $exit = true }

  loop do
    fetch_and_run_jobs

    if $exit
      Navvy::Log.info '*** Cleaning up ***'
      Navvy::Job.cleanup
      break
    end
    sleep sleep_time
  end
end