Class: ZmeygoSync::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/zmeygo_sync/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Command

Returns a new instance of Command.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/zmeygo_sync/command.rb', line 10

def initialize(args ={})
   @options = {
    :verbose => false,
    :pid_dir => "#{Rails.root}/tmp/pids",
    :sleep_delay => 60,
    :debug => false # when debug is false - it will daomonize, otherwise - it won't
  }

  @monitor = false

  opts = OptionParser.new do |opts|
    opts.banner = "Usage: #{File.basename($0)} [options] start|stop|restart|run"

    opts.on('-h', '--help', 'Show this message') do
      puts opts
      exit 1
    end
    opts.on('-s','--sleep-delay N', "Amount of time to sleep when no jobs are found") do |n|
      @options[:sleep_delay] = n.to_i
    end
    opts.on('-v','--verbose', "verbosity") do
      @options[:verbose] = true
    end
    opts.on('-d','--debug', "debug mode, don't daemonize") do
      @options[:debug] = true
    end
  end
  @args = opts.parse!(args)

  ZmeygoSync.configure

  self.options = @options
  self.client = ZmeygoSync::Client.new(@options)

end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



8
9
10
# File 'lib/zmeygo_sync/command.rb', line 8

def client
  @client
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/zmeygo_sync/command.rb', line 8

def options
  @options
end

Instance Method Details

#daemonizeObject



71
72
73
74
75
76
77
# File 'lib/zmeygo_sync/command.rb', line 71

def daemonize
  if @options[:debug]
    run
  else
    Daemons.run_proc('zmeygo_sync') { run }
  end
end

#runObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/zmeygo_sync/command.rb', line 46

def run
  unless defined?(Rails)
    raise "This is not rails app???"
  end
  step = 0
  loop do
    Rails.logger.info "[zmeygo_sync] pushing..."
    self.client.push
    # once in 5 minutes download locales from server
    if step % 10 == 0
      Rails.logger.info "[zmeygo_sync] pulling..."
      self.client.pull
    end
    step += 1
    if step > 1_000
      step = 0
    end
    sleep(@options[:sleep_delay])
  end
rescue => e
  Rails.logger.fatal e
  STDERR.puts e.message
  exit 1
end