Module: Lab::Lims::Worker

Defined in:
app/services/lab/lims/worker.rb

Overview

Pull/Push orders from/to the LIMS queue (Oops meant CouchDB).

Constant Summary collapse

LOG_FILES_TO_KEEP =
5
LOG_FILE_SIZE =
500.megabytes

Class Method Summary collapse

Class Method Details

.file_logger(worker_name) ⇒ Object



74
75
76
# File 'app/services/lab/lims/worker.rb', line 74

def self.file_logger(worker_name)
  Logger.new(log_path("#{worker_name}.log"), LOG_FILES_TO_KEEP, LOG_FILE_SIZE)
end

.lims_apiObject



89
90
91
# File 'app/services/lab/lims/worker.rb', line 89

def self.lims_api
  Lab::Lims::ApiFactory.create_api
end

.log_path(filename) ⇒ Object



78
79
80
# File 'app/services/lab/lims/worker.rb', line 78

def self.log_path(filename)
  Lab::Lims::Utils::LIMS_LOG_PATH.join(filename)
end

.realtime_updates_enabled?Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
# File 'app/services/lab/lims/worker.rb', line 82

def self.realtime_updates_enabled?
  Lims::Config.updates_socket.key?('url')
rescue Lab::Lims::Config::ConfigNotFound => e
  Rails.logger.warn("Check for realtime updates failed: #{e.message}")
  false
end

.startObject



12
13
14
15
16
17
18
19
20
21
# File 'app/services/lab/lims/worker.rb', line 12

def self.start
  User.current = Utils.lab_user

  fork(&method(:start_push_worker))
  # fork(&method(:start_pull_worker))
  fork(&method(:start_acknowledgement_worker))
  fork(&method(:start_realtime_pull_worker)) if realtime_updates_enabled?

  Process.waitall
end

.start_acknowledgement_workerObject



31
32
33
34
35
36
# File 'app/services/lab/lims/worker.rb', line 31

def self.start_acknowledgement_worker
  start_worker('acknowledgement_worker') do
    worker = AcknowledgementWorker.new(lims_api)
    worker.push_acknowledgement
  end
end

.start_pull_workerObject



38
39
40
41
42
43
44
# File 'app/services/lab/lims/worker.rb', line 38

def self.start_pull_worker
  start_worker('pull_worker') do
    worker = PullWorker.new(lims_api)

    worker.pull_orders
  end
end

.start_push_workerObject



23
24
25
26
27
28
29
# File 'app/services/lab/lims/worker.rb', line 23

def self.start_push_worker
  start_worker('push_worker') do
    worker = PushWorker.new(lims_api)

    worker.push_orders # (wait: true)
  end
end

.start_realtime_pull_workerObject



46
47
48
49
50
51
52
# File 'app/services/lab/lims/worker.rb', line 46

def self.start_realtime_pull_worker
  start_worker('realtime_pull_worker') do
    worker = PullWorker.new(Lims::Api::WsApi.new(Lab::Lims::Config.updates_socket))

    worker.pull_orders
  end
end

.start_worker(worker_name) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/services/lab/lims/worker.rb', line 57

def self.start_worker(worker_name)
  Rails.logger = LoggerMultiplexor.new(file_logger(worker_name), $stdout)
  ActiveRecord::Base.logger = Rails.logger
  Rails.logger.level = :debug

  File.open(log_path("#{worker_name}.lock"), File::RDWR | File::CREAT, 0o644) do |fout|
    unless fout.flock(File::LOCK_EX | File::LOCK_NB)
      Rails.logger.warn("Another process already holds lock #{worker_name} (#{fout.read}), exiting...")
      break
    end

    fout.write("Locked by process ##{Process.pid} under process group ##{Process.ppid} at #{Time.now}")
    fout.flush
    yield
  end
end