def get(self, id): task = Task.query.filter_by(id=id).first() if len(task.interface.all()) <= 1: flash(u'定时任务执行过程的测试用例为多用例,请你谅解') return redirect(url_for('home.timingtask')) try: time_start = eval(task.taskstart) day_week = time_start['day_of_week'] hour = time_start['hour'] mindes = time_start['minute'] sched.add_job(func=addtask, id=str(id), args=[str(id)], trigger='cron', day_of_week=day_week, hour=hour, minute=mindes, jobstore='redis', replace_existing=True) task.yunxing_status = '启动' db.session.commit() flash(u'定时任务启动成功!') return redirect(url_for('home.timingtask')) except Exception as e: flash(u'定时任务启动失败!原因:%e' % e) return redirect(url_for('home.timingtask'))
def get(self, id): task = Task.query.filter_by(id=id).first() if len(task.interface.all()) <= 1: flash(MessageEnum.task_must_be_mulite_case.value[1]) return redirect(url_for('home.timingtask')) try: time_start = eval(task.taskstart) day_week = time_start['day_of_week'] hour = time_start['hour'] mindes = time_start['minute'] sched.add_job(func=addtask, id=str(id), args=[str(id)], trigger='cron', day_of_week=day_week, hour=hour, minute=mindes, jobstore='redis', replace_existing=True) task.yunxing_status = '启动' db.session.commit() flash(MessageEnum.task_start_success.value[1]) return redirect(url_for('home.timingtask')) except Exception as e: logger.exception(e) flash(MessageEnum.task_start_eeor.value[1]) return redirect(url_for('home.timingtask'))
def post(self): res = ResMsg() data = request.get_json() flag, mes = format_checker(statistics_task_schema, data) if not flag: res.update(code=-1, data=mes, msg=param_format_error) return jsonify(res.data) password = data.get('password') if password != 'za-test': res.update(code=-1, data='', msg=password_error) return jsonify(res.data) run_time = '0 2 * * *' job_name = 'case_statistics_%s' % password try: if sched.get_job(job_name): mes = {'error': '缓存中已存在任务 %s 信息' % job_name} res.update(code=-1, data=mes, msg=task_start_error) return jsonify(res.data) # 标准crontab格式'5 2 * * *' 每天凌晨2点5分执行 sched.add_job(case_data_statistics, CronTrigger.from_crontab(run_time), id=job_name, args=[], jobstore='redis', replace_existing=True) res.update(code=1, data='', msg=request_success) return jsonify(res.data) except Exception as e: res.update(code=-1, data=str(e), msg=task_start_error) return jsonify(res.data)
def save(self, mod): sched.add_job(lambda: mod.execute_change(self.newState), "cron", day_of_week=self.wDay, hour=self.hour, minute=self.minute, id=self.id, replace_existing=True)
def cast(self): #schedule the campaign url = Campaignpages.query.filter_by(campaign_id=self.id, index=0).first().page.url base_url = 'https://%s' % self.domain.domain if self.ssl else 'http://%s' % self.domain.domain mail = self.profile.get_mailer() #self.profile.schedule_campaign(email=self.email, targets=self.list.targets, campaign_id=self.id, base_url=base_url, interval=self.send_interval, batch_size=self.batch_size, start_time=self.start_time, data=data, ip=self.server.ip, port=self.server.port, url=url) job_id = str(self.id) # Schedule the campaign and intialize it current_jobs = sched.get_jobs() # In case the batch size or interval are blank, set them appropriately if not self.batch_size: self.batch_size = len(self.list.targets) if not self.send_interval: self.send_interval = 0 db.session.commit() # Schedule the campaign and intialize it try: if self.start_time < datetime.now(): # schedule campaign to be run in 8 seconds from current timme - anything less and the campaign will wait 1 interval before sending the first batch of emails. Does not affect sending without batches or future start times sched.add_job(func=self.run_campaign, trigger='interval', minutes=int(self.send_interval), id=job_id, start_date=datetime.now() + timedelta(0, 8), replace_existing=True, args=[mail, base_url, url]) else: sched.add_job(func=self.run_campaign, trigger='interval', minutes=int(self.send_interval), id=job_id, start_date=self.start_time, replace_existing=True, args=[mail, base_url, url]) except Exception: app.logger.exception( f'Error scheduling campaign {self.name} (ID: {self.id})') else: app.logger.info( f'Scheduled campaign {self.name} (ID: {self.id}) to start at {self.start_time} - Sending {len(self.list.targets)} emails in batches of {self.batch_size} every {self.send_interval} minutes' ) self.status = 'Scheduled' db.session.commit()
def check_in_exp(): check_in_users = User.query.filter_by(check_in_mode=True) if len(check_in_users.all()) == 0: return now = datetime.now() exp_users = check_in_users.filter( User.last_check_in < now - timedelta(hours=24)).all() account_sid = os.environ.get('TWILIOSID') auth_token = os.environ.get('TWILIOTOKEN') tw_num = os.environ.get('TWILIONUM') client = Client(account_sid, auth_token) for user in exp_users: all_contacts = user.contacts.all() if len(all_contacts) > 0: body_text = user.username + " has not check into a saved location within the past " + user.check_in_period + " hours. Consider checking in on them?" for contact in all_contacts: num_to = "+1" + str(contact.phone) message = client.messages.create(to=num_to, from_=tw_num, body=body_text) sched.add_job(check_in_exp, 'interval', days=1) sched.start()