Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancel root task when nested task is canceled #59

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/processing/TaskProcessing.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ public function cancel(TaskProcess $process): void
$exception,
);
}

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

/**
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
Loading