Class: Trainspotter::BackgroundWorker

Inherits:
Struct
  • Object
show all
Defined in:
lib/trainspotter/background_worker.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.instanceObject

Returns the value of attribute instance.



6
7
8
# File 'lib/trainspotter/background_worker.rb', line 6

def instance
  @instance
end

Instance Attribute Details

#intervalObject

Returns the value of attribute interval

Returns:

  • (Object)

    the current value of interval



4
5
6
# File 'lib/trainspotter/background_worker.rb', line 4

def interval
  @interval
end

#lock_fileObject

Returns the value of attribute lock_file

Returns:

  • (Object)

    the current value of lock_file



4
5
6
# File 'lib/trainspotter/background_worker.rb', line 4

def lock_file
  @lock_file
end

#lock_pathObject

Returns the value of attribute lock_path

Returns:

  • (Object)

    the current value of lock_path



4
5
6
# File 'lib/trainspotter/background_worker.rb', line 4

def lock_path
  @lock_path
end

#loggerObject

Returns the value of attribute logger

Returns:

  • (Object)

    the current value of logger



4
5
6
# File 'lib/trainspotter/background_worker.rb', line 4

def logger
  @logger
end

#task_blockObject

Returns the value of attribute task_block

Returns:

  • (Object)

    the current value of task_block



4
5
6
# File 'lib/trainspotter/background_worker.rb', line 4

def task_block
  @task_block
end

#timer_taskObject

Returns the value of attribute timer_task

Returns:

  • (Object)

    the current value of 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

Raises:

  • (ArgumentError)


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

.stopObject



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

Returns:

  • (Boolean)


52
53
54
# File 'lib/trainspotter/background_worker.rb', line 52

def running?
  timer_task&.running?
end

#startObject



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

#stopObject



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