Class: Dynflow::Rails::Daemon

Inherits:
Object
  • Object
show all
Defined in:
lib/dynflow/rails/daemon.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dynflow_memory_watcher_class = ::Dynflow::Watchers::MemoryConsumptionWatcher, non_default_daemons_class = nil) ⇒ Daemon

make Daemon dependency injection ready for testing purposes



14
15
16
17
18
19
20
# File 'lib/dynflow/rails/daemon.rb', line 14

def initialize(
  dynflow_memory_watcher_class = ::Dynflow::Watchers::MemoryConsumptionWatcher,
  non_default_daemons_class = nil
)
  @dynflow_memory_watcher_class = dynflow_memory_watcher_class
  @daemons_class = non_default_daemons_class
end

Instance Attribute Details

#daemons_classObject (readonly)

Returns the value of attribute daemons_class.



11
12
13
# File 'lib/dynflow/rails/daemon.rb', line 11

def daemons_class
  @daemons_class
end

#dynflow_memory_watcher_classObject (readonly)

Returns the value of attribute dynflow_memory_watcher_class.



11
12
13
# File 'lib/dynflow/rails/daemon.rb', line 11

def dynflow_memory_watcher_class
  @dynflow_memory_watcher_class
end

Instance Method Details

#run(rails_root = Dir.pwd, options = {}) ⇒ Object

Load the Rails environment and initialize the executor in this thread.



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
62
63
64
# File 'lib/dynflow/rails/daemon.rb', line 35

def run(rails_root = Dir.pwd, options = {})
  stdout.puts('Starting Rails environment')
  rails_env_file = File.expand_path('./config/environment.rb', rails_root)
  unless File.exist?(rails_env_file)
    raise "#{rails_root} doesn't seem to be a Rails root directory"
  end

  stderr.puts("Starting dynflow with the following options: #{options}")

  ::Rails.application.dynflow.executor!

  if options[:memory_limit] && options[:memory_limit].to_i > 0
    ::Rails.application.dynflow.config.on_init do |world|
      stdout_cap = stdout
      memory_watcher = initialize_memory_watcher(world, options[:memory_limit], options)
      world.terminated.on_resolution do
        stdout_cap.puts("World has been terminated")
        memory_watcher = nil # the object can be disposed
      end
    end
  end

  require rails_env_file
  ::Rails.application.dynflow.initialize!
  world_id = ::Rails.application.dynflow.world.id
  stdout.puts("Everything ready for world: #{world_id}")
  sleep
ensure
  stdout.puts('Exiting')
end

#run_background(command = 'start', options = {}) ⇒ Object

run the executor as a daemon



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
97
98
# File 'lib/dynflow/rails/daemon.rb', line 67

def run_background(command = 'start', options = {})
  options = default_options.merge(options)
  FileUtils.mkdir_p(options[:pid_dir])
  begin
    require 'daemons'
  rescue LoadError
    raise "You need to add gem 'daemons' to your Gemfile if you wish to use it."
  end

  unless %w(start stop restart run).include?(command)
    raise "Command exptected to be 'start', 'stop', 'restart', 'run', was #{command.inspect}"
  end

  stdout.puts("Dynflow Executor: #{command} in progress")

  options[:executors_count].times do
    daemons_class.run_proc(
      options[:process_name],
      daemons_options(command, options)
    ) do |*_args|
      begin
        ::Logging.reopen
        run(options[:rails_root], options)
      rescue => e
        stderr.puts e.message
        ::Rails.logger.fatal('Failed running Dynflow daemon')
        ::Rails.logger.fatal(e)
        exit 1
      end
    end
  end
end

#stderrObject



30
31
32
# File 'lib/dynflow/rails/daemon.rb', line 30

def stderr
  STDERR
end

#stdoutObject



26
27
28
# File 'lib/dynflow/rails/daemon.rb', line 26

def stdout
  STDOUT
end