Skip to content

Commit

Permalink
Add a job to reanalyze more posts based on date
Browse files Browse the repository at this point in the history
  • Loading branch information
RikudouSage committed Feb 16, 2024
1 parent 3639ce6 commit c91d1b7
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/packages/messenger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ framework:
App\Message\AnalyzePostReportMessage: async
App\Message\AnalyzeUserMessage: async
App\Message\BanUserMessage: async
App\Message\ReanalyzePostsMessage: async
App\Message\RemoveCommentMessage: async
App\Message\RemovePostMessage: async
App\Message\RestoreCommentMessage: async
Expand Down
45 changes: 44 additions & 1 deletion src/Command/TriggerJobCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

namespace App\Command;

use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use LogicException;
use ReflectionClass;
use ReflectionNamedType;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -11,6 +17,7 @@
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\TransportNamesStamp;
use TypeError;

#[AsCommand(name: 'app:trigger-job', description: 'Triggers an arbitrary job by providing a class and its arguments')]
final class TriggerJobCommand extends Command
Expand Down Expand Up @@ -55,10 +62,46 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$badges = $input->getOption('sync') ? [new TransportNamesStamp(['sync'])] : [];
$message = new $class(...$args);
$message = $this->createInstance($class, $args);
$this->messageBus->dispatch($message, $badges);

$io->success('Successfully dispatched the job.');
return Command::SUCCESS;
}

/**
* @template T of object
* @param class-string<T> $class
* @param array<string> $args
* @return T
*/
private function createInstance(string $class, array $args): object
{
try {
return new $class(...$args);
} catch (TypeError) {
$reflection = new ReflectionClass($class);
$arguments = $reflection->getConstructor()?->getParameters() ?? throw new LogicException('Not constructor found');
$namedParameters = !array_is_list($args);
$i = 0;
foreach ($arguments as $argument) {
$type = $argument->getType();
if (!$type instanceof ReflectionNamedType) {
throw new LogicException('Can only handle single (or nullable) types for construction');
}
$value = $namedParameters ? $args[$argument->getName()] : $args[$i];

if (is_a($type->getName(), DateTime::class, true)) {
$value = new DateTime($value);
} else if (is_a($type->getName(), DateTimeInterface::class, true)) {
$value = new DateTimeImmutable($value);
}

$namedParameters ? ($args[$argument->getName()] = $value) : ($args[$i] = $value);
++$i;
}

return new $class(...$args);
}
}
}
13 changes: 13 additions & 0 deletions src/Message/ReanalyzePostsMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Message;

use DateTimeInterface;

final readonly class ReanalyzePostsMessage
{
public function __construct(
public DateTimeInterface $since,
) {
}
}
47 changes: 47 additions & 0 deletions src/MessageHandler/ReanalyzePostsHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\MessageHandler;

use App\Message\AnalyzePostMessage;
use App\Message\ReanalyzePostsMessage;
use DateTimeInterface;
use Rikudou\LemmyApi\Enum\ListingType;
use Rikudou\LemmyApi\Enum\SortType;
use Rikudou\LemmyApi\LemmyApi;
use Rikudou\LemmyApi\Response\View\PostView;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\DispatchAfterCurrentBusStamp;

#[AsMessageHandler]
final readonly class ReanalyzePostsHandler
{
public function __construct(
private MessageBusInterface $messageBus,
private LemmyApi $api,
) {
}

public function __invoke(ReanalyzePostsMessage $message): void
{
foreach ($this->getPosts($message->since) as $post) {
$this->messageBus->dispatch(new AnalyzePostMessage($post->post->id), [
new DispatchAfterCurrentBusStamp(),
]);
}
}

/**
* @return iterable<PostView>
*/
private function getPosts(DateTimeInterface $until): iterable
{
$page = 1;
do {
$posts = $this->api->post()->getPosts(page: $page, sort: SortType::New, listingType: ListingType::All);
foreach ($posts as $post) {
yield $post;
}
} while (isset($post) && $post->post->published > $until);
}
}

0 comments on commit c91d1b7

Please sign in to comment.