Skip to content

Commit

Permalink
Close all explicitely created NativeConnection (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjameswh authored Nov 29, 2024
1 parent ac6e079 commit a07b78a
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 70 deletions.
41 changes: 23 additions & 18 deletions empty/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,30 @@ async function run() {
address: 'localhost:7233',
// TLS and gRPC metadata configuration goes here.
});
// Step 2: Register Workflows and Activities with the Worker.
const worker = await Worker.create({
connection,
namespace: 'default',
taskQueue: TASK_QUEUE_NAME,
// Workflows are registered using a path as they run in a separate JS context.
workflowsPath: require.resolve('./workflows'),
activities,
});
try {
// Step 2: Register Workflows and Activities with the Worker.
const worker = await Worker.create({
connection,
namespace: 'default',
taskQueue: TASK_QUEUE_NAME,
// Workflows are registered using a path as they run in a separate JS context.
workflowsPath: require.resolve('./workflows'),
activities,
});

// Step 3: Start accepting tasks on the Task Queue specified in TASK_QUEUE_NAME
//
// The worker runs until it encounters an unexpected error or the process receives a shutdown signal registered on
// the SDK Runtime object.
//
// By default, worker logs are written via the Runtime logger to STDERR at INFO level.
//
// See https://typescript.temporal.io/api/classes/worker.Runtime#install to customize these defaults.
await worker.run();
// Step 3: Start accepting tasks on the Task Queue specified in TASK_QUEUE_NAME
//
// The worker runs until it encounters an unexpected error or the process receives a shutdown signal registered on
// the SDK Runtime object.
//
// By default, worker logs are written via the Runtime logger to STDERR at INFO level.
//
// See https://typescript.temporal.io/api/classes/worker.Runtime#install to customize these defaults.
await worker.run();
} finally {
// Close the connection once the worker has stopped
await connection.close();
}
}

run().catch((err) => {
Expand Down
21 changes: 13 additions & 8 deletions food-delivery/apps/worker/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ import { namespace, getConnectionOptions } from 'common/lib/temporal-connection'

async function run() {
const connection = await NativeConnection.connect(getConnectionOptions())
const worker = await Worker.create({
workflowsPath: require.resolve('../../../packages/workflows/'),
activities,
connection,
namespace,
taskQueue,
})
try {
const worker = await Worker.create({
workflowsPath: require.resolve('../../../packages/workflows/'),
activities,
connection,
namespace,
taskQueue,
})

await worker.run()
await worker.run()
} finally {
// Close the connection once the worker has stopped
await connection.close()
}
}

run().catch((err) => {
Expand Down
25 changes: 14 additions & 11 deletions hello-world-mtls/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,21 @@ async function run({
},
},
});
try {
const worker = await Worker.create({
connection,
namespace,
workflowsPath: require.resolve('./workflows'),
activities,
taskQueue,
});
console.log('Worker connection successfully established');

const worker = await Worker.create({
connection,
namespace,
workflowsPath: require.resolve('./workflows'),
activities,
taskQueue,
});
console.log('Worker connection successfully established');

await worker.run();
await connection.close();
await worker.run();
} finally {
// Close the connection once the worker has stopped
await connection.close();
}
}

run(getEnv()).catch((err) => {
Expand Down
41 changes: 23 additions & 18 deletions hello-world/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,30 @@ async function run() {
address: 'localhost:7233',
// TLS and gRPC metadata configuration goes here.
});
// Step 2: Register Workflows and Activities with the Worker.
const worker = await Worker.create({
connection,
namespace: 'default',
taskQueue: 'hello-world',
// Workflows are registered using a path as they run in a separate JS context.
workflowsPath: require.resolve('./workflows'),
activities,
});
try {
// Step 2: Register Workflows and Activities with the Worker.
const worker = await Worker.create({
connection,
namespace: 'default',
taskQueue: 'hello-world',
// Workflows are registered using a path as they run in a separate JS context.
workflowsPath: require.resolve('./workflows'),
activities,
});

// Step 3: Start accepting tasks on the `hello-world` queue
//
// The worker runs until it encounters an unexpected error or the process receives a shutdown signal registered on
// the SDK Runtime object.
//
// By default, worker logs are written via the Runtime logger to STDERR at INFO level.
//
// See https://typescript.temporal.io/api/classes/worker.Runtime#install to customize these defaults.
await worker.run();
// Step 3: Start accepting tasks on the `hello-world` queue
//
// The worker runs until it encounters an unexpected error or the process receives a shutdown signal registered on
// the SDK Runtime object.
//
// By default, worker logs are written via the Runtime logger to STDERR at INFO level.
//
// See https://typescript.temporal.io/api/classes/worker.Runtime#install to customize these defaults.
await worker.run();
} finally {
// Close the connection once the worker has stopped
await connection.close();
}
}

run().catch((err) => {
Expand Down
19 changes: 12 additions & 7 deletions message-passing/execute-update/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ async function run() {
const connection = await NativeConnection.connect({
address: 'localhost:7233',
});
const worker = await Worker.create({
connection,
namespace: 'default',
taskQueue: 'my-task-queue',
workflowsPath: require.resolve('./workflows'),
});
await worker.run();
try {
const worker = await Worker.create({
connection,
namespace: 'default',
taskQueue: 'my-task-queue',
workflowsPath: require.resolve('./workflows'),
});
await worker.run();
} finally {
// Close the connection once the worker has stopped
await connection.close();
}
}

run().catch((err) => {
Expand Down
21 changes: 13 additions & 8 deletions message-passing/introduction/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ async function run() {
const connection = await wo.NativeConnection.connect({
address: 'localhost:7233',
});
const worker = await wo.Worker.create({
connection,
namespace: 'default',
taskQueue: 'my-task-queue',
workflowsPath: require.resolve('./workflows'),
activities,
});
await worker.run();
try {
const worker = await wo.Worker.create({
connection,
namespace: 'default',
taskQueue: 'my-task-queue',
workflowsPath: require.resolve('./workflows'),
activities,
});
await worker.run();
} finally {
// Close the connection once the worker has stopped
await connection.close();
}
}

run().catch((err) => {
Expand Down

0 comments on commit a07b78a

Please sign in to comment.