Module: Daemon

Extended by:
Daemon, Forwardable
Included in:
Daemon
Defined in:
lib/onapp/engine/daemon.rb,
lib/onapp/engine/runner.rb,
lib/onapp/engine/logger.rb,
lib/onapp/engine/manager.rb,
lib/onapp/engine/command.rb,
lib/onapp/engine/utils/id.rb,
lib/onapp/engine/utils/em.rb,
lib/onapp/engine/messaging.rb,
lib/onapp/engine/callbacks.rb,
lib/onapp/engine/coordinator.rb,
lib/onapp/engine/supervision.rb,
lib/onapp/engine/initializer.rb,
lib/onapp/engine/utils/retry.rb,
lib/onapp/engine/runner/fork.rb,
lib/onapp/engine/utils/signal.rb,
lib/onapp/engine/utils/process.rb,
lib/onapp/engine/configuration.rb,
lib/onapp/engine/runner/status.rb,
lib/onapp/engine/utils/loggable.rb,
lib/onapp/engine/master_process.rb,
lib/onapp/engine/runner/reactor.rb,
lib/onapp/engine/command/options.rb,
lib/onapp/engine/runner/pid_file.rb,
lib/onapp/engine/utils/evaluator.rb,
lib/onapp/engine/commands_server.rb,
lib/onapp/engine/allocate_workers.rb,
lib/onapp/engine/messaging/message.rb,
lib/onapp/engine/messaging/mailbox.rb,
lib/onapp/engine/utils/shared_value.rb,
lib/onapp/engine/utils/io_timestamp.rb,
lib/onapp/engine/supervision/timers.rb,
lib/onapp/engine/utils/redis_watcher.rb,
lib/onapp/engine/management/activity.rb,
lib/onapp/engine/management_commands.rb,
lib/onapp/engine/runner/stop_service.rb,
lib/onapp/engine/utils/process_cache.rb,
lib/onapp/engine/command/base_command.rb,
lib/onapp/engine/supervision/registry.rb,
lib/onapp/engine/command/stop_command.rb,
lib/onapp/engine/runner/prepare_daemon.rb,
lib/onapp/engine/utils/commands_server.rb,
lib/onapp/engine/command/start_command.rb,
lib/onapp/engine/master_process/message.rb,
lib/onapp/engine/runner/errors_handling.rb,
lib/onapp/engine/command/status_command.rb,
lib/onapp/engine/supervision/definition.rb,
lib/onapp/engine/command/user_interface.rb,
lib/onapp/engine/utils/auto_deleted_hash.rb,
lib/onapp/engine/monitoring/file_watcher.rb,
lib/onapp/engine/utils/per_thread_registry.rb,
lib/onapp/engine/supervision/messages_manager.rb,
lib/onapp/engine/master_process/daemon_socket.rb,
lib/onapp/engine/messaging/connection_manager.rb,
lib/onapp/engine/supervision/process_supervisor.rb,
lib/onapp/engine/supervision/definition/handler.rb,
lib/onapp/engine/master_process/middleware/fork.rb,
lib/onapp/engine/utils/commands_server/commands.rb,
lib/onapp/engine/master_process/middleware/check.rb,
lib/onapp/engine/master_process/middleware/listen.rb,
lib/onapp/engine/master_process/middleware/prepare.rb,
lib/onapp/engine/master_process/middleware/pid_file.rb,
lib/onapp/engine/master_process/middleware/setup_tasks.rb,
lib/onapp/engine/master_process/middleware/environment.rb,
lib/onapp/engine/master_process/middleware/stop_service.rb,
lib/onapp/engine/utils/commands_server/unix_server_client.rb

Overview

Entry point to daemon

Defined Under Namespace

Modules: AllocateWorkers, CommandsServer, Management, Messaging, Monitoring, Utils Classes: Callbacks, Command, Configuration, Coordinator, Initializer, Logger, ManagementCommands, Manager, MasterProcess, Runner, Supervision

Constant Summary

PROCESS_NAME =

Default process name for worker processes

'onappd'.freeze
CLUSTER =

This should be in ENV variable before starting daemon for “HA” functionality in interface project

'DAEMON'.freeze
TASK_MAX_PRIORITY =

Convenience constant

Daemon::Supervision::MessagesManager::TASK_MAX_PRIORITY

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Object) id

worker id

Used as suffix, which will be appended to PROCESS_NAME for naming unix socket and pid files



55
56
57
# File 'lib/onapp/engine/daemon.rb', line 55

def id
  @id
end

- (Object) server

Management server



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

def server
  @server
end

Instance Method Details

- (Array<Fixnum>) another_instances_pids

Lookup for all pids with process name, and looks like onapp engine worker

Returns:

  • (Array<Fixnum>)


186
187
188
# File 'lib/onapp/engine/daemon.rb', line 186

def another_instances_pids
  Daemon::Utils::Process.find('ruby', PROCESS_NAME)
end

- (Daemon::Callbacks) callbacks

Callbacks object

Returns:

See Also:



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/onapp/engine/daemon.rb', line 222

def callbacks
  @callbacks ||=
    Daemon::Callbacks.new(:start_command,
                          :stop_command,
                          :reload_command,
                          :initialize_activity,
                          :stop_activity,
                          :cancel_activity,
                          :onapp_engine_up,
                          :onapp_engine_down,
                          :workers_stopped,
                          :workers_started,
                          :before_fork,
                          :after_fork)
end

- (Daemon::Configuration) config

Daemon configuration

Returns:

See Also:



195
196
197
# File 'lib/onapp/engine/daemon.rb', line 195

def config
  @config ||= Configuration.new
end

- (Object) configure {|Daemon::Configuration| ... }

Convenience method

Yields:



202
203
204
# File 'lib/onapp/engine/daemon.rb', line 202

def configure
  yield config
end

- (Object) delete_pid

TODO:

Should be inlined and removed

Convevience method, deletes pid file



109
110
111
# File 'lib/onapp/engine/daemon.rb', line 109

def delete_pid
  File.delete(pid_file) if File.file?(pid_file)
end

- (String) error_log_file

Returns errors-logfile path

Returns:

  • (String)


130
131
132
# File 'lib/onapp/engine/daemon.rb', line 130

def error_log_file
  root.join('log/onapp.err').to_s
end

- (RFlags::FileFlag) force_stop

TODO:

Seems that it's not used anymore!

This file is used for “force-stop” detection

Returns:

  • (RFlags::FileFlag)

    Should respond to #set, #reset and #set?



67
68
69
# File 'lib/onapp/engine/daemon.rb', line 67

def force_stop
  RFlags::FileFlag.new(root.join('tmp/onapp_daemon.force-stop').to_s)
end

- (Pathname) log_dir

Returns directory for logs

Returns:

  • (Pathname)


116
117
118
# File 'lib/onapp/engine/daemon.rb', line 116

def log_dir
  root.join('log')
end

- (String) log_file

Returns logfile path

Returns:

  • (String)


123
124
125
# File 'lib/onapp/engine/daemon.rb', line 123

def log_file
  log_dir.join('onapp_daemon.log').to_s
end

- (Daemon::ManagementCommands) management_commands

Returns server singleton, which listens to RabbitMQ and invokes some callbacks on Daemon.callbacks



243
244
245
246
# File 'lib/onapp/engine/daemon.rb', line 243

def management_commands
  @management_commands ||=
    Daemon::ManagementCommands.new
end

- (Pathname) pid_dir

Directory with pid-files

Returns:

  • (Pathname)


74
75
76
# File 'lib/onapp/engine/daemon.rb', line 74

def pid_dir
  root.join('tmp/pids')
end

- (String) pid_file

Pid file of current worker process

Returns:

  • (String)


81
82
83
# File 'lib/onapp/engine/daemon.rb', line 81

def pid_file
  pid_dir.join("#{PROCESS_NAME}#{id}.pid").to_s
end

- (Array<String>) pid_files

Seaches for all worker pid files, and returns only paths that exist

Returns:

  • (Array<String>)


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

def pid_files
  Dir[pid_dir.join(PROCESS_NAME).to_s + '*.pid']
end

- (Array<Fixnum>) pids

Pids of each worker's pidfile

Returns:

  • (Array<Fixnum>)


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

def pids
  pid_files.map { |f| File.read(f).to_i if File.file?(f) }
end

- (::Redis) redis

Convenience method

Returns redis connection

Returns:

  • (::Redis)

See Also:



213
214
215
# File 'lib/onapp/engine/daemon.rb', line 213

def redis
  config.redis
end

- (Object) root

Prefix for pid_dir, log_dir and other files



58
59
60
# File 'lib/onapp/engine/daemon.rb', line 58

def root
  config.root_path
end

- (Object) stop!

Makes

kill -TERM <onappd pid>

to all worker processes and deletes all worker pidfiles



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/onapp/engine/daemon.rb', line 138

def stop!
  logger.debug "#{self}.stop!"
  another_instances_pids.each do |pid|
    begin
      logger.debug("kill -15 #{pid}")
      Process.kill(15, pid)
    rescue => ex
      logger.log_exception(ex)
    end
  end

  pid_files.each do |file|
    begin
      logger.debug "Deleting #{file.inspect}"
      File.delete(file)
    rescue => ex
      logger.log_exception(ex)
    end
  end
end

- (Daemon::Supervision) supervision

Returns supervision singleton

Returns:

See Also:



253
254
255
256
# File 'lib/onapp/engine/daemon.rb', line 253

def supervision
  @supervision ||=
    Daemon::Supervision.new
end

- (String) unix_socket

Unix-socket path

Returns:

  • (String)

See Also:



172
173
174
# File 'lib/onapp/engine/daemon.rb', line 172

def unix_socket
  unix_socket_dir.join("#{PROCESS_NAME}#{id}.socket").to_s
end

- (Pathname) unix_socket_dir

Directory for unix socket

Returns:

  • (Pathname)

See Also:



163
164
165
# File 'lib/onapp/engine/daemon.rb', line 163

def unix_socket_dir
  root.join('tmp')
end

- (Array<String>) unix_sockets

Finds all unix-sockets, which look like worker socket

Returns:

  • (Array<String>)


179
180
181
# File 'lib/onapp/engine/daemon.rb', line 179

def unix_sockets
  Dir[unix_socket_dir.join("#{PROCESS_NAME}*.socket").to_s]
end

- (Object) write_pid

TODO:

Should be inlined and removed

Convevience method, writes pid to pidfile



102
103
104
# File 'lib/onapp/engine/daemon.rb', line 102

def write_pid
  File.write(pid_file, Process.pid.to_s)
end