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

Customize worker options #39

Merged
merged 3 commits into from
May 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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,22 @@ To create a new interceptor, you can use the `temporal:make:interceptor {name}`

To run the temporal worker, you can use the `temporal:work {queue?}` command.

If you want to customize the options of the temporal worker, you can call `Temporal::buildWorkerOptionsUsing` in your service provider:

```php
class AppServiceProvider extends ServiceProvider
{
public function boot(): vodi {
\Keepsuit\LaravelTemporal\Facade\Temporal::buildWorkerOptionsUsing(function (string $taskQueue) {
// you can build different worker options based on the task queue
return \Temporal\Worker\WorkerOptions::new()
->withMaxConcurrentActivityTaskPollers(10)
->withMaxConcurrentWorkflowTaskPollers(10);
});
}
}
```

## Testing utilities

In order to test workflows end-to-end, you need a temporal server running.
Expand Down
9 changes: 7 additions & 2 deletions bin/roadrunner-temporal-worker
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<?php
declare(strict_types=1);

use Keepsuit\LaravelTemporal\Facade\Temporal;
use Keepsuit\LaravelTemporal\Interceptors\ApplicationSandboxInterceptor;
use Keepsuit\LaravelTemporal\Support\ApplicationFactory;
use Keepsuit\LaravelTemporal\Support\CurrentApplication;
Expand All @@ -17,7 +18,7 @@ $app = (new ApplicationFactory($basePath))->createApplication();
CurrentApplication::setRootApp($app);

if ($app->environment() !== 'production' && env('TEMPORAL_TESTING_ENV')) {
\Keepsuit\LaravelTemporal\Facade\Temporal::initFakeWorker();
Temporal::initFakeWorker();
}

/** @var TemporalRegistry $registry */
Expand All @@ -28,9 +29,13 @@ $factory = WorkerFactory::create(
converter: $app->make(DataConverterInterface::class)
);

$taskQueue = env('TEMPORAL_QUEUE', \Temporal\WorkerFactory::DEFAULT_TASK_QUEUE);
assert(is_string($taskQueue), 'TEMPORAL_QUEUE must be a string');

// Worker that listens on a Task Queue and hosts both workflow and activity implementations.
$worker = $factory->newWorker(
taskQueue: env('TEMPORAL_QUEUE'),
taskQueue: $taskQueue,
options: Temporal::buildWorkerOptions($taskQueue),
interceptorProvider: new SimplePipelineProvider(array_map(
fn (string $className) => $app->make($className),
array_merge([
Expand Down
16 changes: 10 additions & 6 deletions src/Facade/Temporal.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Keepsuit\LaravelTemporal\Facade;

use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Facade;
Expand All @@ -15,23 +16,26 @@
use Keepsuit\LaravelTemporal\Testing\TemporalTestingEnvironment;
use Keepsuit\LaravelTemporal\Testing\WorkflowMockBuilder;
use Temporal\Client\WorkflowClientInterface;
use Temporal\Worker\WorkerOptions;
use Temporal\Workflow;

/**
* @method static WorkflowBuilder newWorkflow()
* @method static ChildWorkflowBuilder newChildWorkflow()
* @method static ActivityBuilder newActivity()
* @method static LocalActivityBuilder newLocalActivity()
* @method static void buildWorkerOptionsUsing(Closure $callback)
* @method static WorkerOptions|null buildWorkerOptions(string $taskQueue)
* @method static void mockWorkflows(array $workflowMocks, ?string $taskQueue = null)
* @method static WorkflowMockBuilder mockWorkflow(string $workflowName)
* @method static void mockActivities(array $activitiesMocks, ?string $taskQueue = null)
* @method static ActivityMockBuilder mockActivity(string|array $activityName)
* @method static void assertWorkflowDispatched(string $workflowName, \Closure|int|null $callback = null)
* @method static void assertWorkflowDispatchedTimes(string $workflowName, int $times = 1, \Closure|null $callback = null)
* @method static void assertWorkflowNotDispatched(string $workflowName, \Closure|null $callback = null)
* @method static void assertActivityDispatched(string|array $activityName, \Closure|int|null $callback = null)
* @method static void assertActivityDispatchedTimes(string|array $activityName, int $times = 1, \Closure|null $callback = null)
* @method static void assertActivityNotDispatched(string|array $activityName, \Closure|null $callback = null)
* @method static void assertWorkflowDispatched(string $workflowName, Closure|int|null $callback = null)
* @method static void assertWorkflowDispatchedTimes(string $workflowName, int $times = 1, Closure|null $callback = null)
* @method static void assertWorkflowNotDispatched(string $workflowName, Closure|null $callback = null)
* @method static void assertActivityDispatched(string|array $activityName, Closure|int|null $callback = null)
* @method static void assertActivityDispatchedTimes(string|array $activityName, int $times = 1, Closure|null $callback = null)
* @method static void assertActivityNotDispatched(string|array $activityName, Closure|null $callback = null)
*/
class Temporal extends Facade
{
Expand Down
28 changes: 28 additions & 0 deletions src/Temporal.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

namespace Keepsuit\LaravelTemporal;

use Closure;
use Keepsuit\LaravelTemporal\Builder\ActivityBuilder;
use Keepsuit\LaravelTemporal\Builder\ChildWorkflowBuilder;
use Keepsuit\LaravelTemporal\Builder\LocalActivityBuilder;
use Keepsuit\LaravelTemporal\Builder\WorkflowBuilder;
use Temporal\Worker\WorkerOptions;

class Temporal implements Contracts\Temporal
{
/**
* @var Closure(string):(WorkerOptions|null)|null
*/
protected static ?Closure $buildWorkerOptionsCallback = null;

public function newActivity(): ActivityBuilder
{
return ActivityBuilder::new();
Expand All @@ -28,4 +35,25 @@ public function newChildWorkflow(): ChildWorkflowBuilder
{
return ChildWorkflowBuilder::new();
}

public function buildWorkerOptions(string $taskQueue): WorkerOptions
{
if (static::$buildWorkerOptionsCallback) {
$workerOptions = call_user_func(static::$buildWorkerOptionsCallback, $taskQueue);

if ($workerOptions instanceof WorkerOptions) {
return $workerOptions;
}
}

return WorkerOptions::new();
}

/**
* @param Closure(string):(WorkerOptions|null) $callback
*/
public function buildWorkerOptionsUsing(Closure $callback): void
{
static::$buildWorkerOptionsCallback = $callback;
}
}