Skip to content

Commit d0afc42

Browse files
committed
send file to printer service in background job
1 parent 6c2b53f commit d0afc42

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class SendFileToPrintHostJob < ApplicationJob
2+
queue_as :low
3+
unique :until_executed
4+
5+
def perform(print_host:, file:)
6+
service = print_host.service
7+
# Check print host is OK before sending, otherwise raise an ArgumentError to try again later
8+
raise PrintHost::NotReady unless service.ok?
9+
service.upload(file: file, start_print: true)
10+
end
11+
end

app/models/print_host.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ class PrintHost < ApplicationRecord
33

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

6+
class NotReady < RuntimeError
7+
end
8+
69
validates :name, presence: true
710
validates :endpoint, presence: true
811
validates :protocol, presence: true, inclusion: {in: PROTOCOLS.keys}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require "rails_helper"
2+
3+
RSpec.describe SendFileToPrintHostJob do
4+
let(:print_host) { create(:print_host) }
5+
let(:file) { create(:model_file, filename: "test.gcode") }
6+
7+
it "raises PrintHost::NotReady if printer is not ok" do
8+
stub_service = instance_double(Print::MoonrakerService)
9+
allow(stub_service).to receive(:ok?).and_return(false)
10+
allow(print_host).to receive(:service).and_return(stub_service)
11+
12+
expect { described_class.perform_now(print_host: print_host, file: file) }.to raise_error PrintHost::NotReady
13+
end
14+
15+
it "calls upload method in relevant print service" do
16+
stub_service = instance_double(Print::MoonrakerService)
17+
allow(stub_service).to receive(:ok?).and_return(true)
18+
allow(stub_service).to receive(:upload)
19+
allow(print_host).to receive(:service).and_return(stub_service)
20+
21+
described_class.perform_now(print_host: print_host, file: file)
22+
expect(stub_service).to have_received(:upload).with(file: file, start_print: true).once
23+
end
24+
end

0 commit comments

Comments
 (0)