From e57132dbd0182b284b77bae33a051f2b52337a10 Mon Sep 17 00:00:00 2001 From: Martin Artnik Date: Tue, 27 Jul 2021 15:26:18 +0200 Subject: [PATCH 1/2] Added print job queue system, job status endpoint [SCI-5903] --- app/controllers/active_jobs_controller.rb | 7 +++++++ app/controllers/label_printers_controller.rb | 14 ++++++++++++++ app/jobs/application_job.rb | 10 ++++++++++ app/jobs/label_printers/print_job.rb | 16 ++++++++++++++++ app/services/label_printers/fluics/api_client.rb | 2 ++ config/initializers/delayed_job_config.rb | 4 +--- config/routes.rb | 3 +++ 7 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 app/controllers/active_jobs_controller.rb create mode 100644 app/jobs/label_printers/print_job.rb diff --git a/app/controllers/active_jobs_controller.rb b/app/controllers/active_jobs_controller.rb new file mode 100644 index 000000000..2f51490db --- /dev/null +++ b/app/controllers/active_jobs_controller.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class ActiveJobsController < ApplicationController + def status + render json: { status: ApplicationJob.status(params[:id]) } + end +end diff --git a/app/controllers/label_printers_controller.rb b/app/controllers/label_printers_controller.rb index c16c7061c..5db443621 100644 --- a/app/controllers/label_printers_controller.rb +++ b/app/controllers/label_printers_controller.rb @@ -47,6 +47,16 @@ class LabelPrintersController < ApplicationController redirect_to addons_path end + def print + print_job = LabelPrinters::PrintJob.perform_later( + LabelPrinter.find(params[:id]), + LabelTemplate.find(print_job_params[:label_template_id]) + .render(print_job_params[:locals]) + ) + + render json: { job_id: print_job.job_id } + end + def create_fluics # Placeholder for FLUICS printer management @@ -72,6 +82,10 @@ class LabelPrintersController < ApplicationController ) end + def print_job_params + params.require(:label_template_id, :label_template_locals) + end + def find_label_printer @label_printer = LabelPrinter.find(params[:id]) end diff --git a/app/jobs/application_job.rb b/app/jobs/application_job.rb index 43fd60b59..22121dc06 100644 --- a/app/jobs/application_job.rb +++ b/app/jobs/application_job.rb @@ -6,4 +6,14 @@ class ApplicationJob < ActiveJob::Base # Most jobs are safe to ignore if the underlying records are no longer available discard_on ActiveJob::DeserializationError + + def self.status(job_id) + delayed_job = Delayed::Job.where('handler LIKE ?', "%job_id: #{job_id}%").last + + return :done unless delayed_job + return :failed if delayed_job.failed_at + return :running if delayed_job.locked_at + + :pending + end end diff --git a/app/jobs/label_printers/print_job.rb b/app/jobs/label_printers/print_job.rb new file mode 100644 index 000000000..4bf2fa2cc --- /dev/null +++ b/app/jobs/label_printers/print_job.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module LabelPrinters + class PrintJob < ApplicationJob + queue_as :high_priority + + def perform(label_printer, payload) + case label_printer.type_of + when 'fluics' + LabelPrinters::Fluics::ApiClient.new( + label_printer.fluics_api_key + ).print(label_printer.fluics_lid, payload) + end + end + end +end diff --git a/app/services/label_printers/fluics/api_client.rb b/app/services/label_printers/fluics/api_client.rb index dfe346966..6242906de 100644 --- a/app/services/label_printers/fluics/api_client.rb +++ b/app/services/label_printers/fluics/api_client.rb @@ -4,7 +4,9 @@ module LabelPrinters module Fluics class ApiClient class NotFoundError < StandardError; end + class ServerError < StandardError; end + class BadRequestError < StandardError; end include HTTParty diff --git a/config/initializers/delayed_job_config.rb b/config/initializers/delayed_job_config.rb index 4f51127e5..a2bc5f02a 100644 --- a/config/initializers/delayed_job_config.rb +++ b/config/initializers/delayed_job_config.rb @@ -24,9 +24,7 @@ module DelayedWorkerConfig # or left in the database with "failed_at" set dempends on the # DESTROY_FAILED_JOBS value def max_attempts - value = ENV['DELAYED_WORKER_MAX_ATTEMPTS'].to_i - return 6 if value.zero? - value + 1 end # The default DELAYED_WORKER_MAX_RUN_TIME is 30.minutes. diff --git a/config/routes.rb b/config/routes.rb index 8df08c0a5..99848475d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -21,6 +21,8 @@ Rails.application.routes.draw do resources :activities, only: [:index] + get '/jobs/:id/status', to: 'active_jobs#status' + get 'forbidden', to: 'application#forbidden', as: 'forbidden' get 'not_found', to: 'application#not_found', as: 'not_found' @@ -44,6 +46,7 @@ Rails.application.routes.draw do resources :label_printers, except: :show, path: 'users/settings/account/addons/label_printers' do post :create_fluics, on: :collection + post :print, on: :member end get 'users/settings/account/connected_accounts', From 3f2a294cdc560f79093297bd9bc818be683db0a7 Mon Sep 17 00:00:00 2001 From: Martin Artnik Date: Tue, 27 Jul 2021 16:09:23 +0200 Subject: [PATCH 2/2] Added wait for printer ready state in print job [SCI-5903] --- app/jobs/label_printers/print_job.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/jobs/label_printers/print_job.rb b/app/jobs/label_printers/print_job.rb index 4bf2fa2cc..96b4d4f54 100644 --- a/app/jobs/label_printers/print_job.rb +++ b/app/jobs/label_printers/print_job.rb @@ -7,9 +7,14 @@ module LabelPrinters def perform(label_printer, payload) case label_printer.type_of when 'fluics' - LabelPrinters::Fluics::ApiClient.new( + api_client = LabelPrinters::Fluics::ApiClient.new( label_printer.fluics_api_key - ).print(label_printer.fluics_lid, payload) + ) + + api_client.print(label_printer.fluics_lid, payload) + + # wait for FLUICS printer to stop being busy + sleep(5) while api_clinet.status(label_printer.fluics_lid).dig('printerState', 'printerStatus') != '00' end end end