Skip to content

Commit

Permalink
feat: upsert search attributes (#248)
Browse files Browse the repository at this point in the history
* Upsert search attributes

* Search attributes improvements

* Linter

* Fix tests bootstrap

Co-authored-by: Sergey Zhuk <[email protected]>
  • Loading branch information
cv65kr and seregazhuk authored Oct 3, 2022
1 parent 269211c commit 61a9559
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/Internal/Transport/Request/UpsertSearchAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Temporal\Internal\Transport\Request;

use Temporal\Worker\Transport\Command\Request;

class UpsertSearchAttributes extends Request
{
public const NAME = 'UpsertWorkflowSearchAttributes';

/**
* @param array<string, mixed> $searchAttributes
*/
public function __construct(array $searchAttributes)
{
parent::__construct(self::NAME, ['searchAttributes' => $searchAttributes]);
}
}
11 changes: 11 additions & 0 deletions src/Internal/Workflow/ScopeContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Temporal\Workflow\CancellationScopeInterface;
use Temporal\Workflow\ScopedContextInterface;
use Temporal\Workflow\WorkflowContextInterface;
use Temporal\Internal\Transport\Request\UpsertSearchAttributes;

class ScopeContext extends WorkflowContext implements ScopedContextInterface
{
Expand Down Expand Up @@ -163,4 +164,14 @@ public function timer($interval): PromiseInterface

return $result;
}

/**
* {@inheritDoc}
*/
public function upsertSearchAttributes(array $searchAttributes): void
{
$this->request(
new UpsertSearchAttributes($searchAttributes)
);
}
}
11 changes: 11 additions & 0 deletions src/Internal/Workflow/WorkflowContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use Temporal\Workflow\WorkflowContextInterface;
use Temporal\Workflow\WorkflowExecution;
use Temporal\Workflow\WorkflowInfo;
use Temporal\Internal\Transport\Request\UpsertSearchAttributes;

use function React\Promise\reject;
use function React\Promise\resolve;
Expand Down Expand Up @@ -418,6 +419,16 @@ public function getStackTrace(): string
return StackRenderer::renderTrace($this->trace);
}

/**
* {@inheritDoc}
*/
public function upsertSearchAttributes(array $searchAttributes): void
{
$this->services->client->request(
new UpsertSearchAttributes($searchAttributes)
);
}

/**
* {@inheritDoc}
*/
Expand Down
12 changes: 12 additions & 0 deletions src/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -928,4 +928,16 @@ public static function getStackTrace(): string

return $context->getStackTrace();
}

/**
* Upsert search attributes
*
* @param array<string, mixed> $searchAttributes
*/
public static function upsertSearchAttributes(array $searchAttributes): void
{
/** @var ScopedContextInterface $context */
$context = self::getCurrentContext();
$context->upsertSearchAttributes($searchAttributes);
}
}
5 changes: 5 additions & 0 deletions src/Workflow/WorkflowContextInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,9 @@ public function awaitWithTimeout($interval, ...$conditions): PromiseInterface;
* @return string
*/
public function getStackTrace(): string;

/**
* @param array<string, mixed> $searchAttributes
*/
public function upsertSearchAttributes(array $searchAttributes): void;
}
34 changes: 34 additions & 0 deletions tests/Fixtures/src/Workflow/UpsertSearchAttributesWorkflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* This file is part of Temporal package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Temporal\Tests\Workflow;

use Temporal\Activity\ActivityOptions;
use Temporal\Tests\Activity\SampleActivityInterface;
use Temporal\Workflow;
use Temporal\Workflow\WorkflowMethod;

#[Workflow\WorkflowInterface]
class UpsertSearchAttributesWorkflow
{
#[WorkflowMethod]
public function handler()
{
Workflow::upsertSearchAttributes(
[
'attr1' => 'attr1-value',
'attr2' => true,
]
);

return 'done';
}
}
25 changes: 25 additions & 0 deletions tests/Functional/Client/UpsertSearchAttributesWorkflowTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Temporal\Tests\Functional\Client;

/**
* @group client
* @group functional
*/
class UpsertSearchAttributesWorkflowTestCase extends ClientTestCase
{
public function testUpsertSearchAttributes()
{
$client = $this->createClient();
$workflow = $client->newUntypedWorkflowStub('UpsertSearchAttributesWorkflow');

$e = $client->start($workflow);

$this->assertNotEmpty($e->getExecution()->getID());
$this->assertNotEmpty($e->getExecution()->getRunID());

$this->assertSame('done', $workflow->getResult());
}
}
32 changes: 32 additions & 0 deletions tests/SearchAttributeTestInvoker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Temporal\Tests;

use Temporal\Api\Operatorservice\V1\OperatorServiceClient;
use Grpc\ChannelCredentials;
use Temporal\Api\Operatorservice\V1\AddSearchAttributesRequest;

final class SearchAttributeTestInvoker
{
public function __invoke(): void
{
$operation = new OperatorServiceClient(
getenv('TEMPORAL_ADDRESS') ?: '127.0.0.1:7233',
['credentials' => ChannelCredentials::createInsecure()]
);
$result = $operation->AddSearchAttributes(
new AddSearchAttributesRequest(
[
'search_attributes' => [
'attr1' => 2, // Keyword
'attr2' => 5, // Bool
]
]
)
);

$result->getMetadata();
}
}
5 changes: 4 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
declare(strict_types=1);

use Temporal\Testing\Environment;
use Temporal\Tests\SearchAttributeTestInvoker;

require __DIR__ . '/../vendor/autoload.php';

if (getenv('RUN_TEMPORAL_TEST_SERVER') !== false) {
$environment = Environment::create();
$environment->start('./rr serve -c .rr.silent.yaml -w tests');
$environment->startTemporalTestServer();
(new SearchAttributeTestInvoker)();
$environment->startRoadRunner('./rr serve -c .rr.silent.yaml -w tests');
register_shutdown_function(fn() => $environment->stop());
}

0 comments on commit 61a9559

Please sign in to comment.