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 add_cron(project_id): try: request.get_json()["projectId"] = ObjectId(project_id) request.get_json()["createAt"] = datetime.datetime.utcnow() request.get_json()["lastUpdateTime"] = datetime.datetime.utcnow() data = request.get_json() 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']) filtered_data = CronTab.filter_field(data, use_set_default=True) if filtered_data.get('runDate'): cron = Cron(test_case_suite_id_list=filtered_data.get('testCaseSuiteIdList'), test_domain=filtered_data.get('testDomain'), alarm_mail_list=filtered_data.get('alarmMailList'), is_ding_ding_notify=filtered_data.get('isDingDingNotify'), ding_ding_access_token=filtered_data.get('dingdingAccessToken'), ding_ding_notify_strategy=filtered_data.get('dingdingNotifyStrategy'), is_enterprise_wechat_notify=filtered_data.get('isEnterpriseWechatNotify'), enterprise_wechat_access_token=filtered_data.get('enterpriseWechatAccessToken'), enterprise_wechat_notify_strategy=filtered_data.get('enterpriseWechatNotifyStrategy'), trigger_type=filtered_data.get('triggerType'), test_case_id_list=filtered_data.get('testCaseIdList'), is_execute_forbiddened_case=filtered_data.get('isExecuteForbiddenedCase'), run_date=filtered_data.get('runDate')) else: cron = Cron(test_case_suite_id_list=filtered_data.get('testCaseSuiteIdList'), test_domain=filtered_data.get('testDomain'), alarm_mail_list=filtered_data.get('alarmMailList'), is_ding_ding_notify=filtered_data.get('isDingDingNotify'), ding_ding_access_token=filtered_data.get('dingdingAccessToken'), ding_ding_notify_strategy=filtered_data.get('dingdingNotifyStrategy'), is_enterprise_wechat_notify=filtered_data.get('isEnterpriseWechatNotify'), enterprise_wechat_access_token=filtered_data.get('enterpriseWechatAccessToken'), enterprise_wechat_notify_strategy=filtered_data.get('enterpriseWechatNotifyStrategy'), trigger_type=filtered_data.get('triggerType'), test_case_id_list=filtered_data.get('testCaseIdList'), is_execute_forbiddened_case=filtered_data.get('isExecuteForbiddenedCase'), seconds=filtered_data.get('interval')) cron_id = cron_manager.add_cron(cron) for key, value in filtered_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 add_cron_job(project_id): try: request_data = request.get_json() request_data["projectId"] = ObjectId(project_id) request_data["testEnvId"] = ObjectId(request_data["testEnvId"]) if request_data["alarmMailGroupList"] and len( request_data["alarmMailGroupList"]) > 0: for index, value in enumerate(request_data["alarmMailGroupList"]): request_data["alarmMailGroupList"][index] = ObjectId(value) request_data["createAt"] = datetime.utcnow() if 'interval' in request_data and request_data['interval'] < 60: return jsonify({'status': 'failed', 'data': '定时任务间隔不可小于60秒!'}) if 'interval' in request_data: request_data['interval'] = float(request_data['interval']) if 'runDate' in request_data: request_data['runDate'] = common.frontend_date_str2datetime( request_data['runDate']) filtered_data = CronJob.filter_field(request_data, use_set_default=True) if filtered_data.get('runDate'): cron = Cron( test_suite_id_list=filtered_data.get('testSuiteIdList'), project_id=project_id, test_env_id=filtered_data.get('testEnvId'), trigger_type=filtered_data.get('triggerType'), include_forbidden=filtered_data.get('includeForbidden'), alarm_mail_group_list=filtered_data.get('alarmMailGroupList'), run_date=filtered_data.get('runDate')) else: cron = Cron( test_suite_id_list=filtered_data.get('testSuiteIdList'), project_id=project_id, test_env_id=filtered_data.get('testEnvId'), trigger_type=filtered_data.get('triggerType'), include_forbidden=filtered_data.get('includeForbidden'), alarm_mail_group_list=filtered_data.get('alarmMailGroupList'), seconds=filtered_data.get('interval')) cron_id = cron_manager.add_cron(cron) filtered_data['lastUpdateTime'] = datetime.utcnow() update_response = CronJob.update({"_id": cron_id}, {'$set': filtered_data}) if update_response["n"] == 0: return jsonify({'status': 'failed', 'data': '新建成功但未找到相应更新数据!'}) current_app.logger.info("add cron job successfully. New Cron Id: %s" % str(cron_id)) return jsonify({'status': 'ok', 'data': '新建成功'}) except BaseException as e: current_app.logger.error("add cron job failed. - %s" % str(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})
def add_cron_job(project_id): try: request_data = request.get_json() request_data["projectId"] = ObjectId(project_id) request_data["testEnvId"] = ObjectId(request_data["testEnvId"]) if "alarmMailGroupList" in request_data and len( request_data["alarmMailGroupList"]) > 0: for index, value in enumerate(request_data["alarmMailGroupList"]): request_data["alarmMailGroupList"][index] = ObjectId(value) request_data["createAt"] = datetime.utcnow() if 'interval' in request_data and request_data['interval'] < 60: return jsonify({'status': 'failed', 'data': '定时任务间隔不可小于60秒!'}) if 'interval' in request_data: request_data['interval'] = float(request_data['interval']) if 'runDate' in request_data: request_data['runDate'] = common.frontend_date_str2datetime( request_data['runDate']) filtered_data = CronJob.filter_field(request_data, use_set_default=True) new_cron_job_id = str(common.get_object_id()) if filtered_data.get('runDate'): cron = Cron( cron_job_id=new_cron_job_id, test_suite_id_list=filtered_data.get('testSuiteIdList'), project_id=project_id, test_env_id=filtered_data.get('testEnvId'), trigger_type=filtered_data.get('triggerType'), include_forbidden=filtered_data.get('includeForbidden'), enable_wxwork_notify=filtered_data.get('enableWXWorkNotify'), wxwork_api_key=filtered_data.get('WXWorkAPIKey'), wxwork_mention_mobile_list=filtered_data.get( 'WXWorkMentionMobileList'), always_wxwork_notify=filtered_data.get('alwaysWXWorkNotify'), enable_ding_talk_notify=filtered_data.get( 'enableDingTalkNotify'), ding_talk_access_token=filtered_data.get( 'DingTalkAccessToken'), ding_talk_at_mobiles=filtered_data.get('DingTalkAtMobiles'), ding_talk_secret=filtered_data.get('DingTalkSecret'), always_ding_talk_notify=filtered_data.get( 'alwaysDingTalkNotify'), alarm_mail_group_list=filtered_data.get('alarmMailGroupList'), always_send_mail=filtered_data.get('alwaysSendMail'), run_date=filtered_data.get('runDate')) else: cron = Cron( cron_job_id=new_cron_job_id, test_suite_id_list=filtered_data.get('testSuiteIdList'), project_id=project_id, test_env_id=filtered_data.get('testEnvId'), trigger_type=filtered_data.get('triggerType'), include_forbidden=filtered_data.get('includeForbidden'), enable_wxwork_notify=filtered_data.get('enableWXWorkNotify'), wxwork_api_key=filtered_data.get('WXWorkAPIKey'), wxwork_mention_mobile_list=filtered_data.get( 'WXWorkMentionMobileList'), always_wxwork_notify=filtered_data.get('alwaysWXWorkNotify'), enable_ding_talk_notify=filtered_data.get( 'enableDingTalkNotify'), ding_talk_access_token=filtered_data.get( 'DingTalkAccessToken'), ding_talk_at_mobiles=filtered_data.get('DingTalkAtMobiles'), ding_talk_secret=filtered_data.get('DingTalkSecret'), always_ding_talk_notify=filtered_data.get( 'alwaysDingTalkNotify'), alarm_mail_group_list=filtered_data.get('alarmMailGroupList'), always_send_mail=filtered_data.get('alwaysSendMail'), seconds=filtered_data.get('interval')) cron_id = cron_manager.add_cron(cron) filtered_data['lastUpdateTime'] = datetime.utcnow() update_response = CronJob.update({"_id": cron_id}, {'$set': filtered_data}) if update_response["n"] == 0: return jsonify({'status': 'failed', 'data': '新建成功但未找到相应更新数据!'}) current_app.logger.info("add cron job successfully. New Cron Id: %s" % str(cron_id)) return jsonify({'status': 'ok', 'data': '新建成功'}) except BaseException as e: current_app.logger.error("add cron job failed. - %s" % str(e)) return jsonify({'status': 'failed', 'data': '新建失败: %s' % e})