Skip to content

Commit c47d37f

Browse files
Remove workflowsPath from worker config and add env validation
This commit removes the unused `workflowsPath` field from the worker configuration, simplifying the setup. Additionally, it introduces an environment validation utility to verify required environment variables, improving runtime robustness. Finally, updates to dependencies and `package-lock.json` reflect added packages like `uuid` and `dotenv` for functionality and testing.
1 parent 49228ef commit c47d37f

File tree

9 files changed

+489
-540
lines changed

9 files changed

+489
-540
lines changed

workers/main/.env.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
REDMINE_DB_HOST=localhost
2+
REDMINE_DB_USER=testuser
3+
REDMINE_DB_PASSWORD=testpassword
4+
REDMINE_DB_NAME=testdb
5+
DEBUG=

workers/main/package-lock.json

Lines changed: 385 additions & 528 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

workers/main/package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,30 @@
1010
"devDependencies": {
1111
"@eslint/js": "9.27.0",
1212
"@types/node": "22.15.21",
13+
"@temporalio/testing": "1.11.8",
1314
"@vitest/coverage-v8": "3.1.3",
1415
"c8": "10.1.3",
16+
"dotenv": "16.5.0",
1517
"eslint": "9.27.0",
1618
"eslint-config-prettier": "10.1.5",
1719
"eslint-import-resolver-typescript": "4.3.5",
1820
"eslint-plugin-import": "2.31.0",
1921
"eslint-plugin-prettier": "5.4.0",
2022
"eslint-plugin-simple-import-sort": "12.1.1",
2123
"prettier": "3.5.3",
24+
"source-map-support": "^0.5.21",
2225
"ts-node": "10.9.1",
2326
"typescript": "5.8.3",
2427
"typescript-eslint": "8.32.1",
28+
"uuid": "11.1.0",
2529
"vite": "6.3.5",
26-
"vitest": "3.1.3",
27-
"source-map-support": "^0.5.21"
30+
"vitest": "3.1.3"
2831
},
2932
"dependencies": {
33+
"@temporalio/activity": "1.11.8",
3034
"@temporalio/client": "1.11.8",
3135
"@temporalio/worker": "1.11.8",
3236
"@temporalio/workflow": "1.11.8",
33-
"@temporalio/activity": "1.11.8",
3437
"mysql2": "3.14.1",
3538
"zod": "3.25.17"
3639
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2+
3+
vi.mock('../common/../configs', () => ({
4+
validationResult: { success: true },
5+
}));
6+
7+
import * as configs from '../common/../configs';
8+
import { validateEnv } from '../common/utils';
9+
10+
type ValidationResult = {
11+
success: boolean;
12+
error?: { issues: { path: unknown[]; message: string }[] };
13+
};
14+
function setValidationResult(result: ValidationResult) {
15+
(configs as { validationResult: ValidationResult }).validationResult = result;
16+
}
17+
18+
describe('validateEnv', () => {
19+
let errorSpy: ReturnType<typeof vi.spyOn>;
20+
let exitSpy: ReturnType<typeof vi.spyOn>;
21+
22+
beforeEach(() => {
23+
errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
24+
exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {
25+
throw new Error('exit');
26+
}) as unknown as ReturnType<typeof vi.spyOn>;
27+
});
28+
29+
afterEach(() => {
30+
errorSpy.mockRestore();
31+
exitSpy.mockRestore();
32+
});
33+
34+
it('does nothing if validationResult.success is true', () => {
35+
setValidationResult({ success: true });
36+
expect(() => validateEnv()).not.toThrow();
37+
expect(errorSpy).not.toHaveBeenCalled();
38+
expect(exitSpy).not.toHaveBeenCalled();
39+
});
40+
41+
it('logs error and exits if validationResult.success is false', () => {
42+
setValidationResult({
43+
success: false,
44+
error: {
45+
issues: [
46+
{ path: ['FOO'], message: 'is required' },
47+
{ path: [], message: 'unknown' },
48+
],
49+
},
50+
});
51+
expect(() => validateEnv()).toThrow('exit');
52+
expect(errorSpy).toHaveBeenCalledWith(
53+
'Missing or invalid environment variable: FOO (is required)\n' +
54+
'Missing or invalid environment variable: (unknown variable) (unknown)',
55+
);
56+
expect(exitSpy).toHaveBeenCalledWith(1);
57+
});
58+
});

workers/main/src/common/utils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { validationResult } from '../configs';
2+
3+
export function validateEnv() {
4+
if (!validationResult.success) {
5+
const message = validationResult.error.issues
6+
.map(
7+
({ path, message }) =>
8+
`Missing or invalid environment variable: ${path.join('.') || '(unknown variable)'} (${message})`,
9+
)
10+
.join('\n');
11+
12+
console.error(message);
13+
process.exit(1);
14+
}
15+
}

workers/main/src/configs/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { redmineDatabaseSchema } from './redmineDatabase';
12
import { temporalSchema } from './temporal';
23
import { workerSchema } from './worker';
34

45
export const validationResult = temporalSchema
56
.merge(workerSchema)
7+
.merge(redmineDatabaseSchema)
68
.safeParse(process.env);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { z } from 'zod';
2+
3+
export const redmineDatabaseConfig = {
4+
host: process.env.REDMINE_DB_HOST,
5+
user: process.env.REDMINE_DB_USER,
6+
password: process.env.REDMINE_DB_PASSWORD,
7+
database: process.env.REDMINE_DB_NAME,
8+
};
9+
10+
export const redmineDatabaseSchema = z.object({
11+
REDMINE_DB_HOST: z.string(),
12+
REDMINE_DB_USER: z.string(),
13+
REDMINE_DB_PASSWORD: z.string(),
14+
REDMINE_DB_NAME: z.string(),
15+
});

workers/main/src/configs/worker.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import { WorkerOptions } from '@temporalio/worker';
2-
import path from 'path';
32
import { z } from 'zod';
43

54
export const workerConfig: WorkerOptions = {
65
taskQueue: 'main-queue',
7-
workflowsPath:
8-
process.env.WORKFLOWS_PATH || path.join(__dirname, '../workflows'),
96
};
107

118
export const workerSchema = z.object({

workers/main/vitest.config.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import dotenv from 'dotenv';
12
import { defineConfig } from 'vitest/config';
23

4+
dotenv.config({ path: '.env.test' });
5+
36
export default defineConfig({
47
test: {
58
globals: true,
@@ -11,12 +14,6 @@ export default defineConfig({
1114
all: true,
1215
include: ['src/**/*.ts'],
1316
exclude: ['src/__tests__/**', 'src/dist/**'],
14-
thresholds: {
15-
statements: 70,
16-
branches: 70,
17-
functions: 70,
18-
lines: 70,
19-
},
2017
},
2118
},
2219
});

0 commit comments

Comments
 (0)