# Run a Worker - Ruby SDK

> Create and run a Temporal Worker using the Ruby SDK.

## Create and run a Worker 

Create a `Temporalio::Worker` with a Temporal Client, the Task Queue to poll, and the Workflows and Activities it can execute.
Call `run` to start polling.

<!--SNIPSTART ruby-create-worker-->
[features/snippets/worker/worker.rb](https://github.com/temporalio/features/blob/main/features/snippets/worker/worker.rb)
```rb
worker = Temporalio::Worker.new(
  client: client,
  task_queue: 'my-task-queue',
  workflows: [GreetingWorkflow],
  activities: [SayHello]
)

worker.run
```
<!--SNIPEND-->

`run` blocks until the Worker shuts down.
To run several Workers in one process, use `Temporalio::Worker.run_all`.
It runs them until its cancellation is canceled or its optional block completes, then shuts all of them down.

## Register Workflows and Activities 

All Workers listening to the same Task Queue must be registered to handle the same Workflow Types and Activity Types.
If a Worker polls a Task for a type it does not know about, the Task fails. The Workflow Execution itself does not fail.

Pass Workflow classes in `workflows` and Activities in `activities`.
For Activities you can pass either the class or an instance:

```ruby
worker = Temporalio::Worker.new(
  client:,
  task_queue: 'my-task-queue',
  workflows: [GreetingWorkflow, OrderWorkflow],
  activities: [SayHello, ChargeCard.new(payment_client)]
)
```

Passing a class makes the Worker instantiate it for each Activity Execution.
Passing an instance reuses that object for every execution, which is how Activities share state such as a database client. A shared instance must be thread-safe.

## Connect to Temporal Cloud 

To run a Worker against Temporal Cloud, configure the Client connection with your Namespace address and authentication credentials.
See [Connect to Temporal Cloud](/develop/ruby/client/temporal-client#connect-to-temporal-cloud) for setup instructions.

## Configure Worker options 

`Temporalio::Worker.new` takes keyword arguments that control concurrency limits, pollers, timeouts, and caching, including `max_concurrent_activities`, `max_concurrent_workflow_tasks`, and `max_cached_workflows`.
The defaults work for most cases.

To tune these values against real load, see [Worker performance](/develop/worker-performance) and the [Worker tuning reference](/develop/worker-tuning-reference).

## Run a versioned Worker 

Set a Worker Deployment Version and enable versioning in `deployment_options`, then set a versioning behavior on each Workflow.

<!--SNIPSTART ruby-versioned-worker-->
[features/snippets/worker/worker.rb](https://github.com/temporalio/features/blob/main/features/snippets/worker/worker.rb)
```rb
worker = Temporalio::Worker.new(
  client: client,
  task_queue: 'my-task-queue',
  workflows: [VersionedGreetingWorkflow],
  activities: [SayHello],
  deployment_options: Temporalio::Worker::DeploymentOptions.new(
    version: Temporalio::WorkerDeploymentVersion.new(
      deployment_name: 'my-app',
      build_id: '1.0'
    ),
    use_worker_versioning: true
  )
)
```
<!--SNIPEND-->

Declare the behavior in the Workflow class with `workflow_versioning_behavior Temporalio::VersioningBehavior::PINNED` or `AUTO_UPGRADE`, or set a default for the whole Worker with `default_versioning_behavior` on `DeploymentOptions`.

A versioning behavior applies only to a Worker that has versioning enabled.
If a Workflow declares one and its Worker does not enable versioning, the server rejects the Workflow Task and the Task retries instead of failing outright.

See [Worker Versioning](/worker-versioning) for the available versioning behaviors and how new versions roll out.

## Shut down a Worker 

Pass the signals that should stop the Worker to `run`.
The Worker stops polling for new Tasks and waits for in-flight Tasks to finish.

<!--SNIPSTART ruby-worker-graceful-shutdown-->
[features/snippets/worker/worker.rb](https://github.com/temporalio/features/blob/main/features/snippets/worker/worker.rb)
```rb
worker.run(shutdown_signals: %w[SIGINT SIGTERM])
```
<!--SNIPEND-->

You can also pass a block to `run`, which shuts the Worker down when the block completes.
See [Worker shutdown](/encyclopedia/workers/worker-shutdown) for what happens to in-flight Workflow Tasks and Activities.
