Skip to content

Commit 30af2ba

Browse files
supprt runOnce (#106)
* support runOnce
1 parent 97890ad commit 30af2ba

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

agent/__tests__/index.spec.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const buildTestConfig = () => ({
5252
cronExpression: 'cron'
5353
},
5454
StatusReporterJob: {
55-
cronExpression: 'cron'
55+
cronExpression: 'cron',
5656
},
5757
queue: {
5858

@@ -115,6 +115,7 @@ describe('Agent unit test', () => {
115115
Codefresh.mockReset();
116116
Codefresh.mockImplementation(() => ({
117117
init: codefreshInitSpy,
118+
reportStatus: jest.fn(),
118119
}));
119120
jest.unmock('recursive-readdir');
120121
const agent = new Agent();
@@ -134,6 +135,18 @@ describe('Agent unit test', () => {
134135
expect(agent._startJob).toHaveBeenNthCalledWith(2, TaskPullerJob);
135136

136137
});
138+
it('Should call task that has runOnce when agent starts ', async () => {
139+
loadActualJobs();
140+
const config = buildTestConfig();
141+
config.jobs.StatusReporterJob.runOnce = true;
142+
const agent = new Agent();
143+
agent._runOnce = jest.fn();
144+
agent._startJob = jest.fn();
145+
await agent.init(config);
146+
expect(agent._runOnce).toHaveBeenCalledTimes(1);
147+
expect(agent._runOnce).toHaveBeenNthCalledWith(1, StatusReporterJob);
148+
149+
});
137150
});
138151

139152
describe('Prepare server', () => {

agent/index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,14 @@ class Agent {
9797
return Promise
9898
.fromCallback(cb => recursive(path.join(__dirname, './../jobs'), ignorePaths, cb))
9999
.map(require)
100-
.map(Job => this._startJob(Job));
100+
.map(Job => {
101+
const runOnce = _.get(this, `jobs.${Job.name}.runOnce`, false);
102+
if (runOnce) {
103+
this._runOnce(Job);
104+
}
105+
return Job;
106+
}).map(Job => this._startJob(Job));
107+
101108
}
102109

103110
async _readFromVenonaConfDir(dir) {
@@ -141,6 +148,16 @@ class Agent {
141148
});
142149
}
143150

151+
_runOnce(Job) {
152+
const taskLogger = this.logger.child({
153+
namespace: LOGGER_NAMESPACES.TASK,
154+
job: Job.name,
155+
uid: new Chance().guid(),
156+
});
157+
const job = new Job(this.codefreshAPI, this.runtimes, taskLogger);
158+
job.exec();
159+
}
160+
144161
_handleJobError(job) {
145162
return (err) => {
146163
if (err) {

config/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function build() {
3636
},
3737
StatusReporterJob: {
3838
cronExpression: process.env.JOB_REPORT_STATUS_CRON_EXPRESSION || CRON.EVERY_MINUTE,
39+
runOnce: true,
3940
},
4041
DEFAULT_CRON: CRON.EVERY_MINUTE,
4142
queue: {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "venona",
3-
"version": "1.0.6",
3+
"version": "1.0.7",
44
"description": "Codefresh agent to run on Codefresh's runtime environment and execute pipeline",
55
"main": "index.js",
66
"scripts": {

venonactl/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.6
1+
1.0.7

0 commit comments

Comments
 (0)