|
12 | 12 | use Aternos\Taskmaster\Proxy\ProcessProxy;
|
13 | 13 | use Aternos\Taskmaster\Proxy\ProxyInterface;
|
14 | 14 | use Aternos\Taskmaster\Proxy\ProxyStatus;
|
| 15 | +use Aternos\Taskmaster\Task\CloneTaskFactory; |
| 16 | +use Aternos\Taskmaster\Task\InstanceTaskFactory; |
15 | 17 | use Aternos\Taskmaster\Task\TaskFactoryInterface;
|
16 | 18 | use Aternos\Taskmaster\Task\TaskInterface;
|
17 | 19 | use Aternos\Taskmaster\Worker\SocketWorkerInterface;
|
@@ -64,6 +66,7 @@ class Taskmaster
|
64 | 66 |
|
65 | 67 | protected TaskmasterOptions $options;
|
66 | 68 | protected float $defaultTaskTimeout = 0;
|
| 69 | + protected ?TaskFactoryInterface $initTaskFactory = null; |
67 | 70 |
|
68 | 71 | /**
|
69 | 72 | * Taskmaster constructor
|
@@ -389,6 +392,7 @@ public function addWorker(WorkerInterface $worker): static
|
389 | 392 | }
|
390 | 393 |
|
391 | 394 | $worker->setOptionsIfNecessary($this->options);
|
| 395 | + $worker->setInitTaskFactoryIfNecessary($this->initTaskFactory); |
392 | 396 | $this->workers[] = $worker;
|
393 | 397 | return $this;
|
394 | 398 | }
|
@@ -561,6 +565,44 @@ public function setDefaultTaskTimeout(float $timeout): static
|
561 | 565 | return $this;
|
562 | 566 | }
|
563 | 567 |
|
| 568 | + /** |
| 569 | + * Set the default init task factory for all workers |
| 570 | + * |
| 571 | + * The init task factory produces tasks that are executed once as first task on every worker instance |
| 572 | + * to initialize the worker. |
| 573 | + * |
| 574 | + * You can also set the init task factory for each worker individually with {@link WorkerInterface::setInitTaskFactory()}. |
| 575 | + * |
| 576 | + * @param TaskFactoryInterface|null $initTaskFactory |
| 577 | + * @return $this |
| 578 | + */ |
| 579 | + public function setDefaultInitTaskFactory(?TaskFactoryInterface $initTaskFactory): static |
| 580 | + { |
| 581 | + $this->initTaskFactory = $initTaskFactory; |
| 582 | + return $this; |
| 583 | + } |
| 584 | + |
| 585 | + /** |
| 586 | + * Set the default init task for all workers |
| 587 | + * |
| 588 | + * The init task is executed once as first task on every worker instance. |
| 589 | + * This automatically creates a task factory that produces the init tasks. |
| 590 | + * |
| 591 | + * If you pass an object, the task will be cloned for each worker instance, if you pass |
| 592 | + * a class name, a new instance of the class will be created for each worker instance. |
| 593 | + * |
| 594 | + * @param TaskInterface|class-string<TaskInterface> $task |
| 595 | + * @return void |
| 596 | + */ |
| 597 | + public function setDefaultInitTask(TaskInterface|string $task): void |
| 598 | + { |
| 599 | + if (is_string($task)) { |
| 600 | + $this->initTaskFactory = new InstanceTaskFactory($task); |
| 601 | + } else { |
| 602 | + $this->initTaskFactory = new CloneTaskFactory($task); |
| 603 | + } |
| 604 | + } |
| 605 | + |
564 | 606 | /**
|
565 | 607 | * Get the current {@link TaskmasterOptions} instance
|
566 | 608 | *
|
|
0 commit comments