Skip to content

3. Adding a new function

Thierry Feuzeu edited this page Feb 5, 2025 · 1 revision

Here are the steps to implement a new function. We'll take the MoneyBatch workflow as example, and define interfaces, facades and classes for the workflow and the activity.

Workflow

  1. Add the workflow interface and class in the workflow-worker app.

In the src\Workflow\Service\Workflow\MoneyBatch\MoneyBatchWorkflowInterface.php file,

namespace App\Workflow\Service\Workflow\MoneyBatch;

use Temporal\Workflow\WorkflowInterface;

#[WorkflowInterface]
interface MoneyBatchWorkflowInterface
{
}

In the src\Workflow\Service\Workflow\MoneyBatch\MoneyBatchWorkflow.php file,

namespace App\Workflow\Service\Workflow\MoneyBatch;

class MoneyBatchWorkflow implements MoneyBatchWorkflowInterface
{
}
  1. For a main workflow,
  • Define the workflow options and add an attribute to the workflow interface in the workflow-api app.

In the src/Temporal/Factory/WorkflowFactory.php file,

namespace App\Temporal\Factory;

use Carbon\CarbonInterval;
use Temporal\Client\WorkflowOptions;

class WorkflowFactory
{
    public static function moneyBatchOptions(string $workflowTaskQueue): WorkflowOptions
    {
        return WorkflowOptions::new()
            ->withTaskQueue($workflowTaskQueue)
            ->withWorkflowExecutionTimeout(CarbonInterval::hour());
    }
}

In the config\temporal\services.yaml file,

services:
    moneyBatchWorkflowOptions:
        class: 'Temporal\Client\WorkflowOptions'
        factory: ['App\Temporal\Factory\WorkflowFactory', 'moneyBatchOptions']

In the src\Workflow\Service\Workflow\MoneyBatch\MoneyBatchWorkflowInterface.php file,

namespace App\Workflow\Service\Workflow\MoneyBatch;

use App\Temporal\Attribute\WorkflowOptions;
use Temporal\Workflow\WorkflowInterface;

#[WorkflowInterface]
#[WorkflowOptions(serviceId: "moneyBatchWorkflowOptions")]
interface MoneyBatchWorkflowInterface
{
}
  • Add the workflow facade in the workflow-api app.

In the src\Workflow\Service\Workflow\MoneyBatch\MoneyBatchWorkflowFacade.php file,

namespace App\Workflow\Service\Workflow\MoneyBatch;

use App\Temporal\Factory\WorkflowClientTrait;
use Lagdo\Symfony\Facades\AbstractFacade;

class MoneyBatchWorkflowFacade extends AbstractFacade
{
    use WorkflowClientTrait;

    /**
     * @inheritDoc
     */
    protected static function getServiceIdentifier(): string
    {
        return MoneyBatchWorkflowInterface::class;
    }
}

The WorkflowClientTrait trait provides additional helper functions to start a new workflow and get a running workflow. See the src\Controller\MoneyBatchController.php file for examples.

  1. For a child workflow,
  • Define the child workflow options and add an attribute to the workflow interface in the workflow-worker app.

In the src/Temporal/Factory/WorkflowFactory.php file,

namespace App\Temporal\Factory;

use Temporal\Workflow\ChildWorkflowOptions;

class WorkflowFactory
{
    public static function defaultOptions(string $workflowTaskQueue): ChildWorkflowOptions
    {
        return ChildWorkflowOptions::new()
            ->withTaskQueue($workflowTaskQueue);
    }
}

In the config\temporal\services.yaml file,

services:
    defaultChildWorkflowOptions:
        class: 'Temporal\Workflow\ChildWorkflowOptions'
        factory: ['App\Temporal\Factory\WorkflowFactory', 'defaultOptions']

In the src\Workflow\Service\Workflow\Child\ChildWorkflowInterface.php file,

namespace App\Workflow\Service\Workflow\Child;

use App\Temporal\Attribute\ChildWorkflowOptions;
use Temporal\Workflow\WorkflowInterface;

#[WorkflowInterface]
#[ChildWorkflowOptions(serviceId: "defaultChildWorkflowOptions")]
interface ChildWorkflowInterface
{
}
  • Add the child workflow facade in the workflow-worker app.

In the src\Workflow\Service\Workflow\Child\ChildWorkflowFacade.php file,

namespace App\Workflow\Service\Workflow\Child;

use Lagdo\Symfony\Facades\AbstractFacade;

class ChildWorkflowFacade extends AbstractFacade
{
    /**
     * @inheritDoc
     */
    protected static function getServiceIdentifier(): string
    {
        return ChildWorkflowInterface::class;
    }
}

Activity

  1. Add the activity interface and class in the activity-worker app.

In the src\Workflow\Service\Activity\MoneyBatch\AccountActivityInterface.php file,

namespace App\Workflow\Service\Activity\MoneyBatch;

use Temporal\Activity\ActivityInterface;

#[ActivityInterface(prefix: "MoneyBatch.")]
interface AccountActivityInterface
{
}

In the src\Workflow\Service\Activity\MoneyBatch\AccountActivity.php file,

namespace App\Workflow\Service\Activity\MoneyBatch;

class AccountActivity implements AccountActivityInterface
{
}
  1. Define the activity options and add an attribute to the activity interface in the workflow-worker app.

In the src/Temporal/Factory/ActivityFactory.php file,

namespace App\Temporal\Factory;

use Carbon\CarbonInterval;
use Temporal\Activity\ActivityOptions;
use Temporal\Common\RetryOptions;

class ActivityFactory
{
    public static function moneyBatchOptions(string $activityTaskQueue): ActivityOptions
    {
        return ActivityOptions::new()
            ->withTaskQueue($activityTaskQueue)
            ->withStartToCloseTimeout(CarbonInterval::seconds(15))
            ->withScheduleToCloseTimeout(CarbonInterval::hour(1))
            ->withRetryOptions(
                RetryOptions::new()
                    ->withMaximumAttempts(10)
                    ->withInitialInterval(CarbonInterval::second(1))
                    ->withMaximumInterval(CarbonInterval::seconds(10))
            );
    }
}

In the config\temporal\services.yaml file,

services:
    moneyBatchActivityOptions:
        class: 'Temporal\Activity\ActivityOptions'
        factory: ['App\Temporal\Factory\ActivityFactory', 'moneyBatchOptions']

In the src\Workflow\Service\Activity\MoneyBatch\AccountActivityInterface.php file,

namespace App\Workflow\Service\Activity\MoneyBatch;

use App\Temporal\Attribute\ActivityOptions;
use Temporal\Activity\ActivityInterface;

#[ActivityInterface(prefix: "MoneyBatch.")]
#[ActivityOptions(serviceId: "moneyBatchActivityOptions")]
interface AccountActivityInterface
{
}
  1. Add the activity facade in the workflow-worker app.

In the Workflow\Service\Activity\MoneyBatch\AccountActivityFacade.php file,

namespace App\Workflow\Service\Activity\MoneyBatch;

use Lagdo\Symfony\Facades\AbstractFacade;

class AccountActivityFacade extends AbstractFacade
{
    /**
     * @inheritDoc
     */
    protected static function getServiceIdentifier(): string
    {
        return AccountActivityInterface::class;
    }
}
Clone this wiki locally