File tree 7 files changed +89
-0
lines changed
7 files changed +89
-0
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ Tasks can communicate back to the main process during execution and handle resul
24
24
* [ Child/parent attributes] ( #childparent-attributes )
25
25
* [ Synchronized properties] ( #synchronized-properties )
26
26
* [ Serialization in other classes] ( #serialization-in-other-classes )
27
+ * [ Synchronous environment] ( #synchronous-environment )
27
28
* [ Handling the result] ( #handling-the-result )
28
29
* [ Timeout] ( #timeout )
29
30
* [ Handling errors] ( #handling-errors )
@@ -229,6 +230,13 @@ When using only the [`NotSerializable`](src/Communication/Serialization/NotSeria
229
230
When using both attributes, all properties ** must** be marked with either the [ ` Serializable ` ] ( src/Communication/Serialization/Serializable.php )
230
231
or [ ` NotSerializable ` ] ( src/Communication/Serialization/NotSerializable.php ) attribute, otherwise an exception will be thrown.
231
232
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
+
232
240
### Handling the result
233
241
234
242
The ` Task::handleResult() ` function is called when the task returns a value. It can be used to handle
Original file line number Diff line number Diff line change 3
3
namespace Aternos \Taskmaster \Environment \Sync ;
4
4
5
5
use Aternos \Taskmaster \Communication \Promise \ResponsePromise ;
6
+ use Aternos \Taskmaster \Communication \Request \RunTaskRequest ;
6
7
use Aternos \Taskmaster \Communication \RequestInterface ;
7
8
use Aternos \Taskmaster \Communication \ResponseInterface ;
8
9
use Aternos \Taskmaster \Runtime \Runtime ;
@@ -51,6 +52,15 @@ public function sendRequest(RequestInterface $request): ResponsePromise
51
52
return (new ResponsePromise ())->resolve ($ response );
52
53
}
53
54
55
+ /**
56
+ * @inheritDoc
57
+ */
58
+ protected function runTask (RunTaskRequest $ request ): ResponseInterface
59
+ {
60
+ $ request ->getTask ()->setSync ();
61
+ return parent ::runTask ($ request );
62
+ }
63
+
54
64
/**
55
65
* @inheritDoc
56
66
*/
Original file line number Diff line number Diff line change @@ -35,6 +35,7 @@ abstract class Task implements TaskInterface
35
35
#[OnParent] protected ?Exception $ error = null ;
36
36
#[OnParent] protected ?TaskPromise $ promise = null ;
37
37
#[OnParent] protected ?float $ timeout = null ;
38
+ #[OnChild] protected bool $ sync = false ;
38
39
39
40
/**
40
41
* @inheritDoc
@@ -263,4 +264,27 @@ public function setTimeout(?float $timeout): static
263
264
$ this ->timeout = $ timeout ;
264
265
return $ this ;
265
266
}
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
+ }
266
290
}
Original file line number Diff line number Diff line change @@ -154,4 +154,15 @@ public function getTimeout(): ?float;
154
154
* @return $this
155
155
*/
156
156
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 ;
157
168
}
Original file line number Diff line number Diff line change 8
8
use Aternos \Taskmaster \Test \Util \Task \EmptyTask ;
9
9
use Aternos \Taskmaster \Test \Util \Task \InterruptableSleepTask ;
10
10
use Aternos \Taskmaster \Test \Util \Task \SleepTask ;
11
+ use Aternos \Taskmaster \Test \Util \Task \SyncTask ;
11
12
use Aternos \Taskmaster \Test \Util \Task \WarningTask ;
12
13
use Aternos \Taskmaster \Worker \WorkerInterface ;
13
14
@@ -56,6 +57,14 @@ public function testDefaultTimeout(): void
56
57
}
57
58
}
58
59
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
+
59
68
public function testRecoverAfterTimeout (): void
60
69
{
61
70
$ this ->taskmaster ->setDefaultTaskTimeout (0.005 * $ this ->getTimeFactor ());
Original file line number Diff line number Diff line change 4
4
5
5
use Aternos \Taskmaster \Environment \Sync \SyncWorker ;
6
6
use Aternos \Taskmaster \Taskmaster ;
7
+ use Aternos \Taskmaster \Test \Util \Task \SyncTask ;
7
8
8
9
class SyncWorkerTest extends WorkerTestCase
9
10
{
@@ -12,4 +13,12 @@ protected function createTaskmaster(): void
12
13
$ this ->taskmaster = new Taskmaster ();
13
14
$ this ->taskmaster ->addWorker (new SyncWorker ());
14
15
}
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
+ }
15
24
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments