2021-07-27 21:26:18 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module LabelPrinters
|
|
|
|
class PrintJob < ApplicationJob
|
2021-08-02 21:33:51 +08:00
|
|
|
MAX_STATUS_UPDATES = 10
|
|
|
|
|
2021-07-27 21:26:18 +08:00
|
|
|
queue_as :high_priority
|
|
|
|
|
2021-08-02 21:33:51 +08:00
|
|
|
discard_on(StandardError) do |job, _error|
|
|
|
|
label_printer = job.arguments.first
|
|
|
|
label_printer.update!(status: :error)
|
|
|
|
end
|
|
|
|
|
2021-08-12 22:57:25 +08:00
|
|
|
def perform(label_printer, payload, copy_count)
|
2021-07-27 21:26:18 +08:00
|
|
|
case label_printer.type_of
|
|
|
|
when 'fluics'
|
2021-07-27 22:09:23 +08:00
|
|
|
api_client = LabelPrinters::Fluics::ApiClient.new(
|
2021-07-27 21:26:18 +08:00
|
|
|
label_printer.fluics_api_key
|
2021-07-27 22:09:23 +08:00
|
|
|
)
|
|
|
|
|
2021-08-12 22:57:25 +08:00
|
|
|
copy_count.times do
|
|
|
|
response = api_client.print(label_printer.fluics_lid, payload)
|
2021-07-27 22:09:23 +08:00
|
|
|
|
2021-08-12 22:57:25 +08:00
|
|
|
status = response['status'] == 'OK' ? :ready : LabelPrinter::FLUICS_STATUS_MAP[response['printerStatus']]
|
2021-08-02 17:11:11 +08:00
|
|
|
label_printer.update!(status: status)
|
|
|
|
|
2021-08-12 22:57:25 +08:00
|
|
|
break if status != :ready
|
2021-08-02 17:11:11 +08:00
|
|
|
|
2021-08-12 22:57:25 +08:00
|
|
|
# remove first matching job_id from queue (one label out of batch has been printed)
|
|
|
|
label_printer.with_lock do
|
|
|
|
job_ids = label_printer.current_print_job_ids
|
|
|
|
job_ids.delete_at(job_ids.index(job_id) || job_ids.length)
|
|
|
|
|
|
|
|
label_printer.update!(current_print_job_ids: job_ids)
|
|
|
|
end
|
2021-08-02 17:11:11 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-02 21:33:51 +08:00
|
|
|
# mark as unreachable if no final state is reached
|
2021-08-12 22:57:25 +08:00
|
|
|
label_printer.update!(status: :unreachable) unless label_printer.status.in? %w(ready out_of_labels error)
|
2021-07-27 21:26:18 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|