Class: Daemon::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/onapp/engine/runner.rb,
lib/onapp/engine/runner/fork.rb,
lib/onapp/engine/runner/status.rb,
lib/onapp/engine/runner/reactor.rb,
lib/onapp/engine/runner/pid_file.rb,
lib/onapp/engine/runner/stop_service.rb,
lib/onapp/engine/runner/prepare_daemon.rb,
lib/onapp/engine/runner/errors_handling.rb

Overview

Encapsulates worker process

Defined Under Namespace

Modules: Status Classes: ErrorsHandling, Fork, PidFile, PrepareDaemon, Reactor, StopService

Constant Summary

APP =

rack-like application

->(env, forked) do
  builder = Middleware::Builder.new do
    use Daemon::Runner::PidFile
    use Daemon::Runner::PrepareDaemon
    use Daemon::Runner::ErrorsHandling
    use Daemon::Runner::Reactor
  end

  if forked
    builder.insert 0, Daemon::Runner::Fork
  end

  builder.call(env)
end

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Runner) initialize(id = Daemon.another_instances_pids.count, stdout = Daemon.log_file, stderr = Daemon.error_log_file)

Returns a new instance of Runner

Parameters:

  • id (Integer) (defaults to: Daemon.another_instances_pids.count)

    Used in pid_file, unix_socket paths generation. And runner distinction

  • stdout (String) (defaults to: Daemon.log_file)

    Path for reopening stdout stream

  • stderr (String) (defaults to: Daemon.error_log_file)

    Path for reopening stderr stream



57
58
59
60
61
62
63
# File 'lib/onapp/engine/runner.rb', line 57

def initialize(id = Daemon.another_instances_pids.count, stdout = Daemon.log_file, stderr = Daemon.error_log_file)
  @id = id
  @groups = []
  @stdout = stdout
  @stderr = stderr
  extend(MonitorMixin)
end

Instance Attribute Details

- (Object) error (readonly)

Returns the value of attribute error



37
38
39
# File 'lib/onapp/engine/runner.rb', line 37

def error
  @error
end

- (Object) groups (readonly)

Returns the value of attribute groups



37
38
39
# File 'lib/onapp/engine/runner.rb', line 37

def groups
  @groups
end

- (Object) id (readonly)

Returns the value of attribute id



37
38
39
# File 'lib/onapp/engine/runner.rb', line 37

def id
  @id
end

- (Object) pid (readonly)

Returns the value of attribute pid



37
38
39
# File 'lib/onapp/engine/runner.rb', line 37

def pid
  @pid
end

- (Object) stderr (readonly)

Returns the value of attribute stderr



42
43
44
# File 'lib/onapp/engine/runner.rb', line 42

def stderr
  @stderr
end

- (Object) stdout (readonly)

Returns the value of attribute stdout



42
43
44
# File 'lib/onapp/engine/runner.rb', line 42

def stdout
  @stdout
end

Class Method Details

+ (Object) status



49
50
51
# File 'lib/onapp/engine/runner.rb', line 49

def status
  workers.map(&:status)
end

+ (Object) workers



45
46
47
# File 'lib/onapp/engine/runner.rb', line 45

def workers
  @workers ||= Daemon::AllocateWorkers.allocate(Daemon.config.amount_of_service_instances)
end

Instance Method Details

- (Boolean) alive?

If this instance is running in fork, then return value should be true

Returns:

  • (Boolean)

    Status of forked process



76
77
78
# File 'lib/onapp/engine/runner.rb', line 76

def alive?
  @detach_thread && @detach_thread.alive?
end

- (Object) process_name

This is written to $0 (process name in “ps aux”)



88
89
90
# File 'lib/onapp/engine/runner.rb', line 88

def process_name
  "#{Daemon::PROCESS_NAME} groups: #{groups.join(', ')}"
end

- (Object) run(forked = false)

Runs daemon in fork or in current process If forked, than it sets pid, and possibly error



67
68
69
70
71
# File 'lib/onapp/engine/runner.rb', line 67

def run(forked = false)
  synchronize do
    (@pid, @detach_thread = APP.call(self, forked)) unless alive?
  end
end

- (Object) status

Used for writing all runners statuses to redis in Daemon::Runner::Status

See Also:



83
84
85
# File 'lib/onapp/engine/runner.rb', line 83

def status
  {alive: alive?, groups: groups, id: id, pid: pid}
end

- (Daemon::Runner::StopService) stop_service

Returns Service for stopping current process

Returns:

See Also:



95
96
97
# File 'lib/onapp/engine/runner.rb', line 95

def stop_service
  @stop_service ||= StopService.new(Daemon.logger)
end