During this exercise, you will:
- Define and handle a Signal
- Retrieve a handle on the Workflow to Signal
- Use a Temporal Client to send the Signal
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.
In this part of the exercise, you will define your Signal.
- Edit the
workflow.go
file. - Between the
import
andWorkflow()
blocks, define a new Signal typestruct
namedFulfillOrderSignal
. It should contain a single variable, abool
, namedFulfilled
. - Save the file.
You will now handle the Signal you defined in part A, and let the Workflow know what to do when it encounters the FulfillOrderSignal
.
- In
workflow.go
, locate thesignalChan.Receive()
call. This will block the Workflow until a Signal is received. - Wrap the
ExecuteActivity()
call and thelogger.Info()
call in a test forif signal.Fulfilled == true
. - Save the file.
In this part of the exercise, you will create another Temporal client that sends a Signal. signalclient/main.go
currently contains a near-empty Temporal client -- it looks like a Starter or a Worker, but it does not register any Workflows or do anything. You will use this client to send a Signal.
- Edit the
signalclient/main.go
file. - Within the
main()
block, use theFullfillOrderSignal
struct type from thesignals
module (i.e., theworkflow.go
file in the parent directory) to create an instance ofFulfillOrderSignal
that containsFulfilled: true
. - Save the file.
Now you will add the SignalWorkflow()
call itself.
- Continue editing the
signalclient/main.go
file. - Within the
main()
block, after you create an instance ofFulfillOrderSignal
, callSignalWorkflow()
to send a Signal to your running Workflow. It needs, as arguments,context.Background()
, your workflow ID, your run ID (which can be an empty string), the name of the signal, and the signal instance. It should assign its result toerr
so that it can be checked in the next line. - Save the file.
At this point, you can run your Workflow. It should run normally but wait to receive a Signal.
- In one terminal, navigate to the
worker
subdirectory and rungo run main.go
. - In another terminal, navigate to the
starter
subdirectory and rungo run main.go
. You should receive some logging from your Worker along these lines:
2024/03/14 08:48:10 INFO No logger configured for temporal client. Created default one.
2024/03/14 08:48:10 INFO Started Worker Namespace default TaskQueue signals WorkerID 43388@Omelas@
2024/03/14 08:48:21 INFO Signal workflow started Namespace default TaskQueue signals WorkerID 43388@Omelas@ WorkflowType Workflow WorkflowID signals RunID 905330da-9c0f-490e-bd48-c6a9e8840f7a Attempt 1 input Plain text input
- Your Workflow will not return as you added a blocking Signal call. In the next step, you will Signal your workflow.
- In a third terminal, navigate to the
signalclient
subdirectory and rungo run main.go
. It will send a Signal to your Workflow. This should cause your Workflow to return, and theSignal workflow completed
call to be logged in the terminal running your Worker:
2024/03/14 08:48:37 INFO Signal workflow completed. Namespace default TaskQueue signals WorkerID 43388@Omelas@ WorkflowType Workflow WorkflowID signals RunID 905330da-9c0f-490e-bd48-c6a9e8840f7a Attempt 1 result Received Plain text input
- You have successfully sent a Signal from a Temporal Client to a running Workflow.