forked from temporalio/samples-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workflow.go
57 lines (46 loc) · 1.74 KB
/
workflow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// @@@SNIPSTART samples-go-schedule-workflow
package schedule
import (
"context"
"time"
"go.temporal.io/sdk/activity"
"go.temporal.io/sdk/converter"
"go.temporal.io/sdk/workflow"
)
// SampleScheduleWorkflow executes on the given schedule
func SampleScheduleWorkflow(ctx workflow.Context) error {
workflow.GetLogger(ctx).Info("Schedule workflow started.", "StartTime", workflow.Now(ctx))
ao := workflow.ActivityOptions{
StartToCloseTimeout: 10 * time.Second,
}
ctx1 := workflow.WithActivityOptions(ctx, ao)
info := workflow.GetInfo(ctx1)
// Workflow Executions started by a Schedule have the following additional properties appended to their search attributes
//lint:ignore SA1019 - this is a sample
scheduledByIDPayload := info.SearchAttributes.IndexedFields["TemporalScheduledById"]
var scheduledByID string
err := converter.GetDefaultDataConverter().FromPayload(scheduledByIDPayload, &scheduledByID)
if err != nil {
return err
}
//lint:ignore SA1019 - this is a sample
startTimePayload := info.SearchAttributes.IndexedFields["TemporalScheduledStartTime"]
var startTime time.Time
err = converter.GetDefaultDataConverter().FromPayload(startTimePayload, &startTime)
if err != nil {
return err
}
err = workflow.ExecuteActivity(ctx1, DoSomething, scheduledByID, startTime).Get(ctx, nil)
if err != nil {
workflow.GetLogger(ctx).Error("schedule workflow failed.", "Error", err)
return err
}
return nil
}
// DoSomething is an Activity
func DoSomething(ctx context.Context, scheduleByID string, startTime time.Time) error {
activity.GetLogger(ctx).Info("Schedulde job running.", "scheduleByID", scheduleByID, "startTime", startTime)
// Query database, call external API, or do any other non-deterministic action.
return nil
}
// @@@SNIPEND