Class: Hardcode::Cli

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/hardcode/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/hardcode/cli.rb', line 15

def self.exit_on_failure?
  true
end

Instance Method Details

#enqueue(source_dir) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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

#versionObject



34
35
36
# File 'lib/hardcode/cli.rb', line 34

def version
  say "hardcode v#{Hardcode::VERSION}"
end

#watch(source_dir) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# 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

#workObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 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