From 30af2badffe07fec361e1e081fd8e6ea666f7e55 Mon Sep 17 00:00:00 2001 From: oren-codefresh <45915522+oren-codefresh@users.noreply.github.com> Date: Mon, 16 Mar 2020 16:20:12 +0200 Subject: [PATCH] supprt runOnce (#106) * support runOnce --- agent/__tests__/index.spec.js | 15 ++++++++++++++- agent/index.js | 19 ++++++++++++++++++- config/index.js | 1 + package.json | 2 +- venonactl/VERSION | 2 +- 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/agent/__tests__/index.spec.js b/agent/__tests__/index.spec.js index d531095f..2bd8f8e5 100644 --- a/agent/__tests__/index.spec.js +++ b/agent/__tests__/index.spec.js @@ -52,7 +52,7 @@ const buildTestConfig = () => ({ cronExpression: 'cron' }, StatusReporterJob: { - cronExpression: 'cron' + cronExpression: 'cron', }, queue: { @@ -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(); @@ -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', () => { diff --git a/agent/index.js b/agent/index.js index e31e8284..39f96fd8 100644 --- a/agent/index.js +++ b/agent/index.js @@ -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) { @@ -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) { diff --git a/config/index.js b/config/index.js index 14382980..c3c993e2 100644 --- a/config/index.js +++ b/config/index.js @@ -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: { diff --git a/package.json b/package.json index 6a0c2522..d6a50a80 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/venonactl/VERSION b/venonactl/VERSION index af0b7ddb..238d6e88 100644 --- a/venonactl/VERSION +++ b/venonactl/VERSION @@ -1 +1 @@ -1.0.6 +1.0.7