Skip to content

Commit

Permalink
Updates.
Browse files Browse the repository at this point in the history
more typehints.
Replaced some Exception catches with Throwable.
Removed STDERR constant from Daemon.php.
  • Loading branch information
lifo101 committed Oct 13, 2022
1 parent 70560f0 commit 63cfbae
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 26 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ While this library does everything it can to allow you to create a rock solid da
user-land code to keep things stable.

### Requirements
- PHP 5.4.4+
- PHP 7.4+
- A POSIX compatible operating system (Linux, OSX, BSD)
- PHP [POSIX](http://php.net/posix) and [PCNTL](http://php.net/pcntl) Extensions

Expand Down Expand Up @@ -78,5 +78,5 @@ purposes. I also didn't agree with some of his methodologies. I do require some
[Composer](http://getcomposer.org/) makes this a trivial issue.

---
_This library is in a fully working state. I've created very complex daemons that have run for weeks w/o any memory
leaks or crashes. But this is still a **Work in Progress**!_
_This library is in a fully working state. I've created very complex daemons that have run for months w/o any memory
leaks or crashes. More could be done...
9 changes: 5 additions & 4 deletions src/Daemon.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ abstract class Daemon
*
* @var callable|resource|string|null
*/
private $output = STDERR;
private $output = null;

/**
* Main event loop frequency in fractional seconds.
Expand Down Expand Up @@ -293,6 +293,7 @@ protected function __construct()
$this->event = new DaemonEvent();
$this->pid = getmypid() ?: null; // don't want to call event handler with setPid()
$this->parentPid = $this->pid;
$this->setOutput(null);
}

public function isDumpOnSignal(): bool
Expand Down Expand Up @@ -2101,15 +2102,15 @@ public function isAnsi(): bool
/**
* Sets the output destination for console output. Setting to null will default back to STDERR.
*
* @param callable|resource|string $output A callable, an opened stream or an output path
* @param callable|resource|string|null $output A callable, an opened stream or an output path
*
* @return $this
*/
public function setOutput($output): self
{
switch (true) {
case empty($output):
$this->output = STDERR;
case $output === null:
$this->output = fopen('php://stderr', 'w');
break;
case is_callable($output):
case get_resource_type($output) == 'stream':
Expand Down
25 changes: 6 additions & 19 deletions src/Mediator/Mediator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Lifo\Daemon\Worker\WorkerInterface;
use ReflectionClass;
use ReflectionMethod;
use Throwable;

/**
*
Expand Down Expand Up @@ -135,10 +136,9 @@ class Mediator implements TaskInterface
/**
* Was the object setup?
*
* @internal
* @var bool
*/
private $initialized;
private bool $initialized = false;

/**
* The subject being mediated.
Expand Down Expand Up @@ -166,53 +166,41 @@ class Mediator implements TaskInterface
* process will be forked on an as-needed basis.
*
* Auto restarts will not occur if the subject's callback takes a long time.
*
* @var bool
*/
private bool $autoRestart = false;

/**
* Maximum calls this instance will process before exiting. {@link $autoRestart} must be true.
* A small amount of entropy is added to reduce all children restarting at the same time.
*
* @var int
*/
private int $maxCalls = 0;

/**
* Maximum seconds this instance will run before exiting. {@link $autoRestart} must be true.
* A small amount of entropy is added to reduce all children restarting at the same time.
*
* @var int
*/
private int $maxRuntime = 0;

/**
* Minimum seconds this instance will run before exiting. {@link $autoRestart} must be true.
*
* @var int
*/
private int $minRuntime = 0;

/**
* Maximum worker processes allowed
* A small amount of entropy is added to reduce all children restarting at the same time.
*
* @var int
*/
private int $maxProcesses = 1;

/**
* Allow the Mediator to wakeup the daemon?
*
* @var bool
*/
private bool $allowWakeup = false;

/**
* Event listeners
*
* @var array
* @var callable[]|array
*/
private array $events = [];

Expand All @@ -238,21 +226,20 @@ class Mediator implements TaskInterface
/**
* A list of recent calls made for stats purposes
*
* @var array[]
* @var array[][]
*/
private array $recent = [];

/**
* How many calls have been made
*
* @var int
*/
private int $callCount = 0;

/**
* @var int[][]
*/
private array $errorCounts = [];

/**
* @var int[]
*/
Expand Down Expand Up @@ -1178,7 +1165,7 @@ public function run(): void
$result = null;
try {
$result = $this->callSubject($call);
} catch (Exception $e) {
} catch (Throwable $e) {
$this->error("callSubject Exception: " . $e->getMessage());
$call->incErrors();
}
Expand Down

0 comments on commit 63cfbae

Please sign in to comment.