def update_cron_job(project_id, cron_job_id): data = request.get_json() if data and data.get('triggerType') == 'interval' and 'runDate' in data: data.pop('runDate') elif data and data.get('triggerType') == 'date' and 'interval' in data: data.pop('interval') if 'interval' in data: data['interval'] = float(data['interval']) if 'interval' in data and data['interval'] < 60: return jsonify({'status': 'failed', 'data': '定时任务间隔不可小于60秒!'}) if 'runDate' in data: data['runDate'] = common.frontend_date_str2datetime(data['runDate']) has_next_run_time = True if 'next_run_time' in data and data.pop('next_run_time') else False # 判断是否需要重启cron data["testEnvId"] = ObjectId(data["testEnvId"]) filtered_data = CronJob.filter_field(data) try: cron_manager.update_cron(cron_job_id=cron_job_id, project_id=project_id, cron_info=filtered_data) # TODO 仅修改名字/描述时,也重启了定时器,导致下一次运行时间变更, 解决成本有点大,暂不解决:) cron_manager.pause_cron(cron_id=cron_job_id) cron_manager.resume_cron(cron_id=cron_job_id) filtered_data['lastUpdateTime'] = datetime.utcnow() update_response = CronJob.update({"_id": cron_job_id}, {'$set': filtered_data}) if update_response["n"] == 0: return jsonify({'status': 'failed', 'data': '未找到相应更新数据!'}) return jsonify({'status': 'ok', 'data': '更新成功'}) except BaseException as e: current_app.logger.error("update cron job failed. - %s" % str(e)) return jsonify({'status': 'failed', 'data': '更新失败: %s' % e})
def update_cron(cron_id): data = request.get_json() if data and data.get('triggerType') == 'interval' and 'runDate' in data: data.pop('runDate') elif data and data.get('triggerType') == 'date' and 'interval' in data: data.pop('interval') if 'interval' in data: data['interval'] = float(data['interval']) if 'interval' in data and data['interval'] < 60: return jsonify({'status': 'failed', 'data': '定时任务间隔不可小于60秒!'}) if 'runDate' in data: data['runDate'] = common.frontend_date_str2datetime(data['runDate']) has_next_run_time = True if 'next_run_time' in data and data.pop('next_run_time') else False # 判断是否需要重启cron data = CronTab.filter_field(data) try: cron_manager.update_cron(cron_id=cron_id, cron_info=data) # TODO 仅修改名字/描述时,也重启了定时器,导致下一次运行时间变更, 解决成本有点大,暂不解决:) cron_manager.pause_cron(cron_id=cron_id) cron_manager.resume_cron(cron_id=cron_id) if has_next_run_time else None for key, value in data.items(): CronTab.update({"_id": cron_id}, {'$set': {key: value}}) update_response = CronTab.update({"_id": cron_id}, {'$set': {'lastUpdateTime': datetime.datetime.utcnow()}}) if update_response["n"] == 0: return jsonify({'status': 'failed', 'data': '未找到相应更新数据!'}) return jsonify({'status': 'ok', 'data': '更新成功'}) except BaseException as e: return jsonify({'status': 'failed', 'data': '更新失败: %s' % e})
def update_cron_job(project_id, cron_job_id): data = request.get_json() if data and data.get('triggerType') == 'interval' and 'runDate' in data: data.pop('runDate') elif data and data.get('triggerType') == 'date' and 'interval' in data: data.pop('interval') if 'interval' in data: data['interval'] = float(data['interval']) if 'interval' in data and data['interval'] < 60: return jsonify({'status': 'failed', 'data': '定时任务间隔不可小于60秒!'}) if 'runDate' in data: data['runDate'] = common.frontend_date_str2datetime(data['runDate']) has_next_run_time = True if 'next_run_time' in data and data.pop( 'next_run_time') else False # 判断是否需要重启cron data["testEnvId"] = ObjectId(data["testEnvId"]) if data["alarmMailGroupList"] and len(data["alarmMailGroupList"]) > 0: for index, value in enumerate(data["alarmMailGroupList"]): data["alarmMailGroupList"][index] = ObjectId(value) filtered_data = CronJob.filter_field(data) try: cron_manager.update_cron(cron_job_id=cron_job_id, project_id=project_id, cron_info=filtered_data) # update cronJob 自动停用,需点击启动后使用 cron_manager.pause_cron(cron_id=cron_job_id) filtered_data['status'] = 'PAUSED' filtered_data['lastUpdateTime'] = datetime.utcnow() update_response = CronJob.update({"_id": cron_job_id}, {'$set': filtered_data}) if update_response["n"] == 0: return jsonify({'status': 'failed', 'data': '未找到相应更新数据!'}) current_app.logger.info( "update cron job successfully. Cron Job Id: %s" % str(cron_job_id)) return jsonify({'status': 'ok', 'data': '更新成功'}) except BaseException as e: current_app.logger.error("update cron job failed. - %s" % str(e)) return jsonify({'status': 'failed', 'data': '更新失败: %s' % e})