def cancelJob(_id): """cancelled a job Cancelling a job usually means cancelling whole tasks. Also probabaly we have to send kill dognal to active task processes """ job = mongo.db.jobs.find_one({'_id': _id}) tasks = mongo.db.tasks.find({'job': _id}) for each in tasks: _t = ca.AsyncResult(each.get('ctid')) _t.revoke() job['status'] = 'cancelled' """Set status of job to cancelled""" mongo.db.jobs.update({'_id': _id}, job) """Bulk update tasks""" bulk = mongo.db.tasks.initialize_unordered_bulk_op() bulk.find({ 'job': _id, 'status': { '$ne': 'completed' } }).update({ '$set': { 'status': "cancelled", 'cancelled_on': now(), 'slave': None, } }) bulk.execute() return {'info': 'success'}
def pauseJob(_id): """Pause the job. Pausing a job means changing all ready tasks tasks status to pause. """ job = mongo.db.jobs.find_one({'_id': _id}) tasks = mongo.db.tasks.find({'job': _id}) for each in tasks: _t = ca.AsyncResult(each.get('ctid')) _t.revoke() job['status'] = 'paused' """Set status of job to paused""" mongo.db.jobs.update({'_id': _id}, job) """Bulk update tasks""" bulk = mongo.db.tasks.initialize_unordered_bulk_op() bulk.find({ 'job': _id, 'status': { '$ne': 'completed' } }).update({'$set': { 'status': "paused", 'paused_on': now(), }}) bulk.execute() return {'info': 'success'}
def killTaskprocess(_id): task = mongo.db.tasks.find_one({'_id': _id}) ctid = task.get('ctid') pid = task.get('pid') res = ca.AsyncResult(ctid) res.revoke(terminate=True) task['status'] = 'failed' mongo.db.tasks.update({'_id': _id}, task) return {'message': 'OK'}
def addTaskToQueue(taskId): data = mongo.db.tasks.find_one({'_id': taskId}) '''Revoke the task if it's available''' if data.get('ctid'): ca.AsyncResult(data.get('ctid')).revoke() proccess = data.get('proccess') raw_cmd = proccess.get('command') command = raw_cmd.format(threads=0, cwd=proccess.get('cwd'), task=data.get('name').replace(' ', '_'), filepath=proccess.get('filepath')) tname = '%s-%s' % (data.get('_id'), data.get('name')) tname = tname.replace(' ', '_') ctid = execute.apply_async( args=[command, tname, proccess.get('cwd'), proccess.get('target')], routing_key='FFarmRenderQueue01', exchange='FFarmRenderQueue01', queue='FFarmRenderQueue01') data['ctid'] = str(ctid) mongo.db.tasks.update({'_id': taskId}, data) return ctid
def updateTask(): client = server_tools.getClientIp(request) data = general.unpack(request.data) tid = ObjectId(data.get('_id')) data = data.get('data') task = mongo.db.tasks.find_one({'_id': tid}) if not task: abort(404) task['last_activity'] = general.now() for i in data: if data[i]: task[i] = data[i] task['slave'] = client mongo.db.tasks.update({'_id': tid}, task) if task.get('status') == 'on progress': job = mongo.db.jobs.find_one({'_id': task.get('job')}) job['status'] = 'on progress' mongo.db.jobs.update({'_id': job.get('_id')}, job) if task['progress'] == 100: ca.AsyncResult(task.get('ctid')).revoke() return general.pack('OK')