Skip to content

Commit

Permalink
added special sync handling function for tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
matthi4s committed Oct 27, 2023
1 parent 93200ac commit 69e60b8
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Tasks can communicate back to the main process during execution and handle resul
* [Child/parent attributes](#childparent-attributes)
* [Synchronized properties](#synchronized-properties)
* [Serialization in other classes](#serialization-in-other-classes)
* [Synchronous environment](#synchronous-environment)
* [Handling the result](#handling-the-result)
* [Timeout](#timeout)
* [Handling errors](#handling-errors)
Expand Down Expand Up @@ -229,6 +230,13 @@ When using only the [`NotSerializable`](src/Communication/Serialization/NotSeria
When using both attributes, all properties **must** be marked with either the [`Serializable`](src/Communication/Serialization/Serializable.php)
or [`NotSerializable`](src/Communication/Serialization/NotSerializable.php) attribute, otherwise an exception will be thrown.

### Synchronous environment

In some cases special handling is required when the task is executed in a synchronous environment
using the [`SyncWorker`](src/Environment/Sync/SyncWorker.php), e.g. you might not want to close
file handles that are still used by other tasks. The `Task::isSync()` function can
be used to check if the task is being executed synchronously.

### Handling the result

The `Task::handleResult()` function is called when the task returns a value. It can be used to handle
Expand Down
10 changes: 10 additions & 0 deletions src/Environment/Sync/SyncRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Aternos\Taskmaster\Environment\Sync;

use Aternos\Taskmaster\Communication\Promise\ResponsePromise;
use Aternos\Taskmaster\Communication\Request\RunTaskRequest;
use Aternos\Taskmaster\Communication\RequestInterface;
use Aternos\Taskmaster\Communication\ResponseInterface;
use Aternos\Taskmaster\Runtime\Runtime;
Expand Down Expand Up @@ -51,6 +52,15 @@ public function sendRequest(RequestInterface $request): ResponsePromise
return (new ResponsePromise())->resolve($response);
}

/**
* @inheritDoc
*/
protected function runTask(RunTaskRequest $request): ResponseInterface
{
$request->getTask()->setSync();
return parent::runTask($request);
}

/**
* @inheritDoc
*/
Expand Down
24 changes: 24 additions & 0 deletions src/Task/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ abstract class Task implements TaskInterface
#[OnParent] protected ?Exception $error = null;
#[OnParent] protected ?TaskPromise $promise = null;
#[OnParent] protected ?float $timeout = null;
#[OnChild] protected bool $sync = false;

/**
* @inheritDoc
Expand Down Expand Up @@ -263,4 +264,27 @@ public function setTimeout(?float $timeout): static
$this->timeout = $timeout;
return $this;
}

/**
* @inheritDoc
*/
public function setSync(bool $sync = true): static
{
$this->sync = $sync;
return $this;
}

/**
* Check if the task is executed in a sync environment
*
* Some cases must be handled differently in a sync environment because
* you are operating on the same and not just equal objects, e.g. you
* might not want to close file handles that are still used by other tasks.
*
* @return bool
*/
protected function isSync(): bool
{
return $this->sync;
}
}
11 changes: 11 additions & 0 deletions src/Task/TaskInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,15 @@ public function getTimeout(): ?float;
* @return $this
*/
public function setTimeout(?float $timeout): static;

/**
* Tell the task that it's being executed in a sync environment
*
* Some cases must be handled differently in a sync environment because
* you are operating on the same and not just equal objects, e.g. you
* might not want to close file handles that are still used by other tasks.
*
* @return $this
*/
public function setSync(bool $sync = true): static;
}
9 changes: 9 additions & 0 deletions test/Integration/AsyncWorkerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Aternos\Taskmaster\Test\Util\Task\EmptyTask;
use Aternos\Taskmaster\Test\Util\Task\InterruptableSleepTask;
use Aternos\Taskmaster\Test\Util\Task\SleepTask;
use Aternos\Taskmaster\Test\Util\Task\SyncTask;
use Aternos\Taskmaster\Test\Util\Task\WarningTask;
use Aternos\Taskmaster\Worker\WorkerInterface;

Expand Down Expand Up @@ -56,6 +57,14 @@ public function testDefaultTimeout(): void
}
}

public function testSyncTask(): void
{
$task = new SyncTask();
$this->taskmaster->runTask($task);
$this->taskmaster->wait();
$this->assertFalse($task->getResult());
}

public function testRecoverAfterTimeout(): void
{
$this->taskmaster->setDefaultTaskTimeout(0.005 * $this->getTimeFactor());
Expand Down
9 changes: 9 additions & 0 deletions test/Integration/SyncWorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Aternos\Taskmaster\Environment\Sync\SyncWorker;
use Aternos\Taskmaster\Taskmaster;
use Aternos\Taskmaster\Test\Util\Task\SyncTask;

class SyncWorkerTest extends WorkerTestCase
{
Expand All @@ -12,4 +13,12 @@ protected function createTaskmaster(): void
$this->taskmaster = new Taskmaster();
$this->taskmaster->addWorker(new SyncWorker());
}

public function testSyncTask(): void
{
$task = new SyncTask();
$this->taskmaster->runTask($task);
$this->taskmaster->wait();
$this->assertTrue($task->getResult());
}
}
18 changes: 18 additions & 0 deletions test/Util/Task/SyncTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Aternos\Taskmaster\Test\Util\Task;

use Aternos\Taskmaster\Task\OnChild;
use Aternos\Taskmaster\Task\Task;

class SyncTask extends Task
{
/**
* @inheritDoc
*/
#[OnChild]
public function run(): bool
{
return $this->isSync();
}
}

0 comments on commit 69e60b8

Please sign in to comment.