Esempio n. 1
0
 def pause(data):
     params = Command.__get_data(data)
     if params:
         if params.get('process_id'):
             tag = params.get('process_id')
             try:
                 task_exist = TaskModel.get(TaskModel.process_id == tag)
             except Exception:
                 task_exist = False
             queue_exist = PGQ.preview(tag)
             if queue_exist or task_exist:
                 try:
                     action = ActionModel.get(ActionModel.process_id == tag)
                 except:
                     action = ActionModel()
                     action.process_id = params.get('process_id')
                 action.action = constants.STATUS_PAUSE
                 if params.get('timeout'):
                     action.timeout = params.get('timeout')
                 else:
                     return {
                         'data': 'timeout keyword not found',
                         'status': constants.STATUS_ERROR
                     }
                 try:
                     action.save()
                 except Exception:
                     raise DatabaseError('can not access database')
                 Command.send_signal(
                     ['process_h.py', 'process_m.py', 'process_l.py'])
                 return {
                     'data': 'pause request received!',
                     'status': constants.STATUS_PENDING
                 }
             else:
                 return {
                     'data': 'process_id not found',
                     'status': constants.STATUS_ERROR
                 }
         else:
             return {
                 'data': 'process_id keyword not found',
                 'status': constants.STATUS_ERROR
             }
     return {
         'data': 'format or process id is wrong',
         'status': constants.STATUS_ERROR
     }
Esempio n. 2
0
    def resume(data):
        params = Command.__get_data(data)
        if params:
            if params.get('process_id'):
                tag = params.get('process_id')
                try:
                    task_exist = TaskModel.get(TaskModel.process_id == tag)
                except Exception:
                    task_exist = False
                if PGQ.preview(tag) or task_exist:
                    try:
                        action = ActionModel.get(ActionModel.process_id == tag)
                    except:
                        action = ActionModel()
                        action.process_id = params.get('process_id')
                    action.action = constants.STATUS_RESUME
                    try:
                        action.save()
                    except Exception:
                        raise DatabaseError(' can not access database')
                    Command.send_signal(
                        ['process_h.py', 'process_m.py', 'process_l.py'])
                    return {
                        'data': 'resume request applied!',
                        'status': constants.STATUS_SUCCESS
                    }
                else:
                    return {
                        'data': 'process_id not found',
                        'status': constants.STATUS_ERROR
                    }

            else:
                return {
                    'data': 'process_id keyword not found',
                    'status': constants.STATUS_ERROR
                }
        return {
            'data': 'format or process id is wrong',
            'status': constants.STATUS_ERROR
        }
Esempio n. 3
0
 def progress(data):
     params = Command.__get_data(data)
     if params:
         try:
             # check if process is in task_model
             result = TaskModel.get(
                 TaskModel.process_id == params.get('process_id'))
             if result.status == constants.STATUS_RUNNING:
                 return {
                     'data': result.progress,
                     'status': constants.STATUS_SUCCESS
                 }
             else:
                 res = result.response_data
                 for k, r in res.items():
                     try:
                         res.update({k: json.loads(r)})
                     except Exception:
                         pass
                 return {'data': res, 'status': constants.STATUS_SUCCESS}
         except Exception:  # exception happen when process not exist in task_model, so check queue
             if PGQ.preview(params.get('process_id')):
                 return {
                     'data': {
                         'status': constants.STATUS_QUEUE,
                         'process_id': params.get('process_id'),
                         'message': 'process still is in the queue'
                     },
                     'status': constants.STATUS_SUCCESS
                 }
             else:  # task not exist in queue neither
                 return {
                     'data': 'process_id not found',
                     'status': constants.STATUS_ERROR
                 }
     return {
         'data': 'format or process id is wrong',
         'status': constants.STATUS_ERROR
     }
Esempio n. 4
0
    def cancel(data):
        params = Command.__get_data(data)
        if params:
            if params.get('process_id'):

                tag = params.get('process_id')
                try:  # check if this process_id is running
                    task_exist = TaskModel.get(TaskModel.process_id == tag)
                except Exception:
                    task_exist = False
                queue_exist = PGQ.preview(tag)
                # check if this process_id is on queue or running
                if queue_exist or task_exist:
                    try:  # check if any other command submitted
                        action = ActionModel.get(ActionModel.process_id == tag)
                    except:
                        action = ActionModel()
                        action.process_id = params.get('process_id')
                    action.action = constants.STATUS_CANCEL
                    if queue_exist:
                        action.status = constants.STATUS_SUCCESS
                    action.save()

                    if queue_exist:
                        item = PGQ.get_by_tag(
                            tag
                        )  # get from queue and remove item, and save in task_model
                        task_model = TaskModel()
                        # route, call_back, token, process_id, data, module_version
                        error = {
                            'code': CancelExecutionError.get_code(),
                            'message': 'task canceled by user!'
                        }
                        task_model.route = item.data.route
                        task_model.process_id = item.data.process_id
                        task_model.status = constants.STATUS_FAILED
                        task_model.token = item.data.token
                        task_model.module_version = item.data.module_version
                        task_model.data = item.data.data
                        task_model.call_back = item.data.call_back
                        task_model.delivery = constants.STATUS_NEW
                        task_model.queue_name = item.name
                        task_model.response_data = {
                            'data': to_json({}),
                            'error': to_json(error),
                            'status': constants.STATUS_ERROR,
                            'token': item.data.token,
                            'process_id': item.data.process_id
                        }
                        try:
                            task_model.save()
                        except Exception:
                            raise DatabaseError('can not access database')
                    Command.send_signal(
                        ['process_h.py', 'process_m.py', 'process_l.py'])
                    return {
                        'data': 'cancel request received!',
                        'status': constants.STATUS_PENDING
                    }
                else:
                    return {
                        'data': 'process_id not found',
                        'status': constants.STATUS_ERROR
                    }
            else:
                return {
                    'data': 'process_id keyword not found',
                    'status': constants.STATUS_ERROR
                }
        return {
            'data': 'format or process id is wrong',
            'status': constants.STATUS_ERROR
        }