@@ -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.
0 commit comments