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.



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

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|
      memory_watcher = initialize_memory_watcher(world, options[:memory_limit], options)
      world.terminated.on_resolution do
        STDOUT.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



58
59
60
61
62
63
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
# File 'lib/dynflow/rails/daemon.rb', line 58

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