During this exercise, you will:
- Define and handle a Signal
- Retrieve a handle on the Workflow to Signal
- Send an external Signal
- Use a Temporal Client to submit execution requests for both Workflows
Make your changes to the code in the practice
subdirectory (look for
TODO
comments that will guide you to where you should make changes to
the code). If you need a hint or want to verify your changes, look at
the complete version in the solution
subdirectory.
- This exercise contains one Client that runs two different Workflows
—
PizzaWorkflow
andFulfillOrderWorkflow
. Both Workflows are defined inworkflow.go
.PizzaWorkflow
is designed not to complete its final activity —SendBill
— until it receives a Signal fromFulfillOrderWorkflow
. You'll start by defining that Signal. Editworkflow.go
. Near the top of the file, after theimport()
block and before your Workflow definitions, create a type ofstruct{}
namedFulfillOrderSignal
that contains a singlebool
namedFulfilled
. - Next, directly below that, create a
var
namedsignal
that is an instance ofFulfillOrderSignal
withFulfilled: true
. This is the Signal thatFulfillOrderWorkflow
will send toPizzaWorkflow
. - Save the file.
- Next, you need to enable your
PizzaWorkflow
to receive a Signal fromFulfillOrderWorkflow
. Aftervar confirmation OrderConfirmation
, define a Signal Channel, and usesignalChan.Receive()
to block the Workflow until it receives a Signal, after which it can proceed with the logic contained inif signal.Fulfilled == true{}
. Begin by adding a call toworkflow.GetSignalChannel(ctx, "fulfill-order-signal")
and assign it to a variable likesignalChan
. - After that, add
signalChan.Receive(ctx, &signal)
on the following line. - Save the file.
- Near the bottom of
workflow.go
, withinFulfillOrderWorkflow
, you will notice that it runs two Activities —MakePizzas
andDeliverPizzas
. After those Activities complete successfuly, the next step should be to send a Signal to thePizzaWorkflow
that it is time to bill the customer and complete the Workflow. To do this, you need callworkflow.SignalExternalWorkflow()
. - Add this call to the end of
FulfillOrderWorkflow
.SignalExternalWorkflow
needs, as arguments, thectx
Workflow context, Workflow ID (which should bepizza-workflow-order-Z1238
), an optional Run ID (which you can omit by providing "" as the next argument), and the name of the Signal,fulfill-order-signal
. ForSignalExternalWorkflow
calls to block and return properly in Go, you also need to append.Get(ctx, [return-value-pointer])
to aSignalExternalWorkflow
call, though[return-value-pointer]
can benil
here. - Save and close the file.
- Finally, open
start/main.go
for editing. Currently, this Client only starts thePizzaWorkflow
. Directly after thec.ExecuteWorkflow()
call for thePizzaWorkflow
, add another call that starts theFulfillOrderWorkflow
. You can use the call that starts thePizzaWorkflow
and thesignalFulfilledOptions
block as a reference. Don't forget to capture the Workflow Execution and any errors in different variables. - Save and close the file.
At this point, you can run your Workflows.
-
In one terminal, navigate to the
worker
subdirectory and rungo run main.go
. -
In another terminal, navigate to the
start
subdirectory and rungo run main.go
. You should receive output from both Workflows having started and returning the expected result:2024/04/12 10:41:11 Started workflow WorkflowID pizza-workflow-order-Z1238 RunID d41177c4-ffe6-4f51-a884-7fefb3e13cff 2024/04/12 10:41:11 Started workflow WorkflowID signal-fulfilled-order-Z1238 RunID 9d5168e9-1c58-41c3-aca2-ea8182dff11d 2024/04/12 10:41:11 Workflow result: { "OrderNumber": "Z1238", "Status": "SUCCESS", "ConfirmationNumber": "AB9923", "BillingTimestamp": 1712943671, "Amount": 2700 }
-
If you look at the terminal running your Worker, you should see logging from each individual step run by both Workflows, including the Signal being sent and all the related activities:
... 2024/04/12 10:41:11 INFO Starting delivery Namespace default TaskQueue pizza-tasks WorkerID 35880@ted.local@ ActivityID 11 ActivityType DeliverPizzas Attempt 1 WorkflowType FulfillOrderWorkflow WorkflowID signal-fulfilled-order-Z1238 RunID 9d5168e9-1c58-41c3-aca2-ea8182dff11d Z1238 to {701 Mission Street Apartment 9C San Francisco CA 94103} 2024/04/12 10:41:11 INFO Z1238 Namespace default TaskQueue pizza-tasks WorkerID 35880@ted.local@ ActivityID 11 ActivityType DeliverPizzas Attempt 1 WorkflowType FulfillOrderWorkflow WorkflowID signal-fulfilled-order-Z1238 RunID 9d5168e9-1c58-41c3-aca2-ea8182dff11d delivered. ...