Skip to content

Instantly share code, notes, and snippets.

@gnumoksha
Created August 4, 2021 19:14
Show Gist options
  • Save gnumoksha/ee935294b227c8f64a30292666af5e97 to your computer and use it in GitHub Desktop.
Save gnumoksha/ee935294b227c8f64a30292666af5e97 to your computer and use it in GitHub Desktop.
datadog_daemon.php
<?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