Merge pull request #3457 from artoscinote/ma_SCI_5903

Added print job queue system, job status endpoint [SCI-5903]
This commit is contained in:
artoscinote 2021-07-29 13:06:34 +02:00 committed by GitHub
commit 3649e2fbe0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 58 additions and 3 deletions

View file

@ -0,0 +1,7 @@
# frozen_string_literal: true
class ActiveJobsController < ApplicationController
def status
render json: { status: ApplicationJob.status(params[:id]) }
end
end

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,21 @@
# 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'
api_client = LabelPrinters::Fluics::ApiClient.new(
label_printer.fluics_api_key
)
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
end

View file

@ -4,7 +4,9 @@ module LabelPrinters
module Fluics
class ApiClient
class NotFoundError < StandardError; end
class ServerError < StandardError; end
class BadRequestError < StandardError; end
include HTTParty

View file

@ -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.

View file

@ -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',