Skip to content

Commit

Permalink
supprt runOnce (#106)
Browse files Browse the repository at this point in the history
* support runOnce
  • Loading branch information
oren-codefresh authored Mar 16, 2020
1 parent 97890ad commit 30af2ba
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
15 changes: 14 additions & 1 deletion agent/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const buildTestConfig = () => ({
cronExpression: 'cron'
},
StatusReporterJob: {
cronExpression: 'cron'
cronExpression: 'cron',
},
queue: {

Expand Down Expand Up @@ -115,6 +115,7 @@ describe('Agent unit test', () => {
Codefresh.mockReset();
Codefresh.mockImplementation(() => ({
init: codefreshInitSpy,
reportStatus: jest.fn(),
}));
jest.unmock('recursive-readdir');
const agent = new Agent();
Expand All @@ -134,6 +135,18 @@ describe('Agent unit test', () => {
expect(agent._startJob).toHaveBeenNthCalledWith(2, TaskPullerJob);

});
it('Should call task that has runOnce when agent starts ', async () => {
loadActualJobs();
const config = buildTestConfig();
config.jobs.StatusReporterJob.runOnce = true;
const agent = new Agent();
agent._runOnce = jest.fn();
agent._startJob = jest.fn();
await agent.init(config);
expect(agent._runOnce).toHaveBeenCalledTimes(1);
expect(agent._runOnce).toHaveBeenNthCalledWith(1, StatusReporterJob);

});
});

describe('Prepare server', () => {
Expand Down
19 changes: 18 additions & 1 deletion agent/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,14 @@ class Agent {
return Promise
.fromCallback(cb => recursive(path.join(__dirname, './../jobs'), ignorePaths, cb))
.map(require)
.map(Job => this._startJob(Job));
.map(Job => {
const runOnce = _.get(this, `jobs.${Job.name}.runOnce`, false);
if (runOnce) {
this._runOnce(Job);
}
return Job;
}).map(Job => this._startJob(Job));

}

async _readFromVenonaConfDir(dir) {
Expand Down Expand Up @@ -141,6 +148,16 @@ class Agent {
});
}

_runOnce(Job) {
const taskLogger = this.logger.child({
namespace: LOGGER_NAMESPACES.TASK,
job: Job.name,
uid: new Chance().guid(),
});
const job = new Job(this.codefreshAPI, this.runtimes, taskLogger);
job.exec();
}

_handleJobError(job) {
return (err) => {
if (err) {
Expand Down
1 change: 1 addition & 0 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function build() {
},
StatusReporterJob: {
cronExpression: process.env.JOB_REPORT_STATUS_CRON_EXPRESSION || CRON.EVERY_MINUTE,
runOnce: true,
},
DEFAULT_CRON: CRON.EVERY_MINUTE,
queue: {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "venona",
"version": "1.0.6",
"version": "1.0.7",
"description": "Codefresh agent to run on Codefresh's runtime environment and execute pipeline",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion venonactl/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.6
1.0.7

0 comments on commit 30af2ba

Please sign in to comment.