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



42
43
44
45
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
# File 'lib/hardcode/cli.rb', line 42

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
    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



29
30
31
# File 'lib/hardcode/cli.rb', line 29

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

#watch(source_dir) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/hardcode/cli.rb', line 106

def watch(source_dir)
  FileUtils.touch LOCK_FILE
  conn = Bunny.new
  conn.start
  ch = conn.create_channel
  q = ch.queue('stack_encode', durable: true)
  listener = Listen.to(source_dir) do |modified, added, removed|
    unless added.empty?
      source_file = added.first
      # 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



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/hardcode/cli.rb', line 85

def work
  Sneakers.configure(
    amqp: options[:ampq_url],
    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