def start_forced_fap(fap): SchedulerState.clear_user_app_queue() SchedulerState.set_app_started_at() name = fap['name'] params = fap['params'] app = { 'name': name, 'username': '******', 'userid': '>>>>FORCED<<<<', 'params': fap['params'], 'task_id': start_forced_fap.request.id, 'last_alive': time.time(), 'started_at': datetime.datetime.utcnow().isoformat(), 'is_default': False, 'is_forced': True, 'expire_at': str(datetime.datetime.utcnow() + datetime.timedelta(weeks=52)) } SchedulerState.set_current_app(app) SchedulerState.set_event_lock(False) fap = globals()[name](app['username'], app['userid']) try: fap.run(params=params) return True finally: fap.close() redis.set(SchedulerState.KEY_FORCED_APP, 'False') SchedulerState.set_current_app({})
def disable_frontage(self): SchedulerState.clear_user_app_queue() c_app = SchedulerState.get_current_app() if c_app: self.stop_app(c_app, Fap.CODE_CLOSE_APP, 'The admin disabled Arbalet Frontage')
def admin_clear_queue(user): if is_admin(user): SchedulerState.stop_app_request(user) SchedulerState.clear_user_app_queue() else: abort(403, "Forbidden Bru") return '', 204
def admin_app_force(user): req = request.get_json() if 'name' not in req: abort(415, 'Missing application name') name = req['name'] params = req.get('params', {}) forced = False if is_admin(user): if SchedulerState.set_forced_app_request(name, params): SchedulerState.clear_user_app_queue() SchedulerState.stop_app_request(user) forced = True return jsonify(keep_alive_delay=SchedulerState.DEFAULT_KEEP_ALIVE_DELAY, forced=forced) else: abort(403, "Forbidden Bru")
def check_app_scheduler(self): # check keep alive app (in user waiting app Q) self.keep_alive_waiting_app() # collect usefull struct & data queue = SchedulerState.get_user_app_queue() # User waiting app c_app = SchedulerState.get_current_app() # Current running app now = datetime.datetime.now() forced_app = SchedulerState.get_forced_app_request() # Is a app running ? if c_app: close_request, close_userid = SchedulerState.get_close_app_request() if close_request: message = Fap.CODE_CLOSE_APP if close_userid != c_app['userid'] else None print_flush('## Stopping app upon user reques [check_app_scheduler]') self.stop_app(c_app, message, 'Executing requested app closure') redis.set(SchedulerState.KEY_STOP_APP_REQUEST, '{}') return if len(forced_app) > 0 and not SchedulerState.get_forced_app(): print_flush('## Closing previous app for forced one [check_app_scheduler]') SchedulerState.clear_user_app_queue() self.stop_app(c_app, Fap.CODE_CLOSE_APP, 'The admin started a forced app') return # do we kill an old app no used ? ? if self.keep_alive_current_app(c_app): return # is expire soon ? if not c_app.get('is_default', False) and now > (datetime.datetime.strptime(c_app['expire_at'], "%Y-%m-%d %H:%M:%S.%f") - datetime.timedelta(seconds=EXPIRE_SOON_DELAY)): if not SchedulerState.get_expire_soon(): Fap.send_expires_soon(EXPIRE_SOON_DELAY, c_app['username'], c_app['userid']) # is the current_app expired ? if self.app_is_expired(c_app) or c_app.get('is_default', False): # is the current_app a FORCED_APP ? if SchedulerState.get_forced_app(): print_flush('## Stopping expired forced app [check_app_scheduler]') self.stop_app(c_app) return # is some user-app are waiting in queue ? if len(queue) > 0: next_app = queue[0] self.stop_current_app_start_next(queue, c_app, next_app) return else: # is a defautl scheduled app ? if c_app.get('is_default', False) and self.app_is_expired(c_app): print_flush('## Stopping expired default scheduled app [check_app_scheduler]') self.stop_app(c_app) return # it's a USER_APP, we let it running, do nothing else: pass else: if len(forced_app) > 0 and not SchedulerState.get_forced_app(): print_flush("## Starting {} [FORCED]".format(forced_app['name'])) SchedulerState.set_event_lock(True) SchedulerState.clear_forced_app_request() start_forced_fap.apply_async(args=[forced_app], queue='userapp') redis.set(SchedulerState.KEY_FORCED_APP, 'True') return # is an user-app waiting in queue to be started ? elif len(queue) > 0: SchedulerState.set_event_lock(True) start_fap.apply_async(args=[queue[0]], queue='userapp') print_flush(" Starting {} [QUEUE]".format(queue[0]['name'])) return else: return self.start_default_app()