A Worker is a service that executes Workflows and Activities.
Workers are run on user controlled hosts.
You can use the
worker package to create and run as many Workers as your use case demands, across any number of hosts.
Workers poll Task Queues for Tasks, execute chunks of code in response to those Tasks, and then communicate the results back to the Temporal Server.
As a developer, running Workers is a fairly simple procedure, because the Go SDK handles all of the communication between the Worker and the Temporal Server behind the scenes.
To start a Worker you need to pass it the following three things:
- The Temporal Go SDK client.
- The name of the Task Queue that it will poll.
- The Worker's options, which can be empty by default.
To run the Worker, you need to first register which Workflows and/or Activities it can execute and then pass it an interrupt channel.
As a simple example, let's say we want our Worker to be able to execute the following Go functions:
This is how you would create and run the Worker for those functions:
RegisterActivity calls essentially create an in-memory mapping between the fully qualified function names and their implementations, inside the Worker process.
If the Worker polls a Task for a Workflow or Activity function it does not know about, it will fail that Task. However, the failure of the Task will not cause the associated Workflow to fail.
When you start a Workflow by calling
ExecuteWorkflow(), the Temporal Server adds a new Task to the Workflow's Task Queue, and any Worker polling that Task Queue could execute that Task.