SpawningDaemon.php

Summary
SpawningDaemon.php
Spawning DaemonBase class for daemon that can launch one or more processing threads, respawning them if they exit.
SpawningDaemonBase class for daemon that can launch one or more processing threads, respawning them if they exit.
__constructClass constructor
runThreadPerform some actual work!
runSpawn one or more background processes and let them start running.
initPipesCreate an IPC socket pair which child processes can use to detect if the parent process has been killed.
processManagerBuild an IOManager that simply ensures that we have a connection to the parent process open.
initAndRunChildInitialize things for a fresh thread, call runThread(), and exit at completion with appropriate return value.
resetDbReconnect to the database for each child process, or they’ll get very confused trying to use the same socket.
log
name

Spawning Daemon

Base class for daemon that can launch one or more processing threads, respawning them if they exit.

postActiv

the micro-blogging software

Copyright

Copyright © 2016-2017, Maiyannah Bishop

Derived from code copyright various sources

  • GNU Social © 2013-2016, Free Software Foundation, Inc
  • StatusNet © 2008-2012, StatusNet, Inc

License

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program.  If not, see http://www.gnu.org/licenses/.

https://www.gnu.org/licenses/agpl.html

About

Base class for daemon that can launch one or more processing threads, respawning them if they exit.

This is mainly intended for indefinite workloads such as monitoring a queue or maintaining an IM channel.

Child classes should implement the

We can then pass individual items through the QueueHandler subclasses they belong to.  We additionally can handle queues for multiple sites.

PHP version

Tested with PHP 7

File Authors

Web

SpawningDaemon

Base class for daemon that can launch one or more processing threads, respawning them if they exit.

This is mainly intended for indefinite workloads such as monitoring a queue or maintaining an IM channel.

Child classes should implement the

We can then pass individual items through the QueueHandler subclasses they belong to.  We additionally can handle queues for multiple sites.

Summary
__constructClass constructor
runThreadPerform some actual work!
runSpawn one or more background processes and let them start running.
initPipesCreate an IPC socket pair which child processes can use to detect if the parent process has been killed.
processManagerBuild an IOManager that simply ensures that we have a connection to the parent process open.
initAndRunChildInitialize things for a fresh thread, call runThread(), and exit at completion with appropriate return value.
resetDbReconnect to the database for each child process, or they’ll get very confused trying to use the same socket.
log
name

__construct

function __construct($id = null,
$daemonize = true,
$threads = 1)

Class constructor

Parameters

  • int $id
  • boolean $daemonize (default true)
  • int $threads

runThread

public abstract function runThread()

Perform some actual work!

Returns

  • int exit code; use self::EXIT_SHUTDOWN to request not to respawn.

run

function run()

Spawn one or more background processes and let them start running.  Each individual process will execute whatever’s in the runThread() method, which should be overridden.

Child processes will be automatically respawned when they exit.

Todo

possibly allow for not respawning on “normal” exits... though ParallelizingDaemon is probably better for workloads that have forseeable endpoints.

initPipes

function initPipes()

Create an IPC socket pair which child processes can use to detect if the parent process has been killed.

processManager

public function processManager()

Build an IOManager that simply ensures that we have a connection to the parent process open.  If it breaks, the child process will die.

Returns

  • the created ProcessManager object

initAndRunChild

protected function initAndRunChild($thread)

Initialize things for a fresh thread, call runThread(), and exit at completion with appropriate return value.

Parameters

  • $thread

Returns

  • int $exitCode

resetDb

protected function resetDb()

Reconnect to the database for each child process, or they’ll get very confused trying to use the same socket.

log

function log($level,
$msg)

name

function name()
function __construct($id = null,
$daemonize = true,
$threads = 1)
Class constructor
public abstract function runThread()
Perform some actual work!
function run()
Spawn one or more background processes and let them start running.
function initPipes()
Create an IPC socket pair which child processes can use to detect if the parent process has been killed.
public function processManager()
Build an IOManager that simply ensures that we have a connection to the parent process open.
protected function initAndRunChild($thread)
Initialize things for a fresh thread, call runThread(), and exit at completion with appropriate return value.
protected function resetDb()
Reconnect to the database for each child process, or they’ll get very confused trying to use the same socket.
function log($level,
$msg)
function name()