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



46
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
# File 'lib/hardcode/cli.rb', line 46

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



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

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

#watch(source_dir) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/hardcode/cli.rb', line 111

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
      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
  end
  listener.start
  sleep
end

#workObject



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/hardcode/cli.rb', line 89

def work
  Sneakers.configure(
    amqp: options[:amqp_url],
    vhost: options[:amqp_vhost],
    daemonize: false,
    log: STDOUT,
    metrics: Sneakers::Metrics::LoggingMetrics.new
  )
  Sneakers.logger.level = options[:debug] ? Logger::DEBUG : Logger::INFO
  r = Sneakers::Runner.new([ Hardcode::Worker ])
  r.run
end