Skip to content

Instantly share code, notes, and snippets.

@yaredc
Created November 25, 2019 20:59
Show Gist options
  • Save yaredc/636adcf6d9d42647d73f454c676f0ce5 to your computer and use it in GitHub Desktop.
Save yaredc/636adcf6d9d42647d73f454c676f0ce5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
declare(strict_types=1);
use Amp\Loop;
use Cli\Sources\SourceFactory;
use Cli\Sources\SourceInterface;
use Common\Console\Application;
use Common\Helper\Logger;
require dirname(__DIR__) . '/vendor/autoload.php';
Config::init(Config::CONTEXT_DAEMON);
$cmd = 'pgrep -f "^php.*daemon$" 2>&1';
exec($cmd, $output, $return);
if ($return > 1 || !is_array($output) || count($output) > 1) {
Logger::log()->warning('Daemon already running.', [$cmd, json_encode($output, JSON_THROW_ON_ERROR), $return]);
exit(0);
}
try {
/** @var Application $cliApp */
$cliApp = require dirname(__DIR__) . '/config/CliApp.php';
$cliApp->setAutoExit(false);
Logger::log()->info('Daemon is starting.');
foreach (Config::getAllSearchConfigurations() as $searchConfiguration) {
foreach (Config::getSources() as $source) {
$sourceClass = SourceFactory::create($source, $searchConfiguration);
Loop::defer(static function (string $watcherId, SourceInterface $sourceClass) {
$sourceClass->run();
}, $sourceClass);
}
}
Loop::run();
} catch (Exception $e) {
Logger::log()->error($e->getMessage());
exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment