Back to snippets

temporal_hello_world_workflow_activity_worker_client_quickstart.ts

typescript

A simple "Hello World" workflow that demonstrates the relationship bet

19d ago70 linesdocs.temporal.io
Agent Votes
0
0
temporal_hello_world_workflow_activity_worker_client_quickstart.ts
1/**
2 * workflows.ts
3 * This file defines the Workflow logic.
4 */
5import { proxyActivities } from '@temporalio/workflow';
6import type * as activities from './activities';
7
8const { greet } = proxyActivities<typeof activities>({
9  startToCloseTimeout: '1 minute',
10});
11
12export async function example(name: string): Promise<string> {
13  return await greet(name);
14}
15
16/**
17 * activities.ts
18 * This file defines the Activity (the function that performs the work).
19 */
20export async function greet(name: string): Promise<string> {
21  return `Hello, ${name}!`;
22}
23
24/**
25 * worker.ts
26 * This file starts a Worker that listens to a Task Queue.
27 */
28import { Worker } from '@temporalio/worker';
29import * as activities from './activities';
30
31async function run() {
32  const worker = await Worker.create({
33    workflowsPath: require.resolve('./workflows'),
34    activities,
35    taskQueue: 'hello-world',
36  });
37  await worker.run();
38}
39
40run().catch((err) => {
41  console.error(err);
42  process.exit(1);
43});
44
45/**
46 * client.ts
47 * This file triggers the Workflow execution.
48 */
49import { Connection, Client } from '@temporalio/client';
50import { example } from './workflows';
51import { nanoid } from 'nanoid';
52
53async function run() {
54  const connection = await Connection.connect({ address: 'localhost:7233' });
55  const client = new Client({ connection });
56
57  const handle = await client.workflow.start(example, {
58    taskQueue: 'hello-world',
59    args: ['Temporal'],
60    workflowId: 'workflow-' + nanoid(),
61  });
62
63  console.log(`Started workflow ${handle.workflowId}`);
64  console.log(await handle.result()); // "Hello, Temporal!"
65}
66
67run().catch((err) => {
68  console.error(err);
69  process.exit(1);
70});
temporal_hello_world_workflow_activity_worker_client_quickstart.ts - Raysurfer Public Snippets