def query_job(): jobs = JobSaver.query_job(**request.json) if not jobs: return get_json_result(retcode=101, retmsg='Query job failed, no job found.') data = jobs[0].to_json(filters=[ 'job_id', 'create_time', 'start_time', 'end_time', 'status', 'elapsed' ]) plays = JobSaver.query_play(reverse=False, order_by='play_id', **request.json) if plays: play_filters = [ 'job_id', 'play_id', 'create_time', 'start_time', 'end_time', 'status', 'elapsed' ] data['f_plays'] = [ play.to_json(filters=play_filters) for play in plays ] return get_json_result(retmsg="Query job successfully.", data=data)
def stop_job(job_id, status=JobStatus.CANCELED): jobs = JobSaver.query_job(job_id=job_id) if jobs: plays = JobSaver.query_play(job_id=job_id) stop_result = {} final_status = True for play in [item for item in plays if not EndStatus.contains(item.f_status)]: stop_status = JobController.stop_play(job_id=job_id, play_id=play.f_play_id, status=status) stop_result[play.f_play_id] = 'stopped successfully' if stop_status else 'stopped failed' final_status = final_status & stop_status if final_status: update_info = { 'job_id': job_id, 'end_time': current_timestamp(), 'status': JobStatus.CANCELED } JobSaver.update_job(update_info) JobSaver.update_job_status(update_info) return final_status, stop_result else: return False, {job_id: f"Cannot found job {job_id}"}
def run_do(self): try: running_plays = JobSaver.query_play(status='running') stop_job_ids = set() for play in running_plays: try: process_exist = job_utils.check_job_process(int( play.f_pid)) if not process_exist: detect_logger.info( 'job {} play {} process does not exist'.format( play.f_job_id, play.f_pid)) stop_job_ids.add(play.f_job_id) detect_logger.info( f'start to stop play {play.f_play_id}') JobController.stop_play(job_id=play.f_job_id, play_id=play.f_play_id) except Exception as e: detect_logger.exception(e) # ready_plays = JobSaver.query_play(status='ready') # for play in ready_plays: # try: if stop_job_ids: schedule_logger().info( 'start to stop jobs: {}'.format(stop_job_ids)) for job_id in stop_job_ids: jobs = JobSaver.query_job(job_id=job_id) if jobs: if not EndStatus.contains(jobs[0].f_status): JobController.stop_job(job_id=job_id) except Exception as e: detect_logger.exception(e) finally: detect_logger.info('finish detect running job')