Class: SimpleWorker::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_worker/config.rb

Overview

Config is used to setup the SimpleWorker client. You must set the access_key and secret_key.

config.global_attributes allows you to specify attributes that will automatically be set on every worker,

this is good for database connection information or things that will be used across the board.

config.database configures a database connection. If specified like ActiveRecord, SimpleWorker will automatically establish a connection for you before running your worker.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



34
35
36
37
38
39
40
41
42
43
# File 'lib/simple_worker/config.rb', line 34

def initialize
  @global_attributes = {}
  @extra_requires = []
  @merged = {}
  @unmerged = {}
  @merged_gems = {}
  @unmerged_gems = {}
  @mailers = {}

end

Instance Attribute Details

#databaseObject

Returns the value of attribute database.



13
14
15
# File 'lib/simple_worker/config.rb', line 13

def database
  @database
end

#extra_requiresObject

Returns the value of attribute extra_requires.



13
14
15
# File 'lib/simple_worker/config.rb', line 13

def extra_requires
  @extra_requires
end

#force_uploadObject

Returns the value of attribute force_upload.



13
14
15
# File 'lib/simple_worker/config.rb', line 13

def force_upload
  @force_upload
end

#global_attributesObject

Returns the value of attribute global_attributes.



13
14
15
# File 'lib/simple_worker/config.rb', line 13

def global_attributes
  @global_attributes
end

#hostObject

Returns the value of attribute host.



13
14
15
# File 'lib/simple_worker/config.rb', line 13

def host
  @host
end

#mailerObject

Returns the value of attribute mailer.



13
14
15
# File 'lib/simple_worker/config.rb', line 13

def mailer
  @mailer
end

#mailersObject

Returns the value of attribute mailers.



13
14
15
# File 'lib/simple_worker/config.rb', line 13

def mailers
  @mailers
end

#mergedObject

Returns the value of attribute merged.



13
14
15
# File 'lib/simple_worker/config.rb', line 13

def merged
  @merged
end

#merged_gemsObject

Returns the value of attribute merged_gems.



13
14
15
# File 'lib/simple_worker/config.rb', line 13

def merged_gems
  @merged_gems
end

#modelsObject

Returns the value of attribute models.



13
14
15
# File 'lib/simple_worker/config.rb', line 13

def models
  @models
end

#portObject

Returns the value of attribute port.



13
14
15
# File 'lib/simple_worker/config.rb', line 13

def port
  @port
end

#project_idObject

Returns the value of attribute project_id.



13
14
15
# File 'lib/simple_worker/config.rb', line 13

def project_id
  @project_id
end

#schemeObject

Returns the value of attribute scheme.



13
14
15
# File 'lib/simple_worker/config.rb', line 13

def scheme
  @scheme
end

#server_gemsObject

Returns the value of attribute server_gems.



13
14
15
# File 'lib/simple_worker/config.rb', line 13

def server_gems
  @server_gems
end

#tokenObject

Returns the value of attribute token.



13
14
15
# File 'lib/simple_worker/config.rb', line 13

def token
  @token
end

#unmergedObject

Returns the value of attribute unmerged.



13
14
15
# File 'lib/simple_worker/config.rb', line 13

def unmerged
  @unmerged
end

#unmerged_gemsObject

Returns the value of attribute unmerged_gems.



13
14
15
# File 'lib/simple_worker/config.rb', line 13

def unmerged_gems
  @unmerged_gems
end

Class Method Details

.gems_to_skipObject



60
61
62
# File 'lib/simple_worker/config.rb', line 60

def self.gems_to_skip
  @gems_to_skip
end

Instance Method Details

#access_key=(x) ⇒ Object



45
46
47
# File 'lib/simple_worker/config.rb', line 45

def access_key=(x)
  raise "SimpleWorker Config Error: access_key and secret_key are no longer used. The new SimpleWorker gem requires a couple of small configuration changes, please see: http://docs.simpleworker.com/ruby/new-gem-v2-update-guide for information."
end

#auto_merge=(b) ⇒ Object



64
65
66
67
68
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
95
96
# File 'lib/simple_worker/config.rb', line 64

def auto_merge=(b)
  if b
    SimpleWorker.logger.info "Initializing SimpleWorker for Rails 3..."
    start_time = Time.now
    SimpleWorker.configure do |c2|
      models_path = File.join(Rails.root, 'app/models/*.rb')
      models = Dir.glob(models_path)
      c2.models = models
      models.each { |model| c2.merge(model) }
      mailers_path = File.join(Rails.root, 'app/mailers/*.rb')
      Dir.glob(mailers_path).collect { |m| c2.mailers[File.basename(m)] = {:filename=>m, :name => File.basename(m), :path_to_templates=>File.join(Rails.root, "app/views/#{File.basename(m, File.extname(m))}")} }
      c2.extra_requires += ['active_support/core_ext', 'action_mailer']
      #puts 'DB FILE=' + File.join(Rails.root, 'config', 'database.yml').to_s
      if defined?(ActiveRecord) && File.exist?(File.join(Rails.root, 'config', 'database.yml'))
        c2.extra_requires += ['active_record']
        c2.database = Rails.configuration.database_configuration[Rails.env]
      else
        #puts 'NOT DOING ACTIVERECORD'
      end

      if defined?(ActionMailer) && ActionMailer::Base.smtp_settings
        c2.mailer = ActionMailer::Base.smtp_settings
      end
      c2.merged_gems.merge!(get_required_gems) if defined?(Bundler)
      SimpleWorker.logger.debug "MODELS " + c2.models.inspect
      SimpleWorker.logger.debug "MAILERS " + c2.mailers.inspect
      SimpleWorker.logger.debug "DATABASE " + c2.database.inspect
      #SimpleWorker.logger.debug "GEMS " + c2.gems.inspect
    end
    end_time = Time.now
    SimpleWorker.logger.info "SimpleWorker initialized. Duration: #{((end_time.to_f-start_time.to_f) * 1000.0).to_i} ms"
  end
end

#get_atts_to_sendObject



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/simple_worker/config.rb', line 160

def get_atts_to_send
  config_data = {}
  config_data['token'] = token
  config_data['project_id'] = project_id
  config_data['database'] = self.database if self.database
  config_data['mailer'] = self.mailer if self.mailer
  config_data['global_attributes'] = self.global_attributes if self.global_attributes
  config_data['scheme'] = self.scheme if self.scheme
  config_data['host'] = self.host if self.host
  config_data['port'] = self.port if self.port
  config_data
end

#get_required_gemsObject



99
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/simple_worker/config.rb', line 99

def get_required_gems
  gems_in_gemfile = Bundler.environment.dependencies.select { |d| d.groups.include?(:default) }
  SimpleWorker.logger.debug 'gems in gemfile=' + gems_in_gemfile.inspect
  gems = {}
  specs = Bundler.load.specs
  SimpleWorker.logger.debug 'Bundler specs=' + specs.inspect
  SimpleWorker.logger.debug "gems_to_skip=" + self.class.gems_to_skip.inspect
  specs.each do |spec|
    SimpleWorker.logger.debug 'spec.name=' + spec.name.inspect
    SimpleWorker.logger.debug 'spec=' + spec.inspect
    if self.class.gems_to_skip.include?(spec.name)
      SimpleWorker.logger.debug "Skipping #{spec.name}"
      next
    end
#        next if dep.name=='rails' #monkey patch
    gem_info = {:name=>spec.name, :version=>spec.version}
    gem_info[:auto_merged] = true
    gem_info[:merge] = true
# Now find dependency in gemfile in case user set the require
    dep = gems_in_gemfile.find { |g| g.name == gem_info[:name] }
    if dep
      SimpleWorker.logger.debug 'dep found in gemfile: ' + dep.inspect
      SimpleWorker.logger.debug 'autorequire=' + dep.autorequire.inspect
      gem_info[:require] = dep.autorequire if dep.autorequire
#        spec = specs.find { |g| g.name==gem_info[:name] }
    end
    gem_info[:version] = spec.version.to_s
    gems[gem_info[:name]] = gem_info
    path = SimpleWorker::Service.get_gem_path(gem_info)
    if path
      gem_info[:path] = path
      if gem_info[:require].nil? && dep
        # see if we should try to require this in our worker
        require_path = gem_info[:path] + "/lib/#{gem_info[:name]}.rb"
        SimpleWorker.logger.debug "require_path=" + require_path
        if File.exists?(require_path)
          SimpleWorker.logger.debug "File exists for require"
          gem_info[:require] = gem_info[:name]
        else
          SimpleWorker.logger.debug "no require"
#              gem_info[:no_require] = true
        end
      end
    else
      SimpleWorker.logger.warn "Could not find '#{gem_info[:name]}' specified in Bundler, continuing anyways."
    end
#        else
#          SimpleWorker.logger.warn "Could not find gem spec for #{gem_info[:name]}"
#          raise "Could not find gem spec for #{gem_info[:name]}"
#        end
  end
  gems
end

#get_server_gemsObject



153
154
155
156
157
158
# File 'lib/simple_worker/config.rb', line 153

def get_server_gems
  return []
  # skipping this now, don't want any server dependencies if possible
  self.server_gems = SimpleWorker.service.get_server_gems unless self.server_gems
  self.server_gems
end

#merge(file) ⇒ Object



173
174
175
176
177
178
179
# File 'lib/simple_worker/config.rb', line 173

def merge(file)
  f2 = SimpleWorker::MergeHelper.check_for_file(file, caller[2])
  fbase = f2[:basename]
  ret = f2
  @merged[fbase] = ret
  ret
end

#merge_gem(gem_name, options = {}) ⇒ Object

Merge a gem globally here



189
190
191
# File 'lib/simple_worker/config.rb', line 189

def merge_gem(gem_name, options={})
  merged_gems[gem_name.to_s] = SimpleWorker::MergeHelper.create_gem_info(gem_name, options)
end

#secret_key=(x) ⇒ Object



48
49
50
# File 'lib/simple_worker/config.rb', line 48

def secret_key=(x)
  raise "SimpleWorker Config Error: access_key and secret_key are no longer used. The new SimpleWorker gem requires a couple of small configuration changes, please see: http://docs.simpleworker.com/ruby/new-gem-v2-update-guide for information."
end

#unmerge(file) ⇒ Object



181
182
183
184
185
186
# File 'lib/simple_worker/config.rb', line 181

def unmerge(file)
  f2 = SimpleWorker::MergeHelper.check_for_file(file, caller[2])
  fbase = f2[:basename]
  @unmerged[fbase] =f2
  @merged.delete(fbase)
end

#unmerge_gem(gem_name) ⇒ Object

Unmerge a global gem



194
195
196
197
198
199
# File 'lib/simple_worker/config.rb', line 194

def unmerge_gem(gem_name)
  gs = gem_name.to_s
  gem_info = {:name=>gs}
  unmerged_gems[gs] = gem_info
  merged_gems.delete(gs)
end