Class: Cron::Worker
- Inherits:
-
Object
- Object
- Cron::Worker
- Defined in:
- lib/app/jobs/cron/worker.rb
Overview
rubocop:disable ClassLength
Constant Summary collapse
- DEFAULT_LOG_LEVEL =
'info'.freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Worker
constructor
A new instance of Worker.
-
#name ⇒ Object
Every worker has a unique name which by default is the pid of the process.
- #say(text, level = default_log_level) ⇒ Object
-
#start ⇒ Object
rubocop:disable CyclomaticComplexity, PerceivedComplexity.
- #stop ⇒ Object
- #stop? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ Worker
Returns a new instance of Worker.
24 25 26 27 |
# File 'lib/app/jobs/cron/worker.rb', line 24 def initialize( = {}) super() @exit = [:exit_on_complete].presence end |
Class Method Details
.reload_app? ⇒ Boolean
20 21 22 |
# File 'lib/app/jobs/cron/worker.rb', line 20 def self.reload_app? defined?(ActionDispatch::Reloader) && Rails.application.config.cache_classes == false end |
.reset ⇒ Object
16 17 18 |
# File 'lib/app/jobs/cron/worker.rb', line 16 def self.reset self.default_log_level ||= DEFAULT_LOG_LEVEL end |
Instance Method Details
#name ⇒ Object
Every worker has a unique name which by default is the pid of the process. There are some advantages to overriding this with something which survives worker restarts: Workers can safely resume working on tasks which are locked by themselves. The worker will assume that it crashed before.
33 34 35 |
# File 'lib/app/jobs/cron/worker.rb', line 33 def name @name ||= "CronServer:#{Socket.gethostname} pid:#{Process.pid}" end |
#say(text, level = default_log_level) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/app/jobs/cron/worker.rb', line 61 def say(text, level = default_log_level) text = "[CronServer(#{name})] #{text}" return if logger.blank? logger.send(level, "#{Time.now.strftime('%FT%T%z')}: #{text}") end |
#start ⇒ Object
rubocop:disable CyclomaticComplexity, PerceivedComplexity
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/app/jobs/cron/worker.rb', line 37 def start # rubocop:disable CyclomaticComplexity, PerceivedComplexity trap('TERM') { stop } trap('INT') { stop } say 'Starting cron jobs server' self.class.lifecycle.run_callbacks(:execute, self) do loop do sleep Cron::Server.find_or_create_server.execute break if stop? end end say 'Exiting cron jobs server...' end |
#stop ⇒ Object
53 54 55 |
# File 'lib/app/jobs/cron/worker.rb', line 53 def stop @exit = true end |
#stop? ⇒ Boolean
57 58 59 |
# File 'lib/app/jobs/cron/worker.rb', line 57 def stop? !!@exit end |