Class: Trainspotter::BackgroundWorker
- Inherits:
-
Struct
- Object
- Struct
- Trainspotter::BackgroundWorker
- Defined in:
- lib/trainspotter/background_worker.rb
Class Attribute Summary collapse
-
.instance ⇒ Object
Returns the value of attribute instance.
Instance Attribute Summary collapse
-
#interval ⇒ Object
Returns the value of attribute interval.
-
#lock_file ⇒ Object
Returns the value of attribute lock_file.
-
#lock_path ⇒ Object
Returns the value of attribute lock_path.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#task_block ⇒ Object
Returns the value of attribute task_block.
-
#timer_task ⇒ Object
Returns the value of attribute timer_task.
Class Method Summary collapse
Instance Method Summary collapse
Class Attribute Details
.instance ⇒ Object
Returns the value of attribute instance.
6 7 8 |
# File 'lib/trainspotter/background_worker.rb', line 6 def instance @instance end |
Instance Attribute Details
#interval ⇒ Object
Returns the value of attribute interval
4 5 6 |
# File 'lib/trainspotter/background_worker.rb', line 4 def interval @interval end |
#lock_file ⇒ Object
Returns the value of attribute lock_file
4 5 6 |
# File 'lib/trainspotter/background_worker.rb', line 4 def lock_file @lock_file end |
#lock_path ⇒ Object
Returns the value of attribute lock_path
4 5 6 |
# File 'lib/trainspotter/background_worker.rb', line 4 def lock_path @lock_path end |
#logger ⇒ Object
Returns the value of attribute logger
4 5 6 |
# File 'lib/trainspotter/background_worker.rb', line 4 def logger @logger end |
#task_block ⇒ Object
Returns the value of attribute task_block
4 5 6 |
# File 'lib/trainspotter/background_worker.rb', line 4 def task_block @task_block end |
#timer_task ⇒ Object
Returns the value of attribute timer_task
4 5 6 |
# File 'lib/trainspotter/background_worker.rb', line 4 def timer_task @timer_task end |
Class Method Details
.start(interval:, lock_path:, logger: nil, &task_block) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/trainspotter/background_worker.rb', line 8 def start(interval:, lock_path:, logger: nil, &task_block) raise ArgumentError, "a block is required" unless task_block return if instance&.running? self.instance = new(interval:, lock_path:, logger:, task_block:) instance.start end |
.stop ⇒ Object
16 17 18 19 |
# File 'lib/trainspotter/background_worker.rb', line 16 def stop instance&.stop self.instance = nil end |
Instance Method Details
#running? ⇒ Boolean
52 53 54 |
# File 'lib/trainspotter/background_worker.rb', line 52 def running? timer_task&.running? end |
#start ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/trainspotter/background_worker.rb', line 22 def start return if running? return unless acquire_lock self.timer_task = Concurrent::TimerTask.new( execution_interval: interval, run_now: true, &task_block ) timer_task.add_observer do |_time, _result, error| if error logger&.error "[Trainspotter] Background worker error: #{error.message}" logger&.error error.backtrace.first(10).join("\n") end end timer_task.execute logger&.info "[Trainspotter] Background worker started (pid=#{Process.pid})" end |
#stop ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/trainspotter/background_worker.rb', line 44 def stop timer_task&.shutdown self.timer_task = nil release_lock logger&.info "[Trainspotter] Background worker stopped" end |