Created
August 4, 2021 19:14
-
-
Save gnumoksha/ee935294b227c8f64a30292666af5e97 to your computer and use it in GitHub Desktop.
datadog_daemon.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
/** | |
* This script is a daemon, i.e. it will be executed by | |
* systemd and kept running for a long time. | |
*/ | |
class Processor | |
{ | |
public function __invoke(Message $message) | |
{ | |
print(sprintf("processing the message %s\n", $message->id ?? 'unknown')); | |
sleep(1); | |
} | |
} | |
class Message | |
{ | |
/** @var string */ | |
public $id; | |
/** @var string */ | |
public $body; | |
public function __construct(string $id, string $body) | |
{ | |
$this->id = $id; | |
$this->body = $body; | |
} | |
} | |
function receiveMessages() : array | |
{ | |
return [ | |
new Message('one', 'foo'), | |
new Message('two', 'bar'), | |
new Message('three', 'baz'), | |
]; | |
} | |
function getProcessors() : array | |
{ | |
return [ | |
new Processor(), | |
new Processor(), | |
]; | |
} | |
$processors = getProcessors(); | |
while (true) { | |
$messages = receiveMessages(); | |
/** | |
* What I want: | |
* - each message is a resource | |
* - trace each message processor | |
* - flush traces after each consumed message | |
*/ | |
foreach ($messages as $message) { | |
foreach ($processors as $processor) { | |
$processor($message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment