Skip to content

Commit 69e60b8

Browse files
committed
added special sync handling function for tasks
1 parent 93200ac commit 69e60b8

File tree

7 files changed

+89
-0
lines changed

7 files changed

+89
-0
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Tasks can communicate back to the main process during execution and handle resul
2424
* [Child/parent attributes](#childparent-attributes)
2525
* [Synchronized properties](#synchronized-properties)
2626
* [Serialization in other classes](#serialization-in-other-classes)
27+
* [Synchronous environment](#synchronous-environment)
2728
* [Handling the result](#handling-the-result)
2829
* [Timeout](#timeout)
2930
* [Handling errors](#handling-errors)
@@ -229,6 +230,13 @@ When using only the [`NotSerializable`](src/Communication/Serialization/NotSeria
229230
When using both attributes, all properties **must** be marked with either the [`Serializable`](src/Communication/Serialization/Serializable.php)
230231
or [`NotSerializable`](src/Communication/Serialization/NotSerializable.php) attribute, otherwise an exception will be thrown.
231232

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

234242
The `Task::handleResult()` function is called when the task returns a value. It can be used to handle

src/Environment/Sync/SyncRuntime.php

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Aternos\Taskmaster\Environment\Sync;
44

55
use Aternos\Taskmaster\Communication\Promise\ResponsePromise;
6+
use Aternos\Taskmaster\Communication\Request\RunTaskRequest;
67
use Aternos\Taskmaster\Communication\RequestInterface;
78
use Aternos\Taskmaster\Communication\ResponseInterface;
89
use Aternos\Taskmaster\Runtime\Runtime;
@@ -51,6 +52,15 @@ public function sendRequest(RequestInterface $request): ResponsePromise
5152
return (new ResponsePromise())->resolve($response);
5253
}
5354

55+
/**
56+
* @inheritDoc
57+
*/
58+
protected function runTask(RunTaskRequest $request): ResponseInterface
59+
{
60+
$request->getTask()->setSync();
61+
return parent::runTask($request);
62+
}
63+
5464
/**
5565
* @inheritDoc
5666
*/

src/Task/Task.php

+24
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ abstract class Task implements TaskInterface
3535
#[OnParent] protected ?Exception $error = null;
3636
#[OnParent] protected ?TaskPromise $promise = null;
3737
#[OnParent] protected ?float $timeout = null;
38+
#[OnChild] protected bool $sync = false;
3839

3940
/**
4041
* @inheritDoc
@@ -263,4 +264,27 @@ public function setTimeout(?float $timeout): static
263264
$this->timeout = $timeout;
264265
return $this;
265266
}
267+
268+
/**
269+
* @inheritDoc
270+
*/
271+
public function setSync(bool $sync = true): static
272+
{
273+
$this->sync = $sync;
274+
return $this;
275+
}
276+
277+
/**
278+
* Check if the task is executed in a sync environment
279+
*
280+
* Some cases must be handled differently in a sync environment because
281+
* you are operating on the same and not just equal objects, e.g. you
282+
* might not want to close file handles that are still used by other tasks.
283+
*
284+
* @return bool
285+
*/
286+
protected function isSync(): bool
287+
{
288+
return $this->sync;
289+
}
266290
}

src/Task/TaskInterface.php

+11
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,15 @@ public function getTimeout(): ?float;
154154
* @return $this
155155
*/
156156
public function setTimeout(?float $timeout): static;
157+
158+
/**
159+
* Tell the task that it's being executed in a sync environment
160+
*
161+
* Some cases must be handled differently in a sync environment because
162+
* you are operating on the same and not just equal objects, e.g. you
163+
* might not want to close file handles that are still used by other tasks.
164+
*
165+
* @return $this
166+
*/
167+
public function setSync(bool $sync = true): static;
157168
}

test/Integration/AsyncWorkerTestCase.php

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Aternos\Taskmaster\Test\Util\Task\EmptyTask;
99
use Aternos\Taskmaster\Test\Util\Task\InterruptableSleepTask;
1010
use Aternos\Taskmaster\Test\Util\Task\SleepTask;
11+
use Aternos\Taskmaster\Test\Util\Task\SyncTask;
1112
use Aternos\Taskmaster\Test\Util\Task\WarningTask;
1213
use Aternos\Taskmaster\Worker\WorkerInterface;
1314

@@ -56,6 +57,14 @@ public function testDefaultTimeout(): void
5657
}
5758
}
5859

60+
public function testSyncTask(): void
61+
{
62+
$task = new SyncTask();
63+
$this->taskmaster->runTask($task);
64+
$this->taskmaster->wait();
65+
$this->assertFalse($task->getResult());
66+
}
67+
5968
public function testRecoverAfterTimeout(): void
6069
{
6170
$this->taskmaster->setDefaultTaskTimeout(0.005 * $this->getTimeFactor());

test/Integration/SyncWorkerTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Aternos\Taskmaster\Environment\Sync\SyncWorker;
66
use Aternos\Taskmaster\Taskmaster;
7+
use Aternos\Taskmaster\Test\Util\Task\SyncTask;
78

89
class SyncWorkerTest extends WorkerTestCase
910
{
@@ -12,4 +13,12 @@ protected function createTaskmaster(): void
1213
$this->taskmaster = new Taskmaster();
1314
$this->taskmaster->addWorker(new SyncWorker());
1415
}
16+
17+
public function testSyncTask(): void
18+
{
19+
$task = new SyncTask();
20+
$this->taskmaster->runTask($task);
21+
$this->taskmaster->wait();
22+
$this->assertTrue($task->getResult());
23+
}
1524
}

test/Util/Task/SyncTask.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Aternos\Taskmaster\Test\Util\Task;
4+
5+
use Aternos\Taskmaster\Task\OnChild;
6+
use Aternos\Taskmaster\Task\Task;
7+
8+
class SyncTask extends Task
9+
{
10+
/**
11+
* @inheritDoc
12+
*/
13+
#[OnChild]
14+
public function run(): bool
15+
{
16+
return $this->isSync();
17+
}
18+
}

0 commit comments

Comments
 (0)