Skip to content

Commit

Permalink
parse result before returning job task ID (#428)
Browse files Browse the repository at this point in the history
  • Loading branch information
theorm authored Oct 7, 2024
1 parent 5d5029a commit fc19bdc
Showing 1 changed file with 80 additions and 66 deletions.
146 changes: 80 additions & 66 deletions src/services/jobs/jobs.class.js
Original file line number Diff line number Diff line change
@@ -1,108 +1,122 @@
/* eslint-disable no-unused-vars */
const debug = require('debug')('impresso/services:jobs');
const { BadGateway, NotFound, NotImplemented } = require('@feathersjs/errors');
const SequelizeService = require('../sequelize.service');
const { STATUS_KILLED, STATUS_DONE } = require('../../models/jobs.model');
const { measureTime } = require('../../util/instruments');
const debug = require('debug')('impresso/services:jobs')
const { BadGateway, NotFound, NotImplemented } = require('@feathersjs/errors')
const SequelizeService = require('../sequelize.service')
const { STATUS_KILLED, STATUS_DONE } = require('../../models/jobs.model')
const { measureTime } = require('../../util/instruments')

class Service {
constructor (options) {
this.options = options;
constructor(options) {
this.options = options
}

setup (app) {
this.app = app;
this.name = 'jobs';
setup(app) {
this.app = app
this.name = 'jobs'
this.sequelizeService = new SequelizeService({
app,
name: this.name,
});
})
}

async find (params) {
async find(params) {
const where = {
creatorId: params.user.id,
};
}

return measureTime(() => this.sequelizeService.find({
query: {
...params.query,
},
where,
}), 'jobs.find.db.find');
return measureTime(
() =>
this.sequelizeService.find({
query: {
...params.query,
},
where,
}),
'jobs.find.db.find'
)
}

async get (id, params) {
async get(id, params) {
const where = {
id,
};
}
if (params.user.uid) {
where['$creator.profile.uid$'] = params.user.uid;
where['$creator.profile.uid$'] = params.user.uid
} else {
where.creatorId = params.user.id;
where.creatorId = params.user.id
}
return measureTime(() => this.sequelizeService.get(id, { where })
.then(job => job.toJSON()), 'jobs.get.db.get');
return measureTime(() => this.sequelizeService.get(id, { where }).then(job => job.toJSON()), 'jobs.get.db.get')
}

async create (data, params) {
async create(data, params) {
// create a test job
const client = this.app.get('celeryClient');
const client = this.app.get('celeryClient')

if (!client) {
throw new BadGateway('celery is not ready');
throw new BadGateway('celery is not ready')
}

debug(`create '${this.name}', test task`);
debug(`create '${this.name}', test task`)

return client.run({
task: 'impresso.tasks.test',
args: [
// user id
params.user.id,
],
}).catch((err) => {
if (err.result.exc_type === 'DoesNotExist') {
throw new NotFound(err.result.exc_message);
} else if (err.result.exc_type === 'OperationalError') {
// probably db is not availabe
throw new NotImplemented();
}
throw new NotImplemented();
});
return client
.run({
task: 'impresso.tasks.test',
args: [
// user id
params.user.id,
],
})
.then(result => {
return { taskId: result.taskId }
})
.catch(err => {
if (err.result.exc_type === 'DoesNotExist') {
throw new NotFound(err.result.exc_message)
} else if (err.result.exc_type === 'OperationalError') {
// probably db is not availabe
throw new NotImplemented()
}
throw new NotImplemented()
})
}

async update (id, data, params) {
return data;
async update(id, data, params) {
return data
}

async patch (id, data, params) {
async patch(id, data, params) {
const where = {
creatorId: params.user.id,
};
debug(`[patch] id:${id}, params.user.uid:${params.user.uid}, where:`, where);
return this.sequelizeService.patch(id, {
status: data.sanitized.status,
}, { where });
}
debug(`[patch] id:${id}, params.user.uid:${params.user.uid}, where:`, where)
return this.sequelizeService.patch(
id,
{
status: data.sanitized.status,
},
{ where }
)
}

async remove (id, params) {
debug(`[remove] id:${id}, params.user.uid:${params.user.uid}`);
return this.sequelizeService.bulkRemove({
id,
creatorId: params.user.id,
status: [STATUS_KILLED, STATUS_DONE],
}).then(removed => ({
params: {
async remove(id, params) {
debug(`[remove] id:${id}, params.user.uid:${params.user.uid}`)
return this.sequelizeService
.bulkRemove({
id,
},
removed,
}));
creatorId: params.user.id,
status: [STATUS_KILLED, STATUS_DONE],
})
.then(removed => ({
params: {
id,
},
removed,
}))
}
}

module.exports = function (options) {
return new Service(options);
};
return new Service(options)
}

module.exports.Service = Service;
module.exports.Service = Service

0 comments on commit fc19bdc

Please sign in to comment.