def run_job(self, I, action_id): if I.lastjob and (not I.lastjob.completed): if not ( action_id == JOB_ACTION['STOP_INSTANCE'] and I.lastjob.canstop ): # TODO: status = 100, timeout > 60s timeout = I.lastjob.created + relativedelta.relativedelta(seconds=+60) if timeout > datetime.now(): return self.trans(_("Previous task is not finished !")) # Create new job job = Job( user = self.current_user, target_type = JOB_TARGET['INSTANCE'], target_id = I.id, action = action_id ) self.db2.add(job) self.db2.commit() I.lastjob = job try: self._job_notify( job.id ) except Exception, e: #[Errno 113] No route to host # TODO: should be a config value job.status = settings.JOB_S_FAILED return self.trans(_("Connect to control server failed: %s")) % e
def run_job(self, id, action_id): # To prevent duplication old = self.db2.query(Job).filter( and_( Job.user_id == self.current_user.id, Job.target_type == JOB_TARGET['INSTANCE'], Job.target_id == id, Job.action == action_id, Job.status == settings.JOB_S_INITIATED ) ).all() if old: return (None, _("Can not run duplication job!")) # TODO: when a job is running, can not rerun also ! # Create new job job = Job( user = self.current_user, target_type = JOB_TARGET['INSTANCE'], target_id = id, action = action_id ) self.db2.add(job) self.db2.commit() try: self._job_notify( job.id ) except Exception, e: #[Errno 113] No route to host # TODO: should be a config value job.status = settings.JOB_S_FAILED self.db2.commit() return (None, _("Connect to control server error: %s") % e)
def run_job(self, I, action_id): if I.lastjob and (not I.lastjob.completed): if not (action_id == JOB_ACTION['STOP_INSTANCE'] and I.lastjob.canstop): # TODO: status = 100, timeout > 60s timeout = I.lastjob.created + relativedelta.relativedelta( seconds=+60) if timeout > datetime.now(): return self.trans(_("Previous task is not finished !")) # Create new job job = Job(user=self.current_user, target_type=JOB_TARGET['INSTANCE'], target_id=I.id, action=action_id) self.db2.add(job) self.db2.commit() I.lastjob = job try: self._job_notify(job.id) except Exception, e: #[Errno 113] No route to host # TODO: should be a config value job.status = settings.JOB_S_FAILED return self.trans(_("Connect to control server failed: %s")) % e
def run_job(self, I, action_id): lastjob = self.get_instance_lastjob(I) if lastjob: timeout = lastjob.created + relativedelta.relativedelta(seconds=+60) if not lastjob.completed: if not (action_id == JOB_ACTION["STOP_INSTANCE"] and lastjob.canstop): # TODO: status = 100, timeout > 60s if timeout > datetime.datetime.now(): return self.trans(_("Previous task is not finished !")) if lastjob.status == settings.JOB_S_FAILED: if timeout > datetime.datetime.now(): return self.trans(_("Previous task was failed, wait a moment please.")) # Create new job job = Job(user=self.current_user, target_type=JOB_TARGET["INSTANCE"], target_id=I.id, action=action_id) self.db.add(job) self.db.commit() try: self._job_notify(job.id) except Exception, e: # [Errno 113] No route to host # TODO: should be a config value job.status = settings.JOB_S_FAILED self.db.commit() return _("Connect to control server failed: %s") % e
def get(self, ID): self.set_header("Cache-Control", "no-cache") self.set_header("Pragma", "no-cache") self.set_header("Expires", "-1") N = self.db2.query(Node).get(ID) if not N: return self.write( self.trans(_('Can not find node %s.')) % ID ) action_id = JOB_ACTION['DISABLE_NODE'] if N.isenable else JOB_ACTION['ENABLE_NODE'] job = Job( user = self.current_user, target_type = JOB_TARGET['NODE'], target_id = ID, action = action_id ) self.db2.add(job) self.db2.commit() try: self._job_notify( job.id ) N.isenable = not N.isenable self.db2.commit() # no news is good news except Exception, e: self.write( self.trans(_('Run job failed: %s')) % e )
def post(self, ID): N = self.db2.query(Node).get(ID) if not N: return self.write( self.trans(_('Can not find node %s.')) % ID ) ERROR = [] form = NodeEditForm(self) if form.validate(): # TODO: check the node ability ! N.vmemory = form.vmemory.data * 1024 * 1024 # KB N.vcpus = form.vcpus.data job = Job( user = self.current_user, target_type = JOB_TARGET['NODE'], target_id = ID, action = JOB_ACTION.get('UPDATE_NODE') ) self.db2.add(job) self.db2.commit() try: self._job_notify( job.id ) url = self.reverse_url('admin:node') return self.redirect(url) except Exception, e: ERROR.append( self.trans(_('Run job failed: %s')) % e )
def new_job(): if request.method == 'GET': return jsonify({'elements': [element.to_json() for element in Job.query.all()]}) else: data = request.get_json(force=True) name = data.get('name') description = data.get('description') pass_code = data.get('pass_code') if pass_code is None or name is None or Job.query.filter_by(name=name).first() is not None: abort(400) # missing arguments or existing one job = Job(name=name, description=description) db.session.add(job) db.session.commit() return jsonify({'element': job.to_json()}), 201
def create_job(**kwargs) -> Job: """ Create new job. Returns: Job created. """ new_job = Job(**kwargs) return JobRepository.create(new_job)
def get(self, id): action = self.get_argument_int("action", 0) node = self.db2.query(Node).get(id) if not node: return self.write('No such node!') if not action: return self.write( self.trans(_("No action specified")) ) elif action == 1: if node.isenable: return self.write('Already enable !') else: action_id = JOB_ACTION['ENABLE_NODE'] #self.new_job(JOB_TARGET['NODE'], id, JOB_ACTION['ENABLE_NODE']) elif action == 2: if not node.isenable: return self.write('Already disable !') else: action_id = JOB_ACTION['DISABLE_NODE'] #self.new_job(JOB_TARGET['NODE'], id, JOB_ACTION['DISABLE_NODE']) else: return self.write('Unknow action!') job = Job( user = self.current_user, target_type = JOB_TARGET['NODE'], target_id = id, action = action_id ) self.db2.add(job) self.db2.commit() self._job_notify( job.id ) return self.write('Action success !')
#!flask/bin/python from app import db from app.job.models import Job import json with open("jobs.json", "r") as jobs_json: jobs = json.load(jobs_json) for job in jobs: db.session.add(Job(name=job["name"], description=job["description"])) db.session.commit()