-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathworkflow.go
57 lines (46 loc) · 1.62 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-cron-workflow
package cron
import (
"context"
"time"
"go.temporal.io/sdk/activity"
"go.temporal.io/sdk/workflow"
)
// CronResult is used to return data from one cron run to the next
type CronResult struct {
RunTime time.Time
}
// SampleCronWorkflow executes on the given schedule
// The schedule is provided when starting the Workflow
func SampleCronWorkflow(ctx workflow.Context) (*CronResult, error) {
workflow.GetLogger(ctx).Info("Cron workflow started.", "StartTime", workflow.Now(ctx))
ao := workflow.ActivityOptions{
StartToCloseTimeout: 10 * time.Second,
}
ctx1 := workflow.WithActivityOptions(ctx, ao)
// Start from 0 for first cron job
lastRunTime := time.Time{}
// Check to see if there was a previous cron job
if workflow.HasLastCompletionResult(ctx) {
var lastResult CronResult
if err := workflow.GetLastCompletionResult(ctx, &lastResult); err == nil {
lastRunTime = lastResult.RunTime
}
}
thisRunTime := workflow.Now(ctx)
err := workflow.ExecuteActivity(ctx1, DoSomething, lastRunTime, thisRunTime).Get(ctx, nil)
if err != nil {
// Cron job failed
// Next cron will still be scheduled by the Server
workflow.GetLogger(ctx).Error("Cron job failed.", "Error", err)
return nil, err
}
return &CronResult{RunTime: thisRunTime}, nil
}
// DoSomething is an Activity
func DoSomething(ctx context.Context, lastRunTime, thisRunTime time.Time) error {
activity.GetLogger(ctx).Info("Cron job running.", "lastRunTime_exclude", lastRunTime, "thisRunTime_include", thisRunTime)
// Query database, call external API, or do any other non-deterministic action.
return nil
}
// @@@SNIPEND