Class: Schked::Worker

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

Constant Summary collapse

DEFAULT_CLEANUP_INTERVAL =
"60s"
DEFAULT_CLEANUP_RETENTION =

seconds before cutoff

24 * 60 * 60

Instance Method Summary collapse

Constructor Details

#initialize(config:) ⇒ Worker

Returns a new instance of Worker.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/schked/worker.rb', line 10

def initialize(config:)
  @config = config
  @liveness_probe = nil

  config.validate!
  @job_run_store = build_job_run_store
  @locker = build_locker

  scheduler_opts = {trigger_lock: locker}.compact
  @scheduler = Rufus::Scheduler.new(**scheduler_opts)

  watch_signals
  Callbacks.new(config: config, job_run_store: @job_run_store).install(@scheduler)
  define_extend_lock if locker
  define_cleanup_job if database_backed_store?
  load_schedule
  start_liveness_probe
end

Instance Method Details

#job(as) ⇒ Object



29
30
31
# File 'lib/schked/worker.rb', line 29

def job(as)
  scheduler.jobs.find { |job| job.opts[:as] == as }
end

#pauseObject



33
34
35
# File 'lib/schked/worker.rb', line 33

def pause
  scheduler.pause
end

#scheduleObject



46
47
48
49
50
51
52
53
# File 'lib/schked/worker.rb', line 46

def schedule
  config
    .paths
    .map { |path| File.expand_path(path) }
    .uniq
    .map { |path| File.read(path) }
    .join("\n")
end

#stopObject



41
42
43
44
# File 'lib/schked/worker.rb', line 41

def stop
  liveness_probe&.stop
  scheduler.stop
end

#waitObject



37
38
39
# File 'lib/schked/worker.rb', line 37

def wait
  scheduler.join
end