Module: ModernTimes::Railsable

Included in:
ModernTimes
Defined in:
lib/modern_times/railsable.rb

Instance Method Summary collapse

Instance Method Details

#configObject



90
91
92
# File 'lib/modern_times/railsable.rb', line 90

def config
  @config
end

#create_rails_manager(manager_config = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/modern_times/railsable.rb', line 63

def create_rails_manager(manager_config={})
  # Take advantage of nil and false values for boolean
  raise 'init_rails has not been called, modify your config/environment.rb to include this call' if @is_jms_enabled.nil?
  raise 'Messaging is not enabled, modify your config/jms.yml file' unless @is_jms_enabled
  default_config = {
      :persist_file    => File.join(Rails.root, "log", "modern_times.yml"),
      :worker_file     => File.join(Rails.root, "config", "workers.yml"),
      :jmx             => @env != 'test',
      :stop_on_signal  => true,
      :dummy_host      => @env,
      :allowed_workers => rails_workers,
  }

  return ModernTimes::Manager.new(default_config.merge(manager_config))
end

#init_railsObject



6
7
8
9
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/modern_times/railsable.rb', line 6

def init_rails
  # Allow user to use JMS w/o modifying jms.yml which could be checked in and hose other users
  @env = ENV['MODERN_TIMES_ENV'] || Rails.env
  if @config = YAML.load(ERB.new(File.read(File.join(Rails.root, "config", "jms.yml"))).result(binding))[@env]
    ModernTimes.logger.info "Messaging Enabled"
    ModernTimes::JMS::Connection.init(@config)
    @is_jms_enabled = true

    # Need to start the JMS Server in this VM
    # TODO: Still want to support this?
    if false
    #if ModernTimes::JMS::Connection.invm?
      @server = ::JMS::Server.create_server('vm://127.0.0.1')
      @server.start

      # Handle messages within this process
      @manager = ModernTimes::Manager.new
      # TODO: Formatting of configured workers in invm state with name and options
      if worker_cfg = @config[:workers]
        worker_cfg.each do |klass, count|
          @manager.add(klass, count, {})
        end
      else
        rails_workers.each do |klass|
          @manager.add(klass, 1, {})
        end
      end

      at_exit do
        @manager.stop if @manager
        @server.stop
      end
    end

  else
    Rails.logger.info "Messaging disabled"
    @is_jms_enabled = false
    worker_file     = File.join(Rails.root, "config", "workers.yml")
    worker_pools = []
    ModernTimes::Manager.parse_worker_file(worker_file, @env) do |klass, count, options|
      # Create a pool for each worker so a single instance won't have to be thread safe when multiple http request hit it concurrently.
      worker_pools << GenePool.new(:pool_size => count, :logger => Rails.logger) do
        klass.new(options)
      end
    end
    # If no config, then just create a worker_pool for each class in the app/workers directory
    if worker_pools.empty?
      worker_pools = rails_workers.map do |klass|
        GenePool.new(:pool_size => 1, :logger => Rails.logger) do
          klass.new({})
        end
      end
    end
    ModernTimes::JMS::Publisher.setup_dummy_publishing(worker_pools)
  end
end

#jms_enabled?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/modern_times/railsable.rb', line 94

def jms_enabled?
  @is_jms_enabled
end

#rails_workersObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/modern_times/railsable.rb', line 79

def rails_workers
  @rails_workers ||= begin
    workers = []
    Dir["#{Rails.root}/app/workers/*_worker.rb"].each do |file|
      require file
      workers << File.basename(file).sub(/\.rb$/, '').classify.constantize
    end
    workers
  end
end