Skip to content
This repository was archived by the owner on Aug 15, 2021. It is now read-only.

Commit a8355a4

Browse files
committed
Workflow description based status
Use the workflow description api for status, as an example
1 parent 8452d0a commit a8355a4

File tree

4 files changed

+46
-50
lines changed

4 files changed

+46
-50
lines changed

workflow/api.go

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ import (
1010

1111
"github.com/go-zoo/bone"
1212
"github.com/gofrs/uuid"
13+
"github.com/gogo/status"
14+
"go.temporal.io/api/enums/v1"
1315
"go.temporal.io/sdk/client"
16+
"google.golang.org/grpc/codes"
1417
)
1518

16-
func Api(c client.Client, r *Repository) http.Handler {
19+
func Api(c client.Client) http.Handler {
1720
mux := bone.New()
1821

1922
// API to launch an workflow. could be an incoming webhook,
@@ -34,7 +37,7 @@ func Api(c client.Client, r *Repository) http.Handler {
3437
TaskQueue: PRCheckTaskQueue,
3538
}
3639

37-
work, err := c.ExecuteWorkflow(context.Background(), options, (&CheckPR{}).CheckPR, details)
40+
work, err := c.ExecuteWorkflow(context.Background(), options, CheckPR, details)
3841
if err != nil {
3942
rw.WriteHeader(http.StatusInternalServerError)
4043
rw.Write([]byte("couldn't enqueue"))
@@ -50,26 +53,50 @@ func Api(c client.Client, r *Repository) http.Handler {
5053

5154
// Get the status of a given job
5255
mux.Get("/jobs/:id", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
56+
ctx := req.Context()
57+
5358
id := bone.GetValue(req, "id")
5459

5560
rw.Header().Add("Content-Type", "application/json")
56-
enc := json.NewEncoder(rw)
5761

58-
var s PRStatus
59-
err := r.Get(req.Context(), id, &s)
60-
switch err {
61-
case nil:
62-
rw.WriteHeader(http.StatusOK)
63-
enc.Encode(&s)
64-
case ErrNotFound:
65-
// TODO: ask temporal here, in case it is PENDING.
62+
// Does the requested workflow execution exist at all?
63+
desc, err := c.DescribeWorkflowExecution(ctx, id, "")
64+
switch {
65+
case err == nil:
66+
case status.Code(err) == codes.NotFound: // XXX: this isn't the right check
6667
rw.WriteHeader(http.StatusNotFound)
67-
enc.Encode("not found")
68+
fmt.Fprint(rw, "{}")
69+
return
6870
default:
6971
rw.WriteHeader(http.StatusInternalServerError)
70-
enc.Encode("error")
71-
fmt.Println(err)
72+
fmt.Fprint(rw, "{}")
73+
return
7274
}
75+
76+
// You could check the execution "memo" here for
77+
// thinks like an org id for AuthZ.
78+
79+
var out struct {
80+
Status string `json:"status"`
81+
}
82+
83+
// Figure out what our status is.
84+
// As soon as the work is added to the queue, its status
85+
// will be running (so "pending" won't work here).
86+
switch desc.WorkflowExecutionInfo.Status {
87+
case enums.WORKFLOW_EXECUTION_STATUS_COMPLETED:
88+
out.Status = "completed"
89+
case enums.WORKFLOW_EXECUTION_STATUS_RUNNING, enums.WORKFLOW_EXECUTION_STATUS_CONTINUED_AS_NEW:
90+
out.Status = "running"
91+
case enums.WORKFLOW_EXECUTION_STATUS_TIMED_OUT:
92+
out.Status = "timed_out"
93+
default: // cancelled, errored, etc
94+
out.Status = "errored"
95+
}
96+
97+
rw.WriteHeader(http.StatusOK)
98+
enc := json.NewEncoder(rw)
99+
enc.Encode(out)
73100
}))
74101

75102
// API to complete an activity.

workflow/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ require (
77
github.com/go-zoo/bone v1.3.0
88
github.com/gofrs/uuid v4.0.0+incompatible
99
go.temporal.io/sdk v1.8.0
10+
google.golang.org/grpc v1.37.0
1011
)

workflow/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"log"
55
"net/http"
6+
"time"
67

78
"go.temporal.io/sdk/client"
89
"go.temporal.io/sdk/worker"
@@ -29,6 +30,8 @@ func main() {
2930
log.Fatalln(err)
3031
}()
3132

33+
time.Sleep(2 * time.Minute)
34+
3235
// This worker hosts both Worker and Activity functions
3336
w := worker.New(c, PRCheckTaskQueue, worker.Options{})
3437

workflow/workflow.go

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,13 @@ type CheckDetails struct {
2222
New string
2323
}
2424

25-
type CheckPR struct {
26-
r *Repository
27-
}
28-
29-
func (cpr *CheckPR) CheckPR(ctx workflow.Context, details CheckDetails) error {
25+
func CheckPR(ctx workflow.Context, details CheckDetails) error {
3026
ctx = workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
3127
StartToCloseTimeout: time.Minute, // Max 1 minute before quitting
3228
})
3329

34-
ctx = workflow.WithLocalActivityOptions(ctx, workflow.LocalActivityOptions{
35-
StartToCloseTimeout: time.Minute, // Max 1 minute before quitting
36-
})
37-
38-
i := workflow.GetInfo(ctx)
39-
40-
// TODO: put status in error on failure.
41-
var status PRStatus
42-
4330
// Start both tests. We get back futures. They transparently handle
4431
// retries and persisting the results.
45-
status.Status = append([]PRStatusItem{{State: "testing", TimeStamp: workflow.Now(ctx), Description: "tests are running"}}, status.Status...)
46-
if err := workflow.ExecuteLocalActivity(ctx, cpr.r.Put, i.WorkflowExecution.ID, status).Get(ctx, nil); err != nil {
47-
return err
48-
}
49-
5032
old := workflow.ExecuteActivity(ctx, Test, details.Repo, details.Old)
5133
new := workflow.ExecuteActivity(ctx, Test, details.Repo, details.New)
5234

@@ -63,33 +45,16 @@ func (cpr *CheckPR) CheckPR(ctx workflow.Context, details CheckDetails) error {
6345

6446
// fast to do and deterministic. run inside the workflow.
6547
var diff string
66-
status.Status = append([]PRStatusItem{{State: "diffing", TimeStamp: workflow.Now(ctx), Description: "test results are diffing"}}, status.Status...)
67-
if err := workflow.ExecuteLocalActivity(ctx, cpr.r.Put, i.WorkflowExecution.ID, status).Get(ctx, nil); err != nil {
68-
return err
69-
}
70-
7148
err := workflow.ExecuteActivity(ctx, DiffResults, oldRes, newRes).Get(ctx, &diff)
7249
if err != nil {
7350
return err
7451
}
7552

7653
// Resolve the final task and finish.
77-
status.Status = append([]PRStatusItem{{State: "reporting", TimeStamp: workflow.Now(ctx), Description: "PR check results are being posted"}}, status.Status...)
78-
if err := workflow.ExecuteLocalActivity(ctx, cpr.r.Put, i.WorkflowExecution.ID, status).Get(ctx, nil); err != nil {
79-
return err
80-
}
81-
8254
if err := workflow.ExecuteActivity(ctx, SetCommitStatus, details.Repo, details.PR, diff).Get(ctx, nil); err != nil {
8355
return err
8456
}
8557

86-
// XXX: Does setting this final status make sense? how will query interact with the actual workflow completion status?
87-
// is there a race condition? probably doesn't matter.
88-
status.Status = append([]PRStatusItem{{State: "complete", TimeStamp: workflow.Now(ctx), Description: "All done"}}, status.Status...)
89-
if err := workflow.ExecuteLocalActivity(ctx, cpr.r.Put, i.WorkflowExecution.ID, status).Get(ctx, nil); err != nil {
90-
return err
91-
}
92-
9358
return nil
9459
}
9560

0 commit comments

Comments
 (0)