Merge pull request #3482 from artoscinote/ma_SCI_5984

Optimize how printing jobs and statuses work [SCI-5984]
This commit is contained in:
artoscinote 2021-08-13 09:25:42 +02:00 committed by GitHub
commit 12bb44147c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 29 deletions

View file

@ -86,24 +86,26 @@ class RepositoryRowsController < ApplicationController
end
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])
# 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|
Array.new(params[:copies].to_i).map do
LabelPrinters::PrintJob.perform_later(
label_printer,
LabelTemplate.first.render( # Currently we will only use the default template
item_id: repository_row.code,
item_name: repository_row.name
)
).job_id
end
LabelPrinters::PrintJob.perform_later(
label_printer,
LabelTemplate.first.render( # Currently we will only use the default template
item_id: repository_row.code,
item_name: repository_row.name
),
params[:copies].to_i
).job_id
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)
end

View file

@ -11,36 +11,33 @@ module LabelPrinters
label_printer.update!(status: :error)
end
def perform(label_printer, payload)
def perform(label_printer, payload, copy_count)
case label_printer.type_of
when 'fluics'
api_client = LabelPrinters::Fluics::ApiClient.new(
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
MAX_STATUS_UPDATES.times do
status =
LabelPrinter::FLUICS_STATUS_MAP[
api_client.status(label_printer.fluics_lid).dig('printerState', 'printerStatus')
]
status = response['status'] == 'OK' ? :ready : LabelPrinter::FLUICS_STATUS_MAP[response['printerStatus']]
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
# 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.with_lock do
label_printer.current_print_job_ids.delete(job_id)
label_printer.save!
end
label_printer.update!(status: :unreachable) unless label_printer.status.in? %w(ready out_of_labels error)
end
end
end

View file

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