Skip to content

Commit b4b54ac

Browse files
feat(cron): support template params
1 parent 21a6aed commit b4b54ac

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

microservices/cron/__tests__/services/task-manager-test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ describe('services/task-manager', () => {
1616
nodeId: 'node1',
1717
method: 'method',
1818
rule: '1 * * * *',
19-
payload: {},
19+
payload: {
20+
params: {
21+
world: "<%= 'hello' %>",
22+
},
23+
},
2024
});
2125
const tasks = [task];
2226

@@ -141,9 +145,13 @@ describe('services/task-manager', () => {
141145
await callback();
142146

143147
const [, { response, status, taskId }] = TypeormMock.entityManager.save.lastCall.args;
148+
const [, params] = sendStub.firstCall.args;
144149

145150
expect(TypeormMock.entityManager.save).to.calledTwice;
146151
expect(sendStub).to.calledOnce;
152+
expect(params).to.deep.equal({
153+
world: 'hello',
154+
});
147155
expect(response).to.deep.equal(responseMock);
148156
expect(status).to.equal(TaskStatus.success);
149157
expect(taskId).to.equal(task.id);

microservices/cron/package-lock.json

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

microservices/cron/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@
4444
"class-transformer": "0.4.1",
4545
"class-validator": "^0.14.0",
4646
"class-validator-jsonschema": "^5.0.0",
47+
"lodash": "^4.17.21",
4748
"node-schedule": "^2.1.1",
4849
"typeorm": "0.2.41"
4950
},
5051
"devDependencies": {
52+
"@types/lodash": "^4.14.191",
5153
"@types/node-schedule": "^2.1.0"
5254
}
5355
}

microservices/cron/src/services/task-manager.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { performance } from 'perf_hooks';
22
import { Log } from '@lomray/microservice-helpers';
33
import type { AbstractMicroservice } from '@lomray/microservice-nodejs-lib';
4+
import _ from 'lodash';
45
import schedule from 'node-schedule';
56
import { getCustomRepository, getRepository, Repository } from 'typeorm';
67
import CONST from '@constants/index';
@@ -148,11 +149,12 @@ class TaskManager {
148149
taskId: id,
149150
status: TaskStatus.running,
150151
});
152+
const templatedParams = JSON.parse(_.template(JSON.stringify(params || {}))() as string);
151153

152154
await this.historyRepository.save(historyRecord);
153155

154156
try {
155-
const response = await this.ms.sendRequest(method, params, options);
157+
const response = await this.ms.sendRequest(method, templatedParams, options);
156158

157159
if (response.getError()) {
158160
throw response.getError();

0 commit comments

Comments
 (0)