-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from leepeuker/add-job-queue-system
Add job proccessing
- Loading branch information
Showing
20 changed files
with
462 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php declare(strict_types=1); | ||
|
||
use Phinx\Migration\AbstractMigration; | ||
|
||
final class AddJobQueueTable extends AbstractMigration | ||
{ | ||
public function down() : void | ||
{ | ||
$this->execute( | ||
<<<SQL | ||
DROP TABLE `job_queue` | ||
SQL | ||
); | ||
} | ||
|
||
public function up() : void | ||
{ | ||
$this->execute( | ||
<<<SQL | ||
CREATE TABLE `job_queue` ( | ||
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, | ||
`job` TEXT NOT NULL, | ||
`created_at` DATETIME NOT NULL DEFAULT NOW(), | ||
PRIMARY KEY (`id`) | ||
) COLLATE="utf8mb4_unicode_ci" ENGINE=InnoDB | ||
SQL | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[program:movary] | ||
command=/usr/local/bin/php /app/bin/console.php jobs:process | ||
numprocs=1 | ||
user=application | ||
autostart=true | ||
autorestart=true | ||
startsecs=1 | ||
startretries=10 | ||
stdout_logfile=/dev/stdout | ||
stdout_logfile_maxbytes=0 | ||
stderr_logfile=/dev/stderr | ||
stderr_logfile_maxbytes=0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Movary\Command; | ||
|
||
use Movary\Worker; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class ProcessJobs extends Command | ||
{ | ||
private const OPTION_NAME_MIN_RUNTIME = 'minRuntime'; | ||
|
||
protected static $defaultName = 'jobs:process'; | ||
|
||
public function __construct( | ||
private readonly Worker\Repository $repository, | ||
private readonly Worker\Service $workerService, | ||
private readonly LoggerInterface $logger, | ||
private readonly ?int $minRuntimeInSeconds = null | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure() : void | ||
{ | ||
$this | ||
->setDescription('Process job from the queue.') | ||
->addOption(self::OPTION_NAME_MIN_RUNTIME, 'minRuntime', InputOption::VALUE_REQUIRED, 'Minimum runtime of command.'); | ||
} | ||
|
||
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter | ||
protected function execute(InputInterface $input, OutputInterface $output) : int | ||
{ | ||
$minRuntime = $input->getOption(self::OPTION_NAME_MIN_RUNTIME) ?? $this->minRuntimeInSeconds; | ||
|
||
$timeStart = microtime(true); | ||
|
||
$this->generateOutput($output, 'Processing job...'); | ||
|
||
try { | ||
$processedJobType = $this->processJob(); | ||
} catch (\Exception $e) { | ||
$this->logger->error('Could not process job.', ['exception' => $e]); | ||
|
||
return Command::FAILURE; | ||
} | ||
|
||
$processedMessage = 'Nothing to process.'; | ||
if ($processedJobType !== null) { | ||
$processedMessage = 'Processed job of type: ' . $processedJobType; | ||
} | ||
|
||
$this->generateOutput($output, $processedMessage); | ||
|
||
$missingTime = (int)$minRuntime - (microtime(true) - $timeStart); | ||
if ($missingTime > 0) { | ||
$waitTime = max((int)ceil($missingTime * 1000000), 0); | ||
|
||
$this->generateOutput($output, 'Sleeping for ' . $waitTime / 1000000 . ' seconds to reach min runtime...'); | ||
|
||
usleep($waitTime); | ||
} | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
private function processJob() : ?string | ||
{ | ||
$job = $this->repository->fetchOldestJob(); | ||
|
||
if ($job === null) { | ||
return null; | ||
} | ||
|
||
$this->workerService->processJob($job); | ||
|
||
/** @noinspection PhpUnreachableStatementInspection */ | ||
return $job->getType(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Movary\HttpController; | ||
|
||
use Movary\Application\User\Service\Authentication; | ||
use Movary\ValueObject\Http\Header; | ||
use Movary\ValueObject\Http\Response; | ||
use Movary\ValueObject\Http\StatusCode; | ||
use Movary\Worker\Service; | ||
|
||
class JobController | ||
{ | ||
public function __construct( | ||
private readonly Authentication $authenticationService, | ||
private readonly Service $workerService | ||
) { | ||
} | ||
|
||
public function scheduleTraktHistorySync() : Response | ||
{ | ||
if ($this->authenticationService->isUserAuthenticated() === false) { | ||
return Response::createFoundRedirect('/'); | ||
} | ||
|
||
$this->workerService->addTraktHistorySyncJob($this->authenticationService->getCurrentUserId()); | ||
|
||
$_SESSION['scheduledTraktHistorySync'] = true; | ||
|
||
return Response::create( | ||
StatusCode::createSeeOther(), | ||
null, | ||
[Header::createLocation($_SERVER['HTTP_REFERER'])] | ||
); | ||
} | ||
|
||
public function scheduleTraktRatingsSync() : Response | ||
{ | ||
if ($this->authenticationService->isUserAuthenticated() === false) { | ||
return Response::createFoundRedirect('/'); | ||
} | ||
|
||
$this->workerService->addTraktRatingsSyncJob($this->authenticationService->getCurrentUserId()); | ||
|
||
$_SESSION['scheduledTraktRatingsSync'] = true; | ||
|
||
return Response::create( | ||
StatusCode::createSeeOther(), | ||
null, | ||
[Header::createLocation($_SERVER['HTTP_REFERER'])] | ||
); | ||
} | ||
} |
Oops, something went wrong.