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

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

View File

@ -19,6 +19,7 @@
"slim/psr7": "^1.5", "slim/psr7": "^1.5",
"slim/twig-view": "^3.3", "slim/twig-view": "^3.3",
"hassankhan/config": "^3.0", "hassankhan/config": "^3.0",
"php-di/php-di": "^6.4" "php-di/php-di": "^6.4",
"pda/pheanstalk": "~4.0"
} }
} }

57
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "e7cbf8cfe22ea0e64510fe92ffbb4c00", "content-hash": "6ac1bc7f15734931ede8901fed3ed3eb",
"packages": [ "packages": [
{ {
"name": "fig/http-message-util", "name": "fig/http-message-util",
@ -235,6 +235,61 @@
}, },
"time": "2018-02-13T20:26:39+00:00" "time": "2018-02-13T20:26:39+00:00"
}, },
{
"name": "pda/pheanstalk",
"version": "v4.0.4",
"source": {
"type": "git",
"url": "https://github.com/pheanstalk/pheanstalk.git",
"reference": "1a43eb97a53144a2e692bce2ea2be721cc9913a4"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/pheanstalk/pheanstalk/zipball/1a43eb97a53144a2e692bce2ea2be721cc9913a4",
"reference": "1a43eb97a53144a2e692bce2ea2be721cc9913a4",
"shasum": ""
},
"require": {
"ext-mbstring": "*",
"php": ">=7.1.0"
},
"require-dev": {
"phpunit/phpunit": "^7"
},
"type": "library",
"autoload": {
"psr-4": {
"Pheanstalk\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Paul Annesley",
"email": "paul@annesley.cc",
"homepage": "http://paul.annesley.cc/",
"role": "Developer"
},
{
"name": "Sam Mousa",
"email": "sam@mousa.nl",
"role": "Maintainer"
}
],
"description": "PHP client for beanstalkd queue",
"homepage": "https://github.com/pheanstalk/pheanstalk",
"keywords": [
"beanstalkd"
],
"support": {
"issues": "https://github.com/pheanstalk/pheanstalk/issues",
"source": "https://github.com/pheanstalk/pheanstalk/tree/v4.0.4"
},
"time": "2021-11-19T15:00:20+00:00"
},
{ {
"name": "php-di/invoker", "name": "php-di/invoker",
"version": "2.3.3", "version": "2.3.3",

View File

@ -1,3 +1,8 @@
{ {
"server_directory": "/opt/minecraft" "server_directory": "/opt/minecraft",
"beanstalkd": {
"host": "localhost",
"port": 11300
}
} }

View File

@ -2,6 +2,7 @@
namespace BitGoblin\MCST\Controllers; namespace BitGoblin\MCST\Controllers;
use Pheanstalk\Pheanstalk;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Views\Twig; use Slim\Views\Twig;
@ -21,6 +22,12 @@ class HomeController extends Controller {
array_push($minecraftServers, new Server($m)); 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); $view = Twig::fromRequest($request);
return $view->render($response, 'index.twig', [ return $view->render($response, 'index.twig', [
'servers' => $minecraftServers, '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);
}