Class: Qwirk::Manager

Inherits:
Object
  • Object
show all
Includes:
Rumx::Bean
Defined in:
lib/qwirk/manager.rb

Constant Summary collapse

@@default_options =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter_factory, options = {}) ⇒ Manager

Constructs a manager. Accepts a hash of config options

name         - name which this bean will be added
env          - environment being executed under.  For a rails project, this will be the value of Rails.env
worker_file  - the worker file is a hash with the environment or hostname as the primary key and a subhash with the worker names
  as the keys and the config options for the value.  In this file, the env will be searched first and if that doesn't exist,
  the hostname will then be searched.  Workers can be defined for development without having to specify the hostname.  For
  production, a set of workers could be defined under production or specific workers for each host name.
persist_file - WorkerConfig attributes that are modified externally (via Rumx interface) will be stored in this file.  Without this
  option, external config changes that are made will be lost when the Manager is restarted.


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/qwirk/manager.rb', line 29

def initialize(adapter_factory, options={})
  @adapter_factory    = adapter_factory
  options           = @@default_options.merge(options)
  @stopped          = false
  @name             = options[:name] || Qwirk::DEFAULT_NAME
  @poll_time        = options[:poll_time] || 3.0
  @stop_time        = options[:stop_time]
  @worker_configs   = []
  @env              = options[:env]
  @worker_options   = parse_worker_file(options[:worker_file])
  @persist_file     = options[:persist_file]
  @persist_options  = (@persist_file && File.exist?(@persist_file)) ? YAML.load_file(@persist_file) : {}

  BaseWorker.worker_classes.each do |worker_class|
    worker_class.each_config(adapter_factory.worker_config_class) do |config_name, extended_worker_config_class, default_options|
      # Least priority is config default_options defined in the Worker class, then the workers.yml file,
      # highest priority is persist_file (ad-hoc changes made manually)
      options = {}
      options = options.merge(@worker_options[config_name]) if @worker_options[config_name]
      options = options.merge(@persist_options[config_name]) if @persist_options[config_name]
      worker_config = extended_worker_config_class.new(adapter_factory, config_name, self, worker_class, default_options, options)
      bean_add_child(config_name, worker_config)
      @worker_configs << worker_config
    end
  end

  start_timer_thread
  at_exit { stop }
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



9
10
11
# File 'lib/qwirk/manager.rb', line 9

def env
  @env
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/qwirk/manager.rb', line 9

def name
  @name
end

#worker_configsObject (readonly)

Returns the value of attribute worker_configs.



9
10
11
# File 'lib/qwirk/manager.rb', line 9

def worker_configs
  @worker_configs
end

Class Method Details

.default_options=(options) ⇒ Object



16
17
18
# File 'lib/qwirk/manager.rb', line 16

def self.default_options=(options)
  @@default_options = options
end

Instance Method Details

#[](name) ⇒ Object



128
129
130
131
132
133
# File 'lib/qwirk/manager.rb', line 128

def [](name)
  @worker_configs.each do |worker_config|
    return worker_config if worker_config.name == name
  end
  return nil
end

#save_persist_stateObject

Store off any options that are no longer set to default



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/qwirk/manager.rb', line 100

def save_persist_state
  return unless @persist_file
  new_persist_options = {}
  BaseWorker.worker_classes.each do |worker_class|
    worker_class.each_config(@adapter_factory.worker_config_class) do |config_name, ignored_extended_worker_config_class, default_options|
      static_options = default_options.merge(@worker_options[config_name] || {})
      worker_config = self[config_name]
      hash = {}
      # Only store off the config values that are specifically different from default values or values set in the workers.yml file
      # Then updates to these values will be allowed w/o being hardcoded to an old default value.
      worker_config.bean_get_attributes do |attribute_info|
        if attribute_info.attribute[:config_item] && attribute_info.ancestry.size == 1
          param_name = attribute_info.ancestry[0].to_sym
          value = attribute_info.value
          hash[param_name] = value if static_options[param_name] != value
        end
      end
      new_persist_options[config_name] = hash unless hash.empty?
    end
  end
  if new_persist_options != @persist_options
    @persist_options = new_persist_options
    File.open(@persist_file, 'w') do |out|
      YAML.dump(@persist_options, out )
    end
  end
end

#start_timer_threadObject

Create a timer_thread to make periodic calls to the worker_configs in order to do such things as expand/contract workers, etc.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/qwirk/manager.rb', line 61

def start_timer_thread
  @timer_thread = Thread.new do
    begin
      while !@stopped
        @worker_configs.each do |worker_config|
          worker_config.periodic_call(@poll_time)
        end
        sleep @poll_time
      end
    rescue Exception => e
      Qwirk.logger.error "Timer thread failed with exception: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
    end
  end
end

#stopObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/qwirk/manager.rb', line 76

def stop
  return if @stopped
  @stopped = true
  Qwirk.logger.info "Stopping manager"
  @worker_configs.each { |worker_config| worker_config.stop }
  if @stop_time
    end_time = Time.now + @stop_time
    @worker_configs.each do |worker_config|
      t = end_time - Time.now
      t = 0 if t < 0
      worker_config.join(t)
    end
  else
    @worker_configs.each { |worker_config| worker_config.join }
  end
  @timer_thread.join
  Qwirk.logger.info "Done stopping manager"
end

#stopped?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/qwirk/manager.rb', line 95

def stopped?
  @stopped
end