Optimize how printing jobs and statuses work [SCI-5984]

This commit is contained in:
Martin Artnik 2021-08-12 16:57:25 +02:00
parent a65d8ad365
commit 1d3e0db904
3 changed files with 30 additions and 29 deletions

View file

@ -86,24 +86,26 @@ class RepositoryRowsController < ApplicationController
end end
def print def print
# reset all potential error states for printers and discard all jobs
# rubocop:disable Rails/SkipsModelValidations
LabelPrinter.update_all(status: :ready, current_print_job_ids: [])
# rubocop:enable Rails/SkipsModelValidations
label_printer = LabelPrinter.find(params[:label_printer_id]) label_printer = LabelPrinter.find(params[:label_printer_id])
# reset potential error state
label_printer.update!(status: :ready, current_print_job_ids: [])
job_ids = RepositoryRow.where(id: params[:repository_row_ids]).flat_map do |repository_row| job_ids = RepositoryRow.where(id: params[:repository_row_ids]).flat_map do |repository_row|
Array.new(params[:copies].to_i).map do LabelPrinters::PrintJob.perform_later(
LabelPrinters::PrintJob.perform_later( label_printer,
label_printer, LabelTemplate.first.render( # Currently we will only use the default template
LabelTemplate.first.render( # Currently we will only use the default template item_id: repository_row.code,
item_id: repository_row.code, item_name: repository_row.name
item_name: repository_row.name ),
) params[:copies].to_i
).job_id ).job_id
end
end end
label_printer.update!(current_print_job_ids: job_ids) label_printer.update!(current_print_job_ids: job_ids * params[:copies].to_i)
redirect_to repository_path(@repository) redirect_to repository_path(@repository)
end end

View file

@ -11,36 +11,33 @@ module LabelPrinters
label_printer.update!(status: :error) label_printer.update!(status: :error)
end end
def perform(label_printer, payload) def perform(label_printer, payload, copy_count)
case label_printer.type_of case label_printer.type_of
when 'fluics' when 'fluics'
api_client = LabelPrinters::Fluics::ApiClient.new( api_client = LabelPrinters::Fluics::ApiClient.new(
label_printer.fluics_api_key label_printer.fluics_api_key
) )
api_client.print(label_printer.fluics_lid, payload) copy_count.times do
response = api_client.print(label_printer.fluics_lid, payload)
# wait for FLUICS printer to stop being busy status = response['status'] == 'OK' ? :ready : LabelPrinter::FLUICS_STATUS_MAP[response['printerStatus']]
MAX_STATUS_UPDATES.times do
status =
LabelPrinter::FLUICS_STATUS_MAP[
api_client.status(label_printer.fluics_lid).dig('printerState', 'printerStatus')
]
label_printer.update!(status: status) label_printer.update!(status: status)
break if status == :ready break if status != :ready
sleep 5 # 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
end end
end end
# mark as unreachable if no final state is reached # mark as unreachable if no final state is reached
label_printer.update!(status: :unreachable) unless label_printer.status.in? %w(ready out_of_labels) label_printer.update!(status: :unreachable) unless label_printer.status.in? %w(ready out_of_labels error)
label_printer.with_lock do
label_printer.current_print_job_ids.delete(job_id)
label_printer.save!
end
end end
end end
end end

View file

@ -1641,12 +1641,14 @@ en:
not_ready: "Not Ready" not_ready: "Not Ready"
out_of_labels: "Out of labels" out_of_labels: "Out of labels"
error: "Printer error" error: "Printer error"
unreachable: "Printer offline"
multiple_items: "%{item_count}/%{starting_item_count} labels" multiple_items: "%{item_count}/%{starting_item_count} labels"
printing_status: printing_status:
done: "Done" done: "Done"
printing: "Printing" printing: "Printing"
out_of_labels: "Waiting for labels. Please, insert labels." out_of_labels: "Waiting for labels. Please, insert labels."
error: "There is an issue with the printer." error: "There is an issue with the printer."
unreachable: "Printer is offline"
activities: activities:
index: index:
global_activities_title: "Global activities" global_activities_title: "Global activities"