Added some basic task runner functionality using beanstalkd and the pheanstalk library

This commit is contained in:
2022-10-07 19:17:05 -04:00
parent bb584d5ea0
commit fdb31eeb00
6 changed files with 133 additions and 3 deletions

View File

@ -2,6 +2,7 @@
namespace BitGoblin\MCST\Controllers;
use Pheanstalk\Pheanstalk;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Views\Twig;
@ -21,6 +22,12 @@ class HomeController extends Controller {
array_push($minecraftServers, new Server($m));
}
// create pheanstalk object and create a job
$pheanstalk = Pheanstalk::create($config->get('beanstalkd.host'), $config->get('beanstalkd.port'));
$pheanstalk
->useTube('mcst')
->put("refresh-server-downloads", Pheanstalk::DEFAULT_PRIORITY);
$view = Twig::fromRequest($request);
return $view->render($response, 'index.twig', [
'servers' => $minecraftServers,

View File

@ -0,0 +1,17 @@
<?php
namespace BitGoblin\MCST\Runners;
class RefreshServerDownloads {
protected $config;
public function __construct($config) {
$this->config = $config;
}
public function start() {
echo "Refreshing server download URLs...\n";
}
}

45
src/runner.php Normal file
View File

@ -0,0 +1,45 @@
<?php
use Noodlehaus\Config;
use Noodlehaus\Parser\Json;
use Pheanstalk\Pheanstalk;
use BitGoblin\MCST\Runners\RefreshServerDownloads;
require __DIR__ . '/../vendor/autoload.php';
// Load app configuration
$config = Config::load(__DIR__ . '/../conf/defaults.json');
// create pheanstalk object
$pheanstalk = Pheanstalk::create($config->get('beanstalkd.host'), $config->get('beanstalkd.port'));
// we want jobs from 'mcst' tube only.
$pheanstalk->watch('mcst');
while (true) {
echo "Waiting for a new job...\n";
// try to reserve a job
$job = $pheanstalk->reserve();
// check if we actually got a job before trying to do work
if (isset($job)) {
$jobPayload = $job->getData();
switch ($jobPayload) {
case 'refresh-server-downloads':
$rsd = new RefreshServerDownloads($config);
$rsd->start();
break;
default:
echo "Runner command " . $jobPayload . " isn't supported.\n";
break;
}
// remove job
$pheanstalk->delete($job);
}
sleep(60);
}