Skip to content

Commit

Permalink
Cancel root task when nested task is canceled
Browse files Browse the repository at this point in the history
  • Loading branch information
kuaukutsu committed Aug 15, 2024
1 parent f318343 commit 61555ba
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 14 deletions.
10 changes: 10 additions & 0 deletions src/processing/TaskProcessing.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ public function cancel(TaskProcess $process): void
$exception,
);
}

if ($task->isPromised()) {
$stateRelation = $task->getState();
if ($stateRelation instanceof TaskStateRelation) {
$this->taskExecutor->cancel(

Check failure on line 145 in src/processing/TaskProcessing.php

View workflow job for this annotation

GitHub Actions / static code analysis (8.2)

TooManyArguments

src/processing/TaskProcessing.php:145:38: TooManyArguments: Too many arguments for method kuaukutsu\poc\task\service\TaskExecutor::cancel - saw 2 (see https://psalm.dev/026)

Check failure on line 145 in src/processing/TaskProcessing.php

View workflow job for this annotation

GitHub Actions / static code analysis (8.3)

TooManyArguments

src/processing/TaskProcessing.php:145:38: TooManyArguments: Too many arguments for method kuaukutsu\poc\task\service\TaskExecutor::cancel - saw 2 (see https://psalm.dev/026)
$this->factory($stateRelation->task),
$stateRelation,
);
}
}
}

/**
Expand Down
15 changes: 9 additions & 6 deletions src/service/action/ActionCancel.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function execute(EntityTask $task, ?TaskStateInterface $state = null): En
}

$state = new TaskStateCanceled(
message: $state?->getMessage() ?? new TaskStateMessage('Task Canceled.'),
message: $state?->getMessage() ?? new TaskStateMessage('Canceled.'),
flag: $task->getFlag(),
);

Expand All @@ -47,6 +47,7 @@ public function execute(EntityTask $task, ?TaskStateInterface $state = null): En
);

$uuid = new EntityUuid($task->getUuid());
$isRoot = $task->isPromised() === false;

try {
$this->stageCommand->stateByTask($uuid, new StageModelState($state));
Expand All @@ -61,11 +62,13 @@ public function execute(EntityTask $task, ?TaskStateInterface $state = null): En
$this->taskCommand->state($uuid, new TaskModelState($state))
);

$this->finallyHandler->handle(
$task->getUuid(),
$task->getOptions(),
$task->getState(),
);
if ($isRoot) {
$this->finallyHandler->handle(
$task->getUuid(),
$task->getOptions(),
$task->getState(),
);
}

return $task;
}
Expand Down
95 changes: 87 additions & 8 deletions tests/ProcessingPromiseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace kuaukutsu\poc\task\tests;

use kuaukutsu\poc\task\state\TaskStateCanceled;
use kuaukutsu\poc\task\tests\service\BaseStorage;
use kuaukutsu\poc\task\tests\stub\TestFinally;
use PHPUnit\Framework\MockObject\Exception;
use Psr\Container\ContainerExceptionInterface;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -49,6 +52,8 @@ final class ProcessingPromiseTest extends TestCase

private readonly TaskManagerOptions $options;

private readonly BaseStorage $storage;

/**
* @throws ContainerExceptionInterface
*/
Expand All @@ -62,6 +67,7 @@ public function __construct(string $name)
$this->processing = self::get(TaskProcessing::class);
$this->builder = self::get(TaskBuilder::class);
$this->handler = self::get(StageHandler::class);
$this->storage = self::get(BaseStorage::class);

$this->options = new TaskManagerOptions(
bindir: __DIR__ . '/bin',
Expand Down Expand Up @@ -120,6 +126,10 @@ public function testLoadingPromise(): void
$contextNestedTask = $this->processing->getTaskProcess();
self::assertNotEquals($context->task, $contextNestedTask->task);

// Root Task is Waiting
$task = $this->taskQuery->getOne($uuid);
self::assertEquals($flag->unset()->setWaiting()->toValue(), $task->flag);

// Nested Task
$nestedUuid = new EntityUuid($contextNestedTask->task);
$task = $this->taskQuery->getOne($nestedUuid);
Expand Down Expand Up @@ -212,19 +222,88 @@ public function testLoadingPromise(): void
$this->destroyer->purge($nestedUuid);
}

protected function setUp(): void
/**
* @throws Exception
*/
public function testCancelPromise(): void
{
$this->task = $this->builder->build(
$this->builder->create(
'task test promise',
new EntityWrapper(
class: TestHandlerStageStub::class,
$flag = new TaskFlag();
$uuid = new EntityUuid($this->task->getUuid());

$this->processing->loadTaskProcess($this->options);

$context = $this->processing->getTaskProcess();
$exitCode = $this->handler->handle($context->task, $context->stage);
self::assertEquals(0, $exitCode);

$this->processing->next(
new TaskProcess(
$context->getHash(),
$context->task,
$context->stage,
$this->getProcess(
new TaskStateWaiting(
uuid: $context->stage,
task: $context->task,
message: new TaskStateMessage('Waiting')
)
),
new EntityWrapper(
class: TestContextResponseStageStub::class,
)
);

$this->processing->loadTaskProcess($this->options);
// Смена контекста на вложенную задачу
$contextNestedTask = $this->processing->getTaskProcess();
$nestedUuid = new EntityUuid($contextNestedTask->task);
// one stage
$exitCode = $this->handler->handle($contextNestedTask->task, $contextNestedTask->stage);
self::assertEquals(0, $exitCode);

$this->processing->cancel(
new TaskProcess(
$contextNestedTask->getHash(),
$contextNestedTask->task,
$contextNestedTask->stage,
$this->getProcess(
new TaskStateCanceled(
new TaskStateMessage('Hidden message')
)
),
)
);

$task = $this->taskQuery->getOne($nestedUuid);
self::assertEquals(
$flag->unset()->setPromised()->setCanceled()->toValue(),
$task->flag,
);

$task = $this->taskQuery->getOne($uuid);
self::assertEquals(
$flag->unset()->setWaiting()->setCanceled()->toValue(),
$task->flag,
);

self::assertEquals('Canceled.', $this->storage->get($this->task->getUuid()));
}

protected function setUp(): void
{
$this->task = $this->builder->build(
$this->builder
->create(
'task test promise',
new EntityWrapper(
class: TestHandlerStageStub::class,
),
new EntityWrapper(
class: TestContextResponseStageStub::class,
),
)
->setFinally(
TestFinally::class,
)
);
}

protected function tearDown(): void
Expand Down

0 comments on commit 61555ba

Please sign in to comment.