Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/jobs/send_file_to_print_host_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class SendFileToPrintHostJob < ApplicationJob
queue_as :low
unique :until_executed

def perform(print_host:, file:)
service = print_host.service
# Check print host is OK before sending, otherwise raise an ArgumentError to try again later
raise PrintHost::NotReady unless service.ok?
service.upload(file: file, start_print: true)
end
end
3 changes: 3 additions & 0 deletions app/models/print_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ class PrintHost < ApplicationRecord

PROTOCOLS = Print.constants.map { [const_get("Print::#{it}::PROTOCOL"), const_get("Print::#{it}")] }.to_h.freeze

class NotReady < RuntimeError
end

validates :name, presence: true
validates :endpoint, presence: true
validates :protocol, presence: true, inclusion: {in: PROTOCOLS.keys}
Expand Down
23 changes: 23 additions & 0 deletions spec/jobs/send_file_to_print_host_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require "rails_helper"

RSpec.describe SendFileToPrintHostJob do
let(:print_host) { create(:print_host) }
let(:file) { create(:model_file, filename: "test.gcode") }

it "raises PrintHost::NotReady if printer is not ok" do
stub_service = instance_double(Print::MoonrakerService)
allow(stub_service).to receive(:ok?).and_return(false)
allow(print_host).to receive(:service).and_return(stub_service)

expect { described_class.perform_now(print_host: print_host, file: file) }.to raise_error PrintHost::NotReady
end

it "calls upload method in relevant print service" do # rubocop:disable RSpec/ExampleLength
stub_service = instance_double(Print::MoonrakerService)
allow(stub_service).to receive(:ok?).and_return(true)
allow(stub_service).to receive(:upload)
allow(print_host).to receive(:service).and_return(stub_service)
described_class.perform_now(print_host: print_host, file: file)
expect(stub_service).to have_received(:upload).with(file: file, start_print: true).once
end
end
Loading