From 47564c9d589892ede0a4cb712cbeb89539f2387b Mon Sep 17 00:00:00 2001 From: Jwahir Sundai Date: Tue, 5 Aug 2025 11:59:09 -0500 Subject: [PATCH 1/2] code consistency with sample --- docs/develop/typescript/set-up.mdx | 95 +++++++++++++++++++----------- 1 file changed, 59 insertions(+), 36 deletions(-) diff --git a/docs/develop/typescript/set-up.mdx b/docs/develop/typescript/set-up.mdx index 0e08fce598..23d8237ad4 100644 --- a/docs/develop/typescript/set-up.mdx +++ b/docs/develop/typescript/set-up.mdx @@ -156,9 +156,10 @@ This test will confirm that: Create an Activity file (activity.ts): ```ts -export async function activity(name: string): Promise { - return 'Hello ' + name; +export async function greet(name: string): Promise { + return `Hello, ${name}!`; } + ``` An Activity is a normal function or method that executes a single, well-defined action (either short or long running), which often involve interacting with the outside world, such as sending emails, making network requests, writing to a database, or calling an API, which are prone to failure. If an Activity fails, Temporal automatically retries it based on your configuration. @@ -169,16 +170,18 @@ Create a Workflow file (workflows.ts): ```ts import { proxyActivities } from '@temporalio/workflow'; -import type * as activityTypes from './activity'; +// Only import the activity types +import type * as activities from './activities'; -const { activity } = proxyActivities({ - startToCloseTimeout: '10 seconds', +const { greet } = proxyActivities({ + startToCloseTimeout: '1 minute', }); -export async function sayHelloWorkflow(name: string): Promise { - const response = await activity(name); - return response; +/** A workflow that simply calls an activity */ +export async function example(name: string): Promise { + return await greet(name); } + ``` Workflows orchestrate Activities and contain the application logic. @@ -191,31 +194,49 @@ If the application itself crashes, Temporal will automatically recreate its pre- Create a Worker file (worker.ts): ```ts -import { Worker } from '@temporalio/worker'; -import * as activitiy from './activity'; +import { NativeConnection, Worker } from '@temporalio/worker'; +import * as activities from './activities'; async function run() { - // Step 1: Register Workflows and Activities with the Worker and connect to - // the Temporal server. - const worker = await Worker.create({ - workflowsPath: require.resolve('./workflows'), - activities: activitiy, - taskQueue: 'my-task-queue', + // Step 1: Establish a connection with Temporal server. + // + // Worker code uses `@temporalio/worker.NativeConnection`. + // (But in your application code it's `@temporalio/client.Connection`.) + const connection = await NativeConnection.connect({ + address: 'localhost:7233', + // TLS and gRPC metadata configuration goes here. }); - // Worker connects to localhost by default and uses console.error for logging. - // Customize the Worker by passing more options to create(): - // https://typescript.temporal.io/api/classes/worker.Worker - // If you need to configure server connection parameters, see docs: - // https://docs.temporal.io/typescript/security#encryption-in-transit-with-mtls - - // Step 2: Start accepting tasks on the my-task-queue queue - await worker.run(); + 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(); + } finally { + // Close the connection once the worker has stopped + await connection.close(); + } } run().catch((err) => { console.error(err); process.exit(1); }); + ``` Run the Worker and keep this terminal running: @@ -239,31 +260,32 @@ This final step will validate that everything is working correctly with your fil Create a separate file called `client.ts`. ```ts -import { Connection, WorkflowClient } from '@temporalio/client'; -import { sayHelloWorkflow } from './workflows'; +import { Connection, Client } from '@temporalio/client'; +import { example } from './workflows'; +import { nanoid } from 'nanoid'; async function run() { - // Connect to the default Server location (localhost:7233) - const connection = await Connection.connect(); + // Connect to the default Server location + const connection = await Connection.connect({ address: 'localhost:7233' }); // In production, pass options to configure TLS and other settings: // { - // address: 'foo.bar.tmprl.cloud', - // tls: {} + // address: 'foo.bar.tmprl.cloud', + // tls: {} // } - const client = new WorkflowClient({ + const client = new Client({ connection, // namespace: 'foo.bar', // connects to 'default' namespace if not specified }); - const handle = await client.start(sayHelloWorkflow, { + const handle = await client.workflow.start(example, { + taskQueue: 'hello-world', // type inference works! args: [name: string] args: ['Temporal'], - taskQueue: 'my-task-queue', - // in practice, use a meaningful business id, eg customerId or transactionId - workflowId: 'my-first-workflow', + // in practice, use a meaningful business ID, like customerId or transactionId + workflowId: 'workflow-' + nanoid(), }); - console.log('Started workflow ' + handle.workflowId); + console.log(`Started workflow ${handle.workflowId}`); // optional: wait for client result console.log(await handle.result()); // Hello, Temporal! @@ -273,6 +295,7 @@ run().catch((err) => { console.error(err); process.exit(1); }); + ``` Then run: From a0e9c4b601def9a6d56d0191962b845f93432422 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 5 Aug 2025 17:00:17 +0000 Subject: [PATCH 2/2] CI: Automatic .md and .mdx formatting --- docs/develop/typescript/set-up.mdx | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/docs/develop/typescript/set-up.mdx b/docs/develop/typescript/set-up.mdx index 23d8237ad4..659b8454e3 100644 --- a/docs/develop/typescript/set-up.mdx +++ b/docs/develop/typescript/set-up.mdx @@ -159,7 +159,6 @@ Create an Activity file (activity.ts): export async function greet(name: string): Promise { return `Hello, ${name}!`; } - ``` An Activity is a normal function or method that executes a single, well-defined action (either short or long running), which often involve interacting with the outside world, such as sending emails, making network requests, writing to a database, or calling an API, which are prone to failure. If an Activity fails, Temporal automatically retries it based on your configuration. @@ -181,7 +180,6 @@ const { greet } = proxyActivities({ export async function example(name: string): Promise { return await greet(name); } - ``` Workflows orchestrate Activities and contain the application logic. @@ -236,7 +234,6 @@ run().catch((err) => { console.error(err); process.exit(1); }); - ``` Run the Worker and keep this terminal running: @@ -260,9 +257,9 @@ This final step will validate that everything is working correctly with your fil Create a separate file called `client.ts`. ```ts -import { Connection, Client } from '@temporalio/client'; -import { example } from './workflows'; +import { Client, Connection } from '@temporalio/client'; import { nanoid } from 'nanoid'; +import { example } from './workflows'; async function run() { // Connect to the default Server location @@ -295,7 +292,6 @@ run().catch((err) => { console.error(err); process.exit(1); }); - ``` Then run: