class PrintNodeServer

Constants

CHROME_PATH
POLL_EVERY

Public Instance Methods

check_for_requirements() click to toggle source
# 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() click to toggle source
# File lib/print_node_server.rb, line 96
def default_printer
  ""
end
loop_action() click to toggle source
# 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("&amp;", "&")
    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() click to toggle source
# 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() click to toggle source
# File lib/print_node_server.rb, line 92
def node_name
  Socket.gethostname
end
print_queue_status() click to toggle source
printers_list() click to toggle source
# File lib/print_node_server.rb, line 100
def printers_list
  `lpstat -p`
end
register_node() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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