Skip to content

Commit

Permalink
Adding the option to execute task:run by class name (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrshaharb committed Aug 5, 2022
1 parent d2e9b42 commit d971436
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
18 changes: 16 additions & 2 deletions Command/RunCommand.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright (c) 2017-present, Evosphere.
* All rights reserved.
Expand All @@ -10,6 +11,7 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class RunCommand extends Command
Expand All @@ -31,15 +33,27 @@ protected function configure(): void
->setName("ts:run")
->setDescription("Run due tasks")
->setHelp("This command actually run the tasks that are due at the moment the command is called.\nThis command should not be called manually. Check the documentation to learn how to set CRON jobs.")
->addArgument("id", InputArgument::OPTIONAL, "The ID of the task. Check ts:list for IDs");
->addArgument("id", InputArgument::OPTIONAL, "The ID of the task. Check ts:list for IDs")
->addOption("class", "c", InputOption::VALUE_OPTIONAL, "the class name of the task (without namespace)");
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$id = $input->getArgument("id");
$class = $input->getOption("class");


if (!$id) {
if (!$id && !$class) {
$this->scheduler->run();
} elseif ($class) {
$tasks = $this->scheduler->getTasks();
foreach ($tasks as $task) {
if (strpos(get_class($task), "\\$class")) {
$this->scheduler->runTask($task);
return 0;
}
}
throw new \Exception("There are no tasks corresponding to this class name");
} else {
$tasks = $this->scheduler->getTasks();
$id = (int)$id;
Expand Down
29 changes: 29 additions & 0 deletions Tests/Command/RunCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,35 @@ public function testRunCommandWithId(): void
"id" => 1,
]);

$this->assertEquals(1, TaskMock::$runCount);
$this->assertEquals(1, $t1->localCount);
$this->assertEquals(0, $t2->localCount);
}

public function testRunCommandWithClassName(): void
{
$container = $this->loadContainer();

/** @var Scheduler $scheduler */
$scheduler = $container->get("ts.scheduler");

$t1 = new TaskMock();
$t2 = new TaskMock();

$scheduler->addTask($t1);
$scheduler->addTask($t2);

$application = new Application();
$application->add(new RunCommand($scheduler));
$command = $application->find("ts:run");

$commandTester = new CommandTester($command);
$commandTester->execute([
"command" => $command->getName(),
"--class" => "TaskMock",
]);


$this->assertEquals(1, TaskMock::$runCount);
$this->assertEquals(1, $t1->localCount);
$this->assertEquals(0, $t2->localCount);
Expand Down

0 comments on commit d971436

Please sign in to comment.