class Hardcode::Cli

Public Class Methods

exit_on_failure?() click to toggle source
# File lib/hardcode/cli.rb, line 15
def self.exit_on_failure?
  true
end

Public Instance Methods

enqueue(source_dir) click to toggle source
# File lib/hardcode/cli.rb, line 47
def enqueue(source_dir)
  if File.exists? LOCK_FILE
    puts "Lockfile present: #{LOCK_FILE}"
    puts "Schedule the job to run in 2 minutes."
    %x[echo #{File.expand_path(__FILE__)} | at now + 2 minutes]
    exit $?.exitstatus
  end

  begin
    FileUtils.touch LOCK_FILE
    conn = Bunny.new(options[:amqp_url])
    conn.start
    ch = conn.create_channel
    q = ch.queue('stack_encode', durable: true)
    Dir.glob(File.join(source_dir, "*.*")) do |source_file|
      # wait until the file is fully written and not uploaded anymore
      while system %Q[lsof '#{source_file}']
       sleep 1
      end
      FileUtils.mv(source_file, options[:tmp_dir], verbose: true)
      ch.default_exchange.publish(
        {
          source: File.join(options[:tmp_dir], File.basename(source_file)),
          dest_dir: options[:destination]
        }.to_json,
        routing_key: q.name,
        persistent: true
      )
    end
  rescue => e
    puts "ERROR: #{e.message}"
  ensure
   FileUtils.rm(LOCK_FILE) if File.exists?(LOCK_FILE)
  end
end
version() click to toggle source
# File lib/hardcode/cli.rb, line 34
def version
  say "hardcode v#{Hardcode::VERSION}"
end
vhost_from_amqp_url(url) click to toggle source
# File lib/hardcode/cli.rb, line 145
def vhost_from_amqp_url(url)
  match = url.match(/^amqp:\/\/.+\/(\/|%2f)(.+)$/) rescue []
  vhost = match && match.size >= 3 ? match[2] : ''
  vhost = "/#{vhost}"
end
watch(source_dir) click to toggle source
# File lib/hardcode/cli.rb, line 115
def watch(source_dir)
  FileUtils.touch LOCK_FILE
  conn = Bunny.new(options[:amqp_url])
  conn.start
  ch = conn.create_channel
  q = ch.queue('stack_encode', durable: true)
  listener = Listen.to(source_dir) do |modified, added, removed|
    added.each do |source_file|
      # wait until the file is fully written and not uploaded anymore
      while system %Q[lsof '#{source_file}']
       sleep 1
      end
      worker_options = {
        source: File.join(options[:tmp_dir], File.basename(source_file)),
        dest_dir: options[:destination]
      }
      worker_options[:ffmpeg_options] = options[:ffmpeg_options] if options[:ffmpeg_options]
      FileUtils.mv(source_file, options[:tmp_dir], verbose: true)
      ch.default_exchange.publish(
        worker_options.to_json,
        routing_key: q.name,
        persistent: true
      )
    end
  end
  listener.start
  sleep
end
work() click to toggle source
# File lib/hardcode/cli.rb, line 87
def work
  vhost = vhost_from_amqp_url(options[:amqp_url])
  Sneakers.configure(
    amqp: options[:amqp_url],
    workers: 2,
    threads: 1,
    vhost: vhost,
    daemonize: false,
    log: STDOUT,
    metrics: Sneakers::Metrics::LoggingMetrics.new
  )
  Sneakers.logger.level = options[:debug] ? Logger::DEBUG : Logger::INFO
  Sneakers::Worker.configure_logger(Logger.new STDOUT)
  Sneakers::Runner.new([ Hardcode::Worker ]).run
end