Skip to content

Commit

Permalink
feat: add duplicate job
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowgate15 committed Aug 17, 2021
1 parent 3d82491 commit b9a756a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
36 changes: 31 additions & 5 deletions app/controllers/api/v1/jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,43 @@ async function add(ctx) {
const { bree } = ctx;
const { body } = ctx.request;

// initial job length for return
const origLength = bree.config.jobs.length;
if (body.copy) {
if (!_.isArray(body.jobs)) body.jobs = [body.jobs];
console.log(body);

const newJobs = [];

for (const job of body.jobs) {
const origJob = bree.config.jobs.find((j) => j.name === job.name);

if (!origJob)
return ctx.throw(Boom.badData('Name does not exist in jobs'));

const newJob = _.clone(origJob);
newJob.name = origJob.name + '(1)';

if (origJob.name.endsWith(')')) {
newJob.name = origJob.name.replace(
/(^.*)(\((\d+)\))(?=$)/i,
(_match, subName, _p2, num) =>
`${subName}(${Number.parseInt(num, 10) + 1})`
);
}

newJobs.push(newJob);
}

body.jobs = newJobs;
}

let jobs;

try {
bree.add(body.jobs);
jobs = bree.add(body.jobs);
} catch (err) {
return ctx.throw(Boom.badData(err));
}

const jobs = bree.config.jobs.slice(origLength - 1);

if (body.start) {
for (const job of jobs) {
bree.start(job.name);
Expand Down
22 changes: 22 additions & 0 deletions test/api/v1/jobs/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,25 @@ test('successfully auto start job', async (t) => {
// ensure job hasn't been started
t.truthy(bree.workers['auto-start']);
});

test('successfully duplicate job', async (t) => {
const { bree, api } = t.context;

const jobs = {
name: 'orig(1)',
path: path.join(utils.root, 'long.js')
};

await api.post(rootUrl).send({ jobs });

const res = await api
.post(rootUrl)
.send({ copy: true, jobs: { name: 'orig(1)' } });

t.is(res.status, 200);

t.log(res);
t.truthy(bree.config.jobs.find((j) => j.name === 'orig(2)'));

t.falsy(bree.workers['orig(2)']);
});

0 comments on commit b9a756a

Please sign in to comment.