def callback(crontab_id): crontab = _get_crontab(crontab_id) if not crontab: return '' data = request.get_json() status = data.get('status', '') container_id = data.get('container_id', '') if not container_id: return '' if status == 'start': cronjob = CronJob.get_by_container_id(container_id) if not cronjob: crontab.add_job(container_id) elif status == 'die': cronjob = CronJob.get_by_container_id(container_id) if not cronjob: cronjob = crontab.add_job(container_id) cronjob.set_status('finished') eru.remove_containers([ container_id, ]) return ''
def callback(crontab_id): crontab = _get_crontab(crontab_id) if not crontab: return "" data = request.get_json() status = data.get("status", "") container_id = data.get("container_id", "") if not container_id: return "" if status == "start": cronjob = CronJob.get_by_container_id(container_id) if not cronjob: crontab.add_job(container_id) elif status == "die": cronjob = CronJob.get_by_container_id(container_id) if not cronjob: cronjob = crontab.add_job(container_id) cronjob.set_status("finished") eru.remove_containers([container_id]) return ""
def run_crontab(crontab_id): crontab = Crontab.get(crontab_id) if not crontab: return # debug 打开并不真正执行任务. if CRONTAB_DEBUG: return props = crontab.props callback_url = url_for('crontab.callback', crontab_id=crontab.id, _external=True) try: r = eru.deploy_private( props.get('group', ''), props.get('pod', ''), props.get('appname', ''), 1, 1, props.get('version', ''), props.get('entrypoint', ''), props.get('env', ''), props.get('network_ids', []), callback_url=callback_url, ) except EruException as e: print e return task_id = r['tasks'][0] while True: time.sleep(1) try: task = eru.get_task(task_id) if not task['finished']: continue container_id = task['props']['container_ids'][0] cronjob = CronJob.get_by_container_id(container_id) if not cronjob: crontab.add_job(container_id) break except EruException as e: print e break