Class: Dynflow::Rails::Daemon
- Inherits:
-
Object
- Object
- Dynflow::Rails::Daemon
- Defined in:
- lib/dynflow/rails/daemon.rb
Instance Attribute Summary collapse
-
#daemons_class ⇒ Object
readonly
Returns the value of attribute daemons_class.
-
#dynflow_memory_watcher_class ⇒ Object
readonly
Returns the value of attribute dynflow_memory_watcher_class.
Instance Method Summary collapse
-
#initialize(dynflow_memory_watcher_class = ::Dynflow::Watchers::MemoryConsumptionWatcher, non_default_daemons_class = nil) ⇒ Daemon
constructor
make Daemon dependency injection ready for testing purposes.
-
#run(rails_root = Dir.pwd, options = {}) ⇒ Object
Load the Rails environment and initialize the executor in this thread.
-
#run_background(command = 'start', options = {}) ⇒ Object
run the executor as a daemon.
- #stderr ⇒ Object
- #stdout ⇒ Object
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_class ⇒ Object (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_class ⇒ Object (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, = {}) stdout.puts('Starting Rails environment') rails_env_file = File.('./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: #{}") ::Rails.application.dynflow.executor! if [:memory_limit] && [:memory_limit].to_i > 0 ::Rails.application.dynflow.config.on_init do |world| stdout_cap = stdout memory_watcher = initialize_memory_watcher(world, [:memory_limit], ) 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', = {}) = .merge() FileUtils.mkdir_p([: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") [:executors_count].times do daemons_class.run_proc( [:process_name], (command, ) ) do |*_args| begin ::Logging.reopen run([:rails_root], ) rescue => e stderr.puts e. ::Rails.logger.fatal('Failed running Dynflow daemon') ::Rails.logger.fatal(e) exit 1 end end end end |
#stderr ⇒ Object
30 31 32 |
# File 'lib/dynflow/rails/daemon.rb', line 30 def stderr STDERR end |
#stdout ⇒ Object
26 27 28 |
# File 'lib/dynflow/rails/daemon.rb', line 26 def stdout STDOUT end |