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..96b4d4f54 --- /dev/null +++ b/app/jobs/label_printers/print_job.rb @@ -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 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',