From fdb31eeb0076ed238b6074c523cf4ad96e5c95e1 Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Fri, 7 Oct 2022 19:17:05 -0400 Subject: [PATCH] Added some basic task runner functionality using beanstalkd and the pheanstalk library --- composer.json | 3 +- composer.lock | 57 +++++++++++++++++++++++++- conf/defaults.json | 7 +++- src/Controllers/HomeController.php | 7 ++++ src/Runners/RefreshServerDownloads.php | 17 ++++++++ src/runner.php | 45 ++++++++++++++++++++ 6 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 src/Runners/RefreshServerDownloads.php create mode 100644 src/runner.php diff --git a/composer.json b/composer.json index ba43b78..efe1ad6 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ "slim/psr7": "^1.5", "slim/twig-view": "^3.3", "hassankhan/config": "^3.0", - "php-di/php-di": "^6.4" + "php-di/php-di": "^6.4", + "pda/pheanstalk": "~4.0" } } diff --git a/composer.lock b/composer.lock index 18af618..5ba1ce4 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e7cbf8cfe22ea0e64510fe92ffbb4c00", + "content-hash": "6ac1bc7f15734931ede8901fed3ed3eb", "packages": [ { "name": "fig/http-message-util", @@ -235,6 +235,61 @@ }, "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", "version": "2.3.3", diff --git a/conf/defaults.json b/conf/defaults.json index af6e59f..ed7a3e1 100644 --- a/conf/defaults.json +++ b/conf/defaults.json @@ -1,3 +1,8 @@ { - "server_directory": "/opt/minecraft" + "server_directory": "/opt/minecraft", + + "beanstalkd": { + "host": "localhost", + "port": 11300 + } } diff --git a/src/Controllers/HomeController.php b/src/Controllers/HomeController.php index 67b22c5..511028a 100644 --- a/src/Controllers/HomeController.php +++ b/src/Controllers/HomeController.php @@ -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, diff --git a/src/Runners/RefreshServerDownloads.php b/src/Runners/RefreshServerDownloads.php new file mode 100644 index 0000000..e99fa8a --- /dev/null +++ b/src/Runners/RefreshServerDownloads.php @@ -0,0 +1,17 @@ +config = $config; + } + + public function start() { + echo "Refreshing server download URLs...\n"; + } + +} diff --git a/src/runner.php b/src/runner.php new file mode 100644 index 0000000..67d3844 --- /dev/null +++ b/src/runner.php @@ -0,0 +1,45 @@ +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); +}