Class: BigWig::Push

Inherits:
Object
  • Object
show all
Defined in:
lib/bigwig/push.rb

Overview

pushes a message onto the queue USAGE:

push = BigWig::Push.new :config => '/path/to/config/file', :timeout => 5

By default, it uses bigwig.yml in the current directory and a timeout of 5 seconds The configuration file is expected to have the same structure as the BigWig daemon’s file (without the plugins folder)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(overrides = {}) ⇒ Push

Create a new BigWig::Push Options:

:config => '/path/to/config/file'
:timeout => 5


23
24
25
26
27
28
# File 'lib/bigwig/push.rb', line 23

def initialize(overrides = {})
  @options = {
    :config => File.expand_path('./bigwig.yml'), 
    :timeout => 5
  }.merge(overrides)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



15
16
17
# File 'lib/bigwig/push.rb', line 15

def config
  @config
end

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/bigwig/push.rb', line 16

def options
  @options
end

Class Method Details

.load_options_from(command_line) ⇒ Object

Helper method so that calling scripts don’t need to parse the Push parameters themselves

USAGE:

options = BigWig::Push.load_options_from ARGV
BigWig::Push.new(options).message 'whatever'

If this is being called from a wrapper script (such as bin/bigwig-push) that needs its own parameters you can pass a block to append your own details

options = BigWig::Push.load_options_from(ARGV) do | parser, options | 
  parser.on('--xxx', 'Push it good') do 
    options[:push_it_good] = :push_it_real_good
  end 
end


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bigwig/push.rb', line 69

def self.load_options_from command_line
  options = {}
        
  parser = OptionParser.new do | parser |
    parser.banner = "Usage: #{File.basename($0)} [options]"

    parser.on('-h', '--help', 'Show this message') do
      puts parser
      exit 2
    end
    
    parser.on('-c', '--config=file', 'Path to the config file; if not supplied then bigwig looks for bigwig.yml in the current folder') do |conf|
      options[:config] = conf
    end
    
    parser.on('-t', '--timeout=value', 'Timeout value in seconds') do | conf | 
      options[:timeout] = conf.to_i
    end 
    
    yield(parser, options) if block_given?

  end
  parser.parse!(command_line)
  
  return options
end

Instance Method Details

#message(method, data = {}, task_id = nil, queue = :default) ⇒ Object

Push a message on to the queue, passing in a message name, plus an optional data hash, an optional task id and an optional queue name



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bigwig/push.rb', line 32

def message method, data = {}, task_id = nil, queue = :default
  BigWig::logger.info("Pushing #{method} on to #{queue}")
  @config = YAML.load(File.open(@options[:config]))
  BigWig::connect_using @config

  begin
    Timeout::timeout(@options[:timeout]) do
      begin
        Warren::Queue.publish queue, :method => method, :id => task_id, :data => data
      rescue Exception => ex
        BigWig::logger.error("...push failed with #{ex}")
        exit 1
      end
    end
  rescue Timeout::Error => te
    BigWig::logger.error("...push timed out: #{te}")
    exit 1
  end

  BigWig::logger.info("...bigwig push finished")
  exit 0
end