During this exercise, you will
- Create Workflow and Activity loggers
- Add logging statements to the code
- Add a Timer to the Workflow Definition
- Launch two Workers and run the Workflow
- Kill one of the Workers during Workflow Execution and observe that the remaining Worker completes the execution
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.
You'll need four terminal windows for this exercise.
- In all terminals, change to the
exercises/durable-execution/practicedirectory. - In one terminal, run
npm installto install dependencies.
- Edit the
src/workflows.tsfile - Add the
logpackage to theimport { proxyActivities } from '@temporalio/workflow';line. - Add a line at the top of the
sayHelloGoodbyeWorkflowfunction to log a message at the Info level- It should mention that the Workflow function has been invoked
- It should also include a name-value pair for the name passed as input
- Before each call to Execute Activity, log a message at Debug level
- This should should identify the word being translated
- It should also include a name-value pair for the language code passed as input
- Save your changes
- Edit the
src/activities.tsfile. - Add an import to the top of the file so you can access the logger:
import * as activity from '@temporalio/activity'package. - Define a new
contextvariable at the top of the Activity function and assign itactivity.Context.current()to get access to the logger. - Insert a logging statement using
context.logat the Info level just after this, so you'll know when the Activity is invoked.- Include the term being translated and the language code as name-value pairs.
- Optionally, add log statements at the Error level anywhere that the Activity returns an error.
- Near the bottom of the function, use the Debug level to log the successful translation
- Include the translated term as a name-value pair.
- Save your changes.
You will now add a Timer between the two Activity calls in the Workflow Definition, which will make it easier to observe durable execution in the next section.
- After the statement where
helloMessageis defined, but before the statement wheregoodbyeInputis defined, add a new statement that logs the messageSleeping between translation callsat the Debug level. - Add the
sleepfunction to theimport { proxyActivities } from '@temporalio/workflow';line. - Just after the new log statement, use
await sleep("10 seconds");to set a Timer for 10 seconds.
It is typical to run Temporal applications using two or more Worker processes. Not only do additional Workers allow the application to scale, it also increases availability since another Worker can take over if a Worker crashes during Workflow Execution. You'll see this for yourself now and will learn more about how Temporal achieves this as you continue through the course.
Before proceeding, make sure that there are no Workers running for this or any previous exercise. Also, please read through all of these instructions before you begin, so that you'll know when and how to react.
- Open
src/worker.ts - Observe that the logger is customized to print logs at the
DEBUGlevel rather than the defaultINFOlevel.
In each terminal, ensure you are in the exercises/durable-execution/practice directory.
- In one terminal, start the translation microservice by running
npm run service. - In another terminal, start the Worker by running
npm start - In another terminal, start a second Worker by running
npm start - In another terminal, execute the Workflow by running
npm run workflow Tatiana sk(replaceTatianawith your first name) - Observe the output in the terminal windows used by each worker.
- As soon as you see a log message in one of the Worker terminals indicating that it has started the Timer, press Ctrl-C in that window to kill that Worker process.
- Switch to the terminal window for the other Worker process. Within a few seconds, you should observe new output, indicating that it has resumed execution of the Workflow.
- Once you see log output indicating that translation was successful, switch back to the terminal window where you started the Workflow.
After the final step, you should see the translated Hello and Goodbye messages, which confirms that Workflow Execution completed successfully despite the original Worker being killed.
Since you added logging code to the Workflow and Activity code, take a moment to look at what you see in the terminal windows for each Worker and think about what took place. You may also find it helpful to look at this Workflow Execution in the Web UI.
The microservice for this exercise logs each successful translation, and if you look at its terminal window, you will see that the service only translated Hello (the first Activity) once, even though the Worker was killed after this translation took place. In other words, Temporal did not re-execute the completed Activity when it restored the state of the Workflow Execution.