From 620139f033bc68ae97db1b92bf65b6d4397c08d9 Mon Sep 17 00:00:00 2001 From: Lukas Benke Date: Fri, 4 Feb 2022 13:35:02 +0100 Subject: [PATCH] Example cron get last completion result fail --- app/composer.json | 3 +- app/gen/proto/docs/index.md | 67 +++++++ .../workflows.common.v1/WorkflowResult.json | 29 +++ .../Common/V1/GPBMetadata/Workflows.php | 36 ++++ .../Workflows/Common/V1/WorkflowResult.php | 186 ++++++++++++++++++ app/proto/workflows/common/workflows.proto | 15 ++ app/src/SimpleActivity/ExecuteCommand.php | 4 +- app/src/SimpleActivity/GreetingWorkflow.php | 37 +++- .../GreetingWorkflowInterface.php | 3 +- bin/buf-generate.sh | 10 + buf.gen.yaml | 15 ++ buf.work.yaml | 3 + buf.yaml | 7 + 13 files changed, 411 insertions(+), 4 deletions(-) create mode 100644 app/gen/proto/docs/index.md create mode 100644 app/gen/proto/jsonschema/json/workflows.common.v1/WorkflowResult.json create mode 100644 app/gen/proto/php/Workflows/Common/V1/GPBMetadata/Workflows.php create mode 100644 app/gen/proto/php/Workflows/Common/V1/WorkflowResult.php create mode 100644 app/proto/workflows/common/workflows.proto create mode 100755 bin/buf-generate.sh create mode 100644 buf.gen.yaml create mode 100644 buf.work.yaml create mode 100644 buf.yaml diff --git a/app/composer.json b/app/composer.json index a2d78ee..9780adc 100644 --- a/app/composer.json +++ b/app/composer.json @@ -7,7 +7,8 @@ "autoload": { "psr-4": { "Temporal\\Samples\\": "src", - "Temporal\\SampleUtils\\": "util-src" + "Temporal\\SampleUtils\\": "util-src", + "Workflows\\": "gen/proto/php/Workflows" } } } diff --git a/app/gen/proto/docs/index.md b/app/gen/proto/docs/index.md new file mode 100644 index 0000000..e5a0167 --- /dev/null +++ b/app/gen/proto/docs/index.md @@ -0,0 +1,67 @@ +# Protocol Documentation + + +## Table of Contents + +- [workflows/common/workflows.proto](#workflows_common_workflows-proto) + - [WorkflowResult](#workflows-common-v1-WorkflowResult) + +- [Scalar Value Types](#scalar-value-types) + + + + +

Top

+ +## workflows/common/workflows.proto + + + + + +### WorkflowResult + + + +| Field | Type | Label | Description | +| ----- | ---- | ----- | ----------- | +| id | [string](#string) | | | +| run_id | [string](#string) | | | +| started_at | [google.protobuf.Timestamp](#google-protobuf-Timestamp) | | | +| finished_at | [google.protobuf.Timestamp](#google-protobuf-Timestamp) | | | +| greeting | [string](#string) | | | + + + + + + + + + + + + + + + +## Scalar Value Types + +| .proto Type | Notes | C++ | Java | Python | Go | C# | PHP | Ruby | +| ----------- | ----- | --- | ---- | ------ | -- | -- | --- | ---- | +| double | | double | double | float | float64 | double | float | Float | +| float | | float | float | float | float32 | float | float | Float | +| int32 | Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint32 instead. | int32 | int | int | int32 | int | integer | Bignum or Fixnum (as required) | +| int64 | Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint64 instead. | int64 | long | int/long | int64 | long | integer/string | Bignum | +| uint32 | Uses variable-length encoding. | uint32 | int | int/long | uint32 | uint | integer | Bignum or Fixnum (as required) | +| uint64 | Uses variable-length encoding. | uint64 | long | int/long | uint64 | ulong | integer/string | Bignum or Fixnum (as required) | +| sint32 | Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int32s. | int32 | int | int | int32 | int | integer | Bignum or Fixnum (as required) | +| sint64 | Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int64s. | int64 | long | int/long | int64 | long | integer/string | Bignum | +| fixed32 | Always four bytes. More efficient than uint32 if values are often greater than 2^28. | uint32 | int | int | uint32 | uint | integer | Bignum or Fixnum (as required) | +| fixed64 | Always eight bytes. More efficient than uint64 if values are often greater than 2^56. | uint64 | long | int/long | uint64 | ulong | integer/string | Bignum | +| sfixed32 | Always four bytes. | int32 | int | int | int32 | int | integer | Bignum or Fixnum (as required) | +| sfixed64 | Always eight bytes. | int64 | long | int/long | int64 | long | integer/string | Bignum | +| bool | | bool | boolean | boolean | bool | bool | boolean | TrueClass/FalseClass | +| string | A string must always contain UTF-8 encoded or 7-bit ASCII text. | string | String | str/unicode | string | string | string | String (UTF-8) | +| bytes | May contain any arbitrary sequence of bytes. | string | ByteString | str | []byte | ByteString | string | String (ASCII-8BIT) | + diff --git a/app/gen/proto/jsonschema/json/workflows.common.v1/WorkflowResult.json b/app/gen/proto/jsonschema/json/workflows.common.v1/WorkflowResult.json new file mode 100644 index 0000000..54e5480 --- /dev/null +++ b/app/gen/proto/jsonschema/json/workflows.common.v1/WorkflowResult.json @@ -0,0 +1,29 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "$ref": "#/definitions/WorkflowResult", + "definitions": { + "WorkflowResult": { + "properties": { + "id": { + "type": "string" + }, + "run_id": { + "type": "string" + }, + "started_at": { + "type": "string", + "format": "date-time" + }, + "finished_at": { + "type": "string", + "format": "date-time" + }, + "greeting": { + "type": "string" + } + }, + "additionalProperties": true, + "type": "object" + } + } +} \ No newline at end of file diff --git a/app/gen/proto/php/Workflows/Common/V1/GPBMetadata/Workflows.php b/app/gen/proto/php/Workflows/Common/V1/GPBMetadata/Workflows.php new file mode 100644 index 0000000..8bedc05 --- /dev/null +++ b/app/gen/proto/php/Workflows/Common/V1/GPBMetadata/Workflows.php @@ -0,0 +1,36 @@ +internalAddGeneratedFile( + ' +§ + workflows/common/workflows.protoworkflows.common.v1"Ë +WorkflowResult +id ( Rid +run_id ( RrunId9 + +started_at ( 2.google.protobuf.TimestampR startedAt; + finished_at ( 2.google.protobuf.TimestampR +finishedAt +greeting ( RgreetingB— +com.workflows.common.v1BWorkflowsProtoP¢WCXªWorkflows.Common.V1ÊWorkflows\\Common\\V1âWorkflows\\Common\\V1\\GPBMetadataêWorkflows::Common::V1bproto3' + , true); + + static::$is_initialized = true; + } +} + diff --git a/app/gen/proto/php/Workflows/Common/V1/WorkflowResult.php b/app/gen/proto/php/Workflows/Common/V1/WorkflowResult.php new file mode 100644 index 0000000..36a0ac4 --- /dev/null +++ b/app/gen/proto/php/Workflows/Common/V1/WorkflowResult.php @@ -0,0 +1,186 @@ +workflows.common.v1.WorkflowResult + */ +class WorkflowResult extends \Google\Protobuf\Internal\Message +{ + /** + * Generated from protobuf field string id = 1 [json_name = "id"]; + */ + protected $id = ''; + /** + * Generated from protobuf field string run_id = 2 [json_name = "runId"]; + */ + protected $run_id = ''; + /** + * Generated from protobuf field .google.protobuf.Timestamp started_at = 3 [json_name = "startedAt"]; + */ + protected $started_at = null; + /** + * Generated from protobuf field .google.protobuf.Timestamp finished_at = 4 [json_name = "finishedAt"]; + */ + protected $finished_at = null; + /** + * Generated from protobuf field string greeting = 5 [json_name = "greeting"]; + */ + protected $greeting = ''; + + /** + * Constructor. + * + * @param array $data { + * Optional. Data for populating the Message object. + * + * @type string $id + * @type string $run_id + * @type \Google\Protobuf\Timestamp $started_at + * @type \Google\Protobuf\Timestamp $finished_at + * @type string $greeting + * } + */ + public function __construct($data = NULL) { + \Workflows\Common\V1\GPBMetadata\Workflows::initOnce(); + parent::__construct($data); + } + + /** + * Generated from protobuf field string id = 1 [json_name = "id"]; + * @return string + */ + public function getId() + { + return $this->id; + } + + /** + * Generated from protobuf field string id = 1 [json_name = "id"]; + * @param string $var + * @return $this + */ + public function setId($var) + { + GPBUtil::checkString($var, True); + $this->id = $var; + + return $this; + } + + /** + * Generated from protobuf field string run_id = 2 [json_name = "runId"]; + * @return string + */ + public function getRunId() + { + return $this->run_id; + } + + /** + * Generated from protobuf field string run_id = 2 [json_name = "runId"]; + * @param string $var + * @return $this + */ + public function setRunId($var) + { + GPBUtil::checkString($var, True); + $this->run_id = $var; + + return $this; + } + + /** + * Generated from protobuf field .google.protobuf.Timestamp started_at = 3 [json_name = "startedAt"]; + * @return \Google\Protobuf\Timestamp|null + */ + public function getStartedAt() + { + return isset($this->started_at) ? $this->started_at : null; + } + + public function hasStartedAt() + { + return isset($this->started_at); + } + + public function clearStartedAt() + { + unset($this->started_at); + } + + /** + * Generated from protobuf field .google.protobuf.Timestamp started_at = 3 [json_name = "startedAt"]; + * @param \Google\Protobuf\Timestamp $var + * @return $this + */ + public function setStartedAt($var) + { + GPBUtil::checkMessage($var, \Google\Protobuf\Timestamp::class); + $this->started_at = $var; + + return $this; + } + + /** + * Generated from protobuf field .google.protobuf.Timestamp finished_at = 4 [json_name = "finishedAt"]; + * @return \Google\Protobuf\Timestamp|null + */ + public function getFinishedAt() + { + return isset($this->finished_at) ? $this->finished_at : null; + } + + public function hasFinishedAt() + { + return isset($this->finished_at); + } + + public function clearFinishedAt() + { + unset($this->finished_at); + } + + /** + * Generated from protobuf field .google.protobuf.Timestamp finished_at = 4 [json_name = "finishedAt"]; + * @param \Google\Protobuf\Timestamp $var + * @return $this + */ + public function setFinishedAt($var) + { + GPBUtil::checkMessage($var, \Google\Protobuf\Timestamp::class); + $this->finished_at = $var; + + return $this; + } + + /** + * Generated from protobuf field string greeting = 5 [json_name = "greeting"]; + * @return string + */ + public function getGreeting() + { + return $this->greeting; + } + + /** + * Generated from protobuf field string greeting = 5 [json_name = "greeting"]; + * @param string $var + * @return $this + */ + public function setGreeting($var) + { + GPBUtil::checkString($var, True); + $this->greeting = $var; + + return $this; + } + +} + diff --git a/app/proto/workflows/common/workflows.proto b/app/proto/workflows/common/workflows.proto new file mode 100644 index 0000000..4462afa --- /dev/null +++ b/app/proto/workflows/common/workflows.proto @@ -0,0 +1,15 @@ +syntax = "proto3"; + +package workflows.common.v1; + +import "google/protobuf/timestamp.proto"; + +message WorkflowResult { + string id = 1; + string run_id = 2; + + google.protobuf.Timestamp started_at = 3; + google.protobuf.Timestamp finished_at = 4; + + string greeting = 5; +} \ No newline at end of file diff --git a/app/src/SimpleActivity/ExecuteCommand.php b/app/src/SimpleActivity/ExecuteCommand.php index 369ec88..460ee5d 100644 --- a/app/src/SimpleActivity/ExecuteCommand.php +++ b/app/src/SimpleActivity/ExecuteCommand.php @@ -27,7 +27,9 @@ public function execute(InputInterface $input, OutputInterface $output) { $workflow = $this->workflowClient->newWorkflowStub( GreetingWorkflowInterface::class, - WorkflowOptions::new()->withWorkflowExecutionTimeout(CarbonInterval::minute()) + WorkflowOptions::new() + ->withWorkflowExecutionTimeout(CarbonInterval::minute()) + ->withCronSchedule('@every 10s') ); $output->writeln("Starting GreetingWorkflow... "); diff --git a/app/src/SimpleActivity/GreetingWorkflow.php b/app/src/SimpleActivity/GreetingWorkflow.php index 6dfa143..7b47647 100644 --- a/app/src/SimpleActivity/GreetingWorkflow.php +++ b/app/src/SimpleActivity/GreetingWorkflow.php @@ -14,6 +14,8 @@ use Carbon\CarbonInterval; use Temporal\Activity\ActivityOptions; use Temporal\Workflow; +use Workflows\Common\V1\WorkflowResult; +use Google\Protobuf\Timestamp; // @@@SNIPSTART php-hello-workflow @@ -36,8 +38,41 @@ public function __construct() public function greet(string $name): \Generator { + $start = Workflow::now(); + + /** + * @var WorkflowResult $previousGreeting + */ + $previousGreeting = Workflow::getLastCompletionResult(); + + if ( $previousGreeting !== null ) { + $name .= "$name - last greeting was: {$previousGreeting->getGreeting()}, started at '{$previousGreeting->getStartedAt()}', completed at '{$previousGreeting->getFinishedAt()}'"; + } + // This is a blocking call that returns only after the activity has completed. - return yield $this->greetingActivity->composeGreeting('Hello', $name); + $greeting = yield $this->greetingActivity->composeGreeting("Hello", $name); + + $end = Workflow::now(); + + return Workflow::async( + function() use($start, $end, $greeting) { + return (new WorkflowResult()) + ->setRunId('1') + ->setId('1') + ->setStartedAt( + (new Timestamp()) + ->setSeconds($start->getTimestamp()) + ->setNanos(0) + ) + ->setFinishedAt( + (new Timestamp()) + ->setSeconds($end->getTimestamp()) + ->setNanos(0) + ) + ->setGreeting($greeting) + ; + } + ); } } // @@@SNIPEND diff --git a/app/src/SimpleActivity/GreetingWorkflowInterface.php b/app/src/SimpleActivity/GreetingWorkflowInterface.php index 2704d06..2b01b89 100644 --- a/app/src/SimpleActivity/GreetingWorkflowInterface.php +++ b/app/src/SimpleActivity/GreetingWorkflowInterface.php @@ -14,13 +14,14 @@ // @@@SNIPSTART php-hello-workflow-interface use Temporal\Workflow\WorkflowInterface; use Temporal\Workflow\WorkflowMethod; +use Workflows\Common\V1\WorkflowResult; #[WorkflowInterface] interface GreetingWorkflowInterface { /** * @param string $name - * @return string + * @return WorkflowResult */ #[WorkflowMethod(name: "SimpleActivity.greet")] public function greet( diff --git a/bin/buf-generate.sh b/bin/buf-generate.sh new file mode 100755 index 0000000..241ee9b --- /dev/null +++ b/bin/buf-generate.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +docker run \ + --rm \ + --interactive \ + --tty \ + --volume "${PWD}":/workspace \ + --workdir /workspace \ + docker.artifactory.futurenet.com/futureapis/protoc-plugins/buf:latest \ + generate --debug --template buf.gen.yaml \ No newline at end of file diff --git a/buf.gen.yaml b/buf.gen.yaml new file mode 100644 index 0000000..aaf1634 --- /dev/null +++ b/buf.gen.yaml @@ -0,0 +1,15 @@ +version: v1 +managed: + enabled: true +plugins: + - name: php + out: app/gen/proto/php + strategy: all + - name: doc + out: app/gen/proto/docs + opt: markdown,index.md + strategy: all + - name: jsonschema + out: app/gen/proto/jsonschema/json + opt: prefix_schema_files_with_package,proto_fieldnames + strategy: all diff --git a/buf.work.yaml b/buf.work.yaml new file mode 100644 index 0000000..f30d0a8 --- /dev/null +++ b/buf.work.yaml @@ -0,0 +1,3 @@ +version: v1 +directories: + - app/proto \ No newline at end of file diff --git a/buf.yaml b/buf.yaml new file mode 100644 index 0000000..5fc4486 --- /dev/null +++ b/buf.yaml @@ -0,0 +1,7 @@ +version: v1 +lint: + use: + - DEFAULT +breaking: + use: + - FILE