-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
51 lines (41 loc) · 1.34 KB
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env ruby
# encoding: utf-8
require "rubygems"
require "bunny"
require "logger"
require "yaml"
# Load configuration settings
CONFIG = YAML.load_file('config.yml')
# Setup logger
logger = Logger.new(STDOUT)
logger.level = Logger::DEBUG
# Consumer Class for Print Jobs
class ZebraJobConsumer < Bunny::Consumer
def cancelled?
@cancelled
end
def handle_cancellation(_)
@cancelled = true
end
end
# Setup RabbitMQ connection
mq = Bunny.new "amqp://#{CONFIG["RABBITMQ"]["USER"]}:#{CONFIG["RABBITMQ"]["PASS"]}@#{CONFIG["RABBITMQ"]["HOST"]}:#{CONFIG["RABBITMQ"]["PORT"]}"
mq.start
logger.info "Started"
channel = mq.create_channel
exchange = channel.topic "partbox", :auto_delete => true
queue = channel.queue("print_queue").bind(exchange, :routing_key => "print_queue")
print_queue_consumer = ZebraJobConsumer.new(channel, queue)
print_queue_consumer.on_delivery do |delivery_info, metadata, payload|
logger.info "Got print job"
logger.info payload
logger.info "This is heading to the printer called:#{metadata[:headers]["printer"]}"
# Build command to print label
command = %W(lpr -P #{metadata[:headers]["printer"]} -o raw)
process = IO.popen(command, "r+")
process.write(payload)
process.close_write()
Process.wait(process.pid)
logger.info "Print queued"
end
queue.subscribe_with(print_queue_consumer, :block => true)