Method: Hardcode::Cli#watch

Defined in:
lib/hardcode/cli.rb

#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