Skip to content

Commit 33c06b1

Browse files
refactor(index.ts): streamline activity module loading
- Removed the dynamic loading of activity modules in index.ts, simplifying the import process by directly importing activities from the new activities/index.ts file. This change enhances code clarity and maintainability by reducing complexity in the module loading logic. - Added a new activities/index.ts file to centralize activity exports, improving the organization of activity functions within the project. These modifications contribute to a cleaner and more efficient codebase for the Temporal worker.
1 parent fa344d1 commit 33c06b1

File tree

2 files changed

+2
-31
lines changed

2 files changed

+2
-31
lines changed

workers/main/src/activities/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './exampleActivity';

workers/main/src/index.ts

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,11 @@ import { Worker, NativeConnection } from '@temporalio/worker';
22
import * as path from 'path';
33
import { readdirSync } from 'fs';
44
import { Connection, ScheduleClient } from '@temporalio/client';
5+
import * as activities from './activities';
56

67
const workflowsPath = path.join(__dirname, 'workflows');
7-
const activitiesPath = path.join(__dirname, 'activities');
8-
9-
const activityModules = readdirSync(activitiesPath)
10-
.filter((f: string) => (f.endsWith('.ts') || f.endsWith('.js')) && !f.endsWith('.test.ts') && !f.endsWith('.test.js'))
11-
.map((f: string) => {
12-
const mod = require(path.join(activitiesPath, f));
13-
const validExports: Record<string, Function> = {};
14-
let hasValid = false;
15-
for (const [key, value] of Object.entries(mod)) {
16-
if (typeof value === 'function') {
17-
validExports[key] = value;
18-
hasValid = true;
19-
} else {
20-
console.warn(`Warning: Export '${key}' in module '${f}' is not a function and will be ignored.`);
21-
}
22-
}
23-
if (!hasValid) {
24-
console.warn(`Warning: No valid activity functions found in module '${f}'.`);
25-
}
26-
return validExports;
27-
});
28-
29-
const activities = Object.assign({}, ...activityModules);
30-
318
const address = process.env.TEMPORAL_ADDRESS || 'temporal:7233';
329

33-
/**
34-
* Entry point for the Temporal worker service.
35-
*
36-
* Loads workflow and activity modules, connects to the Temporal server, ensures a schedule exists,
37-
* and starts the worker to process workflows and activities from the task queue.
38-
*/
39-
4010
/**
4111
* Ensures that a schedule with the given ID exists in Temporal. If it does not exist, creates it.
4212
*

0 commit comments

Comments
 (0)