Class: PrintNodeServer
Constant Summary
collapse
- CHROME_PATH =
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
- POLL_EVERY =
10
Instance Method Summary
collapse
Methods inherited from ProxyServer
#initialize, #set_mime_types
Constructor Details
This class inherits a constructor from ProxyServer
Instance Method Details
#check_for_requirements ⇒ Object
18
19
20
21
|
# File 'lib/print_node_server.rb', line 18
def check_for_requirements
raise "Could not find Google Chrome. Please make sure Google Chrome is installed." if !File.exists?(CHROME_PATH)
raise "Could not find chromehtml2pdf. Run:\nnpm install -g chromehtml2pdf" if !File.which("chromehtml2pdf")
end
|
#default_printer ⇒ Object
96
97
98
|
# File 'lib/print_node_server.rb', line 96
def default_printer
""
end
|
#loop_action ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/print_node_server.rb', line 64
def loop_action
res = send_post
@last_job_success = false
@last_job_id = @last_job_output = nil
if match = res.body.match(/<next-job-id[^>]*>([^<]*)<\/next-job-id>/)
@last_job_id = match[1]
end
if match = res.body.match(/<next-job-name[^>]*>([^<]*)<\/next-job-name>/)
@last_job_name = match[1]
end
if match = res.body.match(/<next-job-url>([^<]*)<\/next-job-url>/)
url = match[1].gsub("&", "&")
path = "/tmp/VaePrintNode-#{@last_job_id}-#{rand.to_s.gsub(".", "")}.pdf"
puts "Printing #{url} to #{path}"
puts `chromehtml2pdf --displayHeaderFooter true --headerTemplate "<div></div>" --footerTemplate "<div style='font-size: 14px; padding-left: 38px'>#{@last_job_name} <span class='pageNumber'></span> / <span class='totalPages'></span></div>" --printBackground true --marginLeft 1 --marginRight 1 --marginTop 1 --marginBottom 1 --out "#{path}" #{Shellwords.shellescape(url)}`
if File.exists?(path)
@last_job_output = `lp #{path}`
Thread.new { sleep 15; FileUtils.rm(path) }
else
@last_job_output = "Error: could not generate PDF!"
end
@last_job_success = true if @last_job_output =~ /request id/
puts @last_job_output
end
rescue StandardError => e
puts e.message
end
|
#main_loop ⇒ Object
56
57
58
59
60
61
62
|
# File 'lib/print_node_server.rb', line 56
def main_loop
loop do
break if @stop or (@last_successful_post_at + 3600) < Time.now
loop_action if (@last_post_at + POLL_EVERY) < Time.now
sleep 1
end
end
|
#node_name ⇒ Object
92
93
94
|
# File 'lib/print_node_server.rb', line 92
def node_name
Socket.gethostname
end
|
#print_queue_status ⇒ Object
104
105
106
|
# File 'lib/print_node_server.rb', line 104
def print_queue_status
`lpq`
end
|
#printers_list ⇒ Object
100
101
102
|
# File 'lib/print_node_server.rb', line 100
def printers_list
`lpstat -p`
end
|
#register_node ⇒ Object
23
24
25
26
27
28
29
30
|
# File 'lib/print_node_server.rb', line 23
def register_node
res = send_post
if match = res.body.match(/<secret-key>([a-f0-9]*)<\/secret-key>/)
@node_id = "/#{match[1]}"
else
raise "Could not connect to Vae to register print node."
end
end
|
#run ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
|
# File 'lib/print_node_server.rb', line 5
def run
check_for_requirements
register_node
puts "Vae Print Node active, waiting for print jobs ..."
puts " (hit Control+C to exit)"
trap("INT") { @stop = true }
main_loop
puts "Quit signal received, cleaning up ..."
end
|
#send_post ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/print_node_server.rb', line 32
def send_post
@last_post_at = Time.now
options = {
method: :post,
headers: { "Content-type": "application/json" },
url: "#{@site.vaeplatform_url}/api/local/v1/print_nodes#{@node_id}",
payload: {
print_node: {
name: node_name,
default_printer: default_printer,
printers_list: printers_list,
print_queue_status: print_queue_status,
last_job_id: @last_job_id,
last_job_success: @last_job_success,
last_job_output: @last_job_output
}
}.to_json
}
options[:verify_ssl] = false if ENV['VAEPLATFORM_LOCAL']
res = RestClient::Request.execute(options)
@last_successful_post_at = Time.now
res
end
|