Skip to content

Commit

Permalink
Add schedule example
Browse files Browse the repository at this point in the history
closes #75
  • Loading branch information
cludden committed Aug 10, 2024
1 parent df64157 commit 39ef5b1
Show file tree
Hide file tree
Showing 49 changed files with 1,979 additions and 162 deletions.
1 change: 1 addition & 0 deletions buf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ modules:
- path: examples/example/proto
- path: examples/helloworld/proto
- path: examples/mutex/proto
- path: examples/schedule/proto
- path: examples/searchattributes/proto
- path: examples/updatabletimer/proto
- path: examples/xns/proto
Expand Down
32 changes: 32 additions & 0 deletions docs/docs/examples/schedule.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import CodeBlock from '@theme/CodeBlock';
import Proto from '!!raw-loader!../../../examples/schedule/proto/example/schedule/v1/schedule.proto';
import Implementation from '!!raw-loader!../../../examples/schedule/main.go';


# Schedule

A simple example showcasing usage of proto workflwos with Temporal [schedules](https://temporal.io/blog/temporal-schedules-reliable-scalable-and-more-flexible-than-cron-jobs).

<CodeBlock language="protobuf" title="example.proto">{Proto}</CodeBlock>

<CodeBlock language="go" title="main.go">{Implementation}</CodeBlock>


## Run this example

1. Clone the examples
```sh
git clone https://github.com/cludden/protoc-gen-go-temporal && cd protoc-gen-go-temporal
```
2. Start temporal
```shell
temporal server start-dev
```
3. In a different terminal, run the worker
```shell
go run ./examples/schedule/... worker
```
4. In a different terminal, create a schedule
```shell
go run ./examples/schedule/... create-schedule
```
5 changes: 5 additions & 0 deletions docs/sidebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ const sidebars: SidebarsConfig = {
id: 'examples/mutex',
label: 'Mutex'
},
{
type: 'doc',
id: 'examples/schedule',
label: 'Schedule'
},
{
type: 'doc',
id: 'examples/searchattributes',
Expand Down
100 changes: 100 additions & 0 deletions examples/schedule/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package main

import (
"fmt"
"log"
"os"
"time"

schedulev1 "github.com/cludden/protoc-gen-go-temporal/gen/example/schedule/v1"
"github.com/urfave/cli/v2"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/worker"
"go.temporal.io/sdk/workflow"
"google.golang.org/protobuf/types/known/timestamppb"
)

type (
Workflows struct{}

ScheduleWorkflow struct {
*schedulev1.ScheduleWorkflowInput
}
)

func main() {
app, err := schedulev1.NewExampleCli(
schedulev1.NewExampleCliOptions().
WithClient(withClient).
WithWorker(func(cmd *cli.Context, c client.Client) (worker.Worker, error) {
w := worker.New(c, schedulev1.ExampleTaskQueue, worker.Options{})
schedulev1.RegisterExampleWorkflows(w, &Workflows{})
return w, nil
}),
)
if err != nil {
log.Fatal(err)
}

app.Commands = append(app.Commands, &cli.Command{
Name: "create-schedule",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Usage: "schedule id",
Value: "schedule_id",
},
},
Action: func(cmd *cli.Context) error {
c, err := withClient(cmd)
if err != nil {
return err
}
defer c.Close()

h, err := c.ScheduleClient().Create(cmd.Context, client.ScheduleOptions{
ID: cmd.String("id"),
Spec: client.ScheduleSpec{
Intervals: []client.ScheduleIntervalSpec{{
Every: time.Minute,
}},
},
Action: &client.ScheduleWorkflowAction{
ID: fmt.Sprintf("%s/", schedulev1.ScheduleWorkflowName),
Workflow: schedulev1.ScheduleWorkflowName,
TaskQueue: schedulev1.ExampleTaskQueue,
Args: []any{
&schedulev1.ScheduleInput{},
},
},
})
if err != nil {
return err
}
fmt.Printf("schedule created: %s\n", h.GetID())
return nil
},
})

if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}

func (w *Workflows) Schedule(ctx workflow.Context, input *schedulev1.ScheduleWorkflowInput) (schedulev1.ScheduleWorkflow, error) {
return &ScheduleWorkflow{input}, nil
}

func (w *ScheduleWorkflow) Execute(ctx workflow.Context) (*schedulev1.ScheduleOutput, error) {
return &schedulev1.ScheduleOutput{
StartedAt: timestamppb.New(workflow.Now(ctx)),
}, nil
}

func withClient(cmd *cli.Context) (client.Client, error) {
c, err := client.DialContext(cmd.Context, client.Options{})
if err != nil {
return nil, err
}
return c, nil
}
22 changes: 22 additions & 0 deletions examples/schedule/proto/example/schedule/v1/schedule.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
syntax = "proto3";

package example.schedule.v1;

import "google/protobuf/timestamp.proto";
import "temporal/v1/temporal.proto";

service Example {
option (temporal.v1.service).task_queue = "schedule-v1";

rpc Schedule(ScheduleInput) returns (ScheduleOutput) {
option (temporal.v1.workflow) = {
name: "example.schedule.v1.Schedule"
};
}
}

message ScheduleInput {}

message ScheduleOutput {
google.protobuf.Timestamp started_at = 1;
}
10 changes: 5 additions & 5 deletions gen/example/helloworld/v1/example.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions gen/example/helloworld/v1/example_temporal.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions gen/example/mutex/v1/mutex.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions gen/example/mutex/v1/mutex_temporal.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions gen/example/mutex/v1/mutexv1xns/mutex_xns_temporal.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 39ef5b1

Please sign in to comment.