def send_task(name, **kwargs): """Serialize task data and send to mule.""" if not UWSGI: logger.warning("Not running within uwsgi. Task `%s` aborted", name) kwargs["__task_name"] = name data = convert_to_json(kwargs) uwsgi.mule_msg(data)
async def process_message(api, rtm, msg, loop): if "ok" in msg: return if msg["type"] != MsgType.MESSAGE or msg.get("subtype"): return # Don't react to bot's own messages :D if msg["user"] == rtm.bot_id: return print("Processing RTM message:", msg) text = msg["text"].strip() if text.startswith(rtm.bot_mention): bot_command = parse_command(text) await handle_bot_commands(api, rtm, bot_command, msg, loop) # Don't parse urls, the command might already did something with it return gerrit_urls = parse_gerrit_urls(text) if gerrit_urls: print(f"Found review links, adding them to queue: {gerrit_urls}") # with loop.create_task, it would raise an AssertionError: # File "/usr/lib/python3.6/asyncio/coroutines.py", line 276, in _format_coroutine # assert iscoroutine(coro) # AssertionError # https://bugs.python.org/issue34071 filtered_urls, existing_requests = await loop.run_in_executor( None, filter_existing_requests, gerrit_urls) loop.create_task(add_reaction(api, rtm, existing_requests, msg, loop)) await loop.run_in_executor(None, save_review_requests, msg, filtered_urls) uwsgi.mule_msg(MuleMessage.RELOAD)
def convert_source(id): source_id = int(request.form['source_id']) format = request.form['format'] # this is bit hack as user can have more roles role = current_user.roles[0].name if IN_UWSGI: uwsgi.mule_msg(current_user.email + '|' + role + '|' + str(source_id) + '|' + format) task_id = '' else: global delegated_client if not loop.is_running(): abort(500, 'Event loop is not running') with delegated_client_lock: if not delegated_client or not delegated_client.is_active(): delegated_client = DelegatedClient( current_app.config['DELEGATED_TOKEN'], current_app.config['DELEGATED_URI'], loop) task_id = delegated_client.call_no_wait(current_user.email, role, 'convert', source_id, format) if not task_id: abort(500, 'No task id') url = url_for('minimal.ebook_detail', id=id) flash(Markup('File was send for conversion %s- it\'ll take a while - <a href="%s">reload this page</a> later to view link to converted file' %\ ('' if not task_id else 'ref. %s '%task_id, url))) return redirect(url)
def convert_source(id): source_id = int(request.form['source_id']) format = request.form['format'] # this is bit hack as user can have more roles role = current_user.roles[0].name if IN_UWSGI: uwsgi.mule_msg(current_user.email+'|'+ role +'|'+str(source_id)+'|'+format) task_id='' else: global delegated_client if not loop.is_running(): abort(500, 'Event loop is not running') with delegated_client_lock: if not delegated_client or not delegated_client.is_active(): delegated_client= DelegatedClient(current_app.config['DELEGATED_TOKEN'], current_app.config['DELEGATED_URI'], loop) task_id=delegated_client.call_no_wait(current_user.email, role, 'convert', source_id, format ) if not task_id: abort(500, 'No task id') url = url_for('minimal.ebook_detail', id=id) flash(Markup('File was send for conversion %s- it\'ll take a while - <a href="%s">reload this page</a> later to view link to converted file' %\ ('' if not task_id else 'ref. %s '%task_id, url))) return redirect(url)
def real_call(self, *args, **kwargs): uwsgi.mule_msg( pickle.dumps({ 'service': 'uwsgi_mulefunc', 'func': self.fname, 'args': args, 'kwargs': kwargs }), self.mule)
def real_call(self, *args, **kwargs): uwsgi.mule_msg(pickle.dumps( { 'service': 'uwsgi_mulefunc', 'func': self.fname, 'args': args, 'kwargs': kwargs } ), self.mule)
def real_call(self, *args, **kwargs): uwsgi.mule_msg( pickle.dumps({ "service": "uwsgi_mulefunc", "func": self.fname, "args": args, "kwargs": kwargs, }), self.mule, )
def handle_signal(signum, frame): log.info('Received signal %d, exiting', signum) if uwsgi and 'mule_id' in dir(uwsgi) and uwsgi.mule_id() > 0: farms = os.environ.get(UWSGI_FARMS_VAR, None) if farms: for farm in farms.split(','): uwsgi.farm_msg(farm, SHUTDOWN_MSG) else: uwsgi.mule_msg(SHUTDOWN_MSG, uwsgi.mule_id()) exit.set()
def add_onetime_job(self, func: Union[str, FunctionType], when: Optional[int] = None, scheduled_by: Optional[str] = None, **kwargs) -> int: """Schedule a job to run at a later time. Args: func: The function to call when: Optional number of seconds to wait before starting job **kwargs: Arguments to pass through to called function Returns: int: job_id """ if when and isinstance(when, int): trigger = 'date' run_date = datetime.datetime.utcnow() + datetime.timedelta( seconds=when) else: trigger = None run_date = None with sqla_session() as session: job = Job() if run_date: job.scheduled_time = run_date session.add(job) session.flush() job_id = job.id kwargs['job_id'] = job_id kwargs['scheduled_by'] = scheduled_by if self.use_mule: try: import uwsgi except Exception as e: logger.exception("use_mule is set but not running in uwsgi") raise e args = dict(kwargs) if isinstance(func, FunctionType): args['func'] = str(func.__qualname__) else: args['func'] = str(func) args['trigger'] = trigger args['when'] = when args['id'] = str(job_id) uwsgi.mule_msg(json.dumps(args)) return job_id else: self._scheduler.add_job(func, trigger=trigger, kwargs=kwargs, id=str(job_id), run_date=run_date) return job_id
def webhook(): if not "token" in request.args: return render_template("403.html"), 403 api_key = request.args["token"] if api_key in config["github"]: logging.debug("Submitting repo update for {}".format(config["github"][api_key])) uwsgi.mule_msg(api_key) return Response("Update successful", 200, mimetype="text/plain") return render_template("403.html"), 403
def _mule_msg(stream, line, mule=None): data = _encode_mule_msg(stream, line) # Unfortunately this check has to come after the marshalling # unless we just want to make a conservative guess if len(data) > max_recv_size: return False # Either deliver to a specific mule msg queue # or the shared queue which will be handled by # the first available mule (yay!) if mule: return uwsgi.mule_msg(data, mule) return uwsgi.mule_msg(data)
def process_request(json_in): uid = str(uuid.uuid4()) json_in["id"] = uid uwsgi.queue_push(json.dumps(json_in)) # Actual content of message does not really matter # This is just to triger mule execution uwsgi.mule_msg("s") while not uwsgi.cache_exists(uid, CACHE_NAME): continue res = uwsgi.cache_get(uid, CACHE_NAME) uwsgi.cache_del(uid, CACHE_NAME) return Response(response=res, status=200, mimetype="application/json")
def remove_scheduled_job(self, job_id, abort_message="removed"): """Remove scheduled job from mule worker or local scheduler depending on setup.""" if self.use_mule: try: import uwsgi except Exception as e: logger.exception("use_mule is set but not running in uwsgi") raise e args = {"scheduler_action": "remove", "id": str(job_id)} uwsgi.mule_msg(json.dumps(args)) else: self.remove_local_job(job_id) with sqla_session() as session: job = session.query(Job).filter(Job.id == job_id).one_or_none() job.finish_abort(message=abort_message)
def add_onetime_job(self, func: Union[str, FunctionType], when: Optional[int] = None, **kwargs): """Schedule a job to run at a later time. Args: func: The function to call when: Optional number of seconds to wait before starting job **kwargs: Arguments to pass through to called function Returns: str: job_id """ job = Jobtracker() id = job.create({'status': JobStatus.SCHEDULED}) if when and isinstance(when, int): trigger = 'date' run_date = datetime.datetime.utcnow() + datetime.timedelta( seconds=when) else: trigger = None run_date = None kwargs['job_id'] = id if self.use_mule: try: import uwsgi except Exception as e: logger.exception("use_mule is set but not running in uwsgi") raise e args = dict(kwargs) if isinstance(func, FunctionType): args['func'] = str(func.__qualname__) else: args['func'] = str(func) args['trigger'] = trigger args['when'] = when args['id'] = id uwsgi.mule_msg(json.dumps(args)) return id else: self._scheduler.add_job(func, trigger=trigger, kwargs=kwargs, id=id, run_date=run_date) return id
def handle_finish_event(db, event, rules, time_ms, time_id): disable_finish = db.reg_get_int('disable_finish', 0) if not disable_finish: run_id = db.run_finished(event['event_id'], time_ms) if run_id is None: db.update('times', time_id, invalid=True) logging.info("Finish [FALSE]: %r", time_id) play_sound('sounds/FalseFinish.wav') else: db.set_run_recalc(run_id) uwsgi.mule_msg('recalc') logging.info("Finish: %r", time_id) play_sound('sounds/CarFinished.wav') else: db.update('times', time_id, invalid=True) logging.info("Finish [DISABLED]: %r", time_id) play_sound('sounds/FalseFinish.wav')
def handle_start_event(db, event, rules, time_ms, time_id): next_entry_id = db.reg_get_int('next_entry_id') db.reg_set('next_entry_id', None) db.reg_set('next_entry_msg', None) disable_start = db.reg_get_int('disable_start', 0) if not disable_start: run_id = db.run_started(event['event_id'], time_ms, next_entry_id) if run_id is None: db.update('times', time_id, invalid=True) logging.info("Start [FALSE]: %r", time_id) play_sound('sounds/FalseStart.wav') else: db.set_run_recalc(run_id) uwsgi.mule_msg('recalc') logging.info("Start: %r", time_id) play_sound('sounds/CarStarted.wav') else: db.update('times', time_id, invalid=True) logging.info("Start [DISABLED]: %r", time_id) play_sound('sounds/FalseStart.wav')
def convert_source(id): token = create_token(current_user, current_app.config['SECRET_KEY'], current_app.config['TOKEN_VALIDITY_HOURS']) source_id = int(request.form['source_id']) format = request.form['format'] if IN_UWSGI: uwsgi.mule_msg(token + '|' + str(source_id) + '|' + format) task_id = '' else: if not loop.is_running(): abort(500, 'Event loop is not running') client = WAMPClient(token, current_app.config['WAMP_URI'], loop=loop) try: task_id = client.call_no_wait('convert', source_id, format) finally: client.close() if not task_id: abort(500, 'No task id') url = url_for('minimal.ebook_detail', id=id) flash(Markup('File was send for conversion %s- it\'ll take a while - <a href="%s">reload this page</a> later to view link to converted file' %\ ('' if not task_id else 'ref. %s '%task_id, url))) return redirect(url)
def application(environ, start_response): """ WSGI application to handle 404's. """ # Remove wsgi.* items for key in environ.keys(): if key.startswith('wsgi.'): del environ[key] # Pass data to the mule uwsgi.mule_msg(json.dumps(environ)) # '' is the default route route_to = '' if environ['PATH_INFO'] in CONFIG['ROUTES'].keys(): route_to = environ['PATH_INFO'] status, html_file = map(str, CONFIG['ROUTES'][route_to]) # Return data to the user start_response(status, [('Content-Type', 'text/html')]) with open(html_file, 'r') as data: for line in data.readlines(): yield line
def control_ws(ws): uwsgi.mule_msg(b"bt scan on", 1) try: with app.request_context(ws.environ): if not session.get("logged_in"): return redirect( with_query_string(url_for('auth.login'), "next", request.url)) with suppress(SystemError, OSError): for raw_msg in iter(ws.receive, None): msg = json.loads(raw_msg.decode()) #for mule, dev in zip(cycle(range(1, NUM_BT_WORKERS+1)), msg["devices"]): for dev in msg["devices"]: uwsgi.mule_msg(b"bt send " + dev.encode() + b" " + pack("B", msg["angle"])) finally: uwsgi.mule_msg(b"bt scan off", 1)
def ping_worker(): pong.clear() uwsgi.mule_msg(b"bt ping", 1) return pong.wait(3)
def spool(self, *args, **kwargs): kwargs.update(ud_spool_func=self.__name__) uwsgi.mule_msg(json.dumps(kwargs))
def scores_page(): db = get_db() g.event = get_event(db) g.rules = get_rules(g.event) if g.event is None: flash("No active event!", F_ERROR) return redirect(url_for('events_page')) if g.rules is None: flash("Invalid rule set for active event!", F_ERROR) return redirect(url_for('events_page')) action = request.form.get('action') if action == 'event_recalc': db.set_event_recalc(g.event['event_id']) uwsgi.mule_msg('recalc') flash("Event scores recalculating") return redirect(url_for('scores_page')) elif action == 'entry_recalc': entry_id = request.form.get('entry_id') if not db.entry_exists(entry_id): flash("Invalid entry id", F_ERROR) return redirect(url_for('entries_page')) db.set_entry_recalc(entry_id) uwsgi.mule_msg('recalc') flash("Entry score recalculating") return redirect(url_for('scores_page')) elif action == 'entries_fill_dns': for entry_id in request.form.getlist('entry_id'): run_count = db.run_count(entry_id=entry_id, state=('started','finished','scored')) for i in range(g.rules.max_runs - run_count): flash("Add DNS [%s, %r]" % (entry_id,i)) run_id = db.insert('runs', event_id=g.event['event_id'], entry_id=entry_id, recalc=1, dns_dnf=1, state='scored') flash("Added new run [%r]" % run_id) uwsgi.mule_msg('recalc') return redirect(url_for('scores_page')) elif action == 'entries_trim_dns': return redirect(url_for('scores_page')) elif action is not None: flash("Unkown form action %r" % action, F_ERROR) return redirect(url_for('scores_page')) # sort entries into car class g.class_entry_list = {} g.entry_list = db.entry_list(g.event['event_id']) for entry in g.entry_list: if entry['car_class'] not in g.class_entry_list: g.class_entry_list[entry['car_class']] = [] if entry['scores_visible']: g.class_entry_list[entry['car_class']].append(entry) # sort each car class by event_time_ms and run_count for car_class in g.class_entry_list: g.class_entry_list[car_class].sort(cmp=entry_cmp) g.entry_run_list = {} for entry in g.entry_list: g.entry_run_list[entry['entry_id']] = db.run_list(entry_id=entry['entry_id'], state=('scored','finished','started'), limit=g.rules.max_runs) return render_template('admin_scores.html')
def disconnect(macaddr): if has_uwsgi: uwsgi.mule_msg(b"bt disconnect " + macaddr.upper().encode(), 1) return render_template("ctrl/connect.html", macaddr=macaddr, connecting=False)
def do_offload(params): mule_msg(dumps(params), mule)
def application(env, start_response): try: uwsgi.mule_msg(env['REQUEST_URI'], 1) except: pass req = uwsgi.workers()[uwsgi.worker_id() - 1]['requests'] uwsgi.setprocname("worker %d managed %d requests" % (uwsgi.worker_id(), req)) try: gc.collect(2) except: pass if DEBUG: print(env['wsgi.input'].fileno()) if env['PATH_INFO'] in routes: return routes[env['PATH_INFO']](env, start_response) if DEBUG: print(env['wsgi.input'].fileno()) try: gc.collect(2) except: pass if DEBUG: print(len(gc.get_objects())) workers = '' for w in uwsgi.workers(): apps = '<table border="1"><tr><th>id</th><th>mountpoint</th><th>startup time</th><th>requests</th></tr>' for app in w['apps']: apps += '<tr><td>%d</td><td>%s</td><td>%d</td><td>%d</td></tr>' % ( app['id'], app['mountpoint'], app['startup_time'], app['requests']) apps += '</table>' workers += """ <tr> <td>%d</td><td>%d</td><td>%s</td><td>%d</td><td>%d</td><td>%d</td><td>%s</td> </tr> """ % (w['id'], w['pid'], w['status'], w['running_time'] / 1000, w['avg_rt'] / 1000, w['tx'], apps) output = """ <img src="/logo"/> version %s running on %s (remote user: %s)<br/> <hr size="1"/> Configuration<br/> <iframe src="/config"></iframe><br/> <br/> Workers and applications<br/> <table border="1"> <tr> <th>wid</th><th>pid</th><th>status</th><th>running time</th><th>average</th><th>tx</th><th>apps</th> </tr> %s </table> """ % (uwsgi.version, uwsgi.hostname, env.get('REMOTE_USER', 'None'), workers) start_response('200 OK', [('Content-Type', 'text/html'), ('Content-Length', str(len(output)))]) # return bytes(output.encode('latin1')) return output
def events_page(): db = get_db() action = request.form.get('action') if action == 'activate': event_id = request.form.get('event_id') if db.event_exists(event_id): db.reg_set('active_event_id', event_id) flash("Set active event to %r" % event_id) else: flash("Invalid event_id", F_ERROR) return redirect(url_for('events_page')); elif action == 'deactivate': db.reg_set('active_event_id', None) flash("Event de-activated") return redirect(url_for('events_page')); elif action == 'update': event_id = request.form.get('event_id') if not db.event_exists(event_id): flash("Invalid event id", F_ERROR) return redirect(url_for('events_page')) event_data = {} for key in db.table_columns('events'): if key in ['event_id']: continue # ignore elif key in request.form: event_data[key] = request.form.get(key) db.update('events', event_id, **event_data) flash("Event changes saved") return redirect(url_for('events_page')) elif action == 'insert': event_data = {} for key in db.table_columns('events'): if key in ['event_id']: continue # ignore elif key in request.form: event_data[key] = request.form.get(key) event_id = db.insert('events', **event_data) flash("Added new event [%r]" % event_id) return redirect(url_for('events_page')) elif action == 'delete': if request.form.get('confirm_delete'): event_id = request.form.get('event_id') if db.event_exists(event_id): if db.reg_get('active_event_id') == event_id: db.reg_set('active_event_id',None) db.update("events", event_id, deleted=1) # FIXME do we need to propagate this to runs and entries? flash("Event deleted") else: flash("Invalid event_id for delete operation.", F_ERROR) else: flash("Delete ignored, no confirmation", F_WARN) return redirect(url_for('events_page')) elif action == 'recalc': event_id = request.form.get('event_id') if not db.event_exists(event_id): flash("Invalid event id", F_ERROR) return redirect(url_for('events_page')) # flag all entries for this event to be recalculated db.set_event_recalc(event_id) uwsgi.mule_msg('recalc') flash("Event scores recalculating") return redirect(url_for('events_page')) elif action is not None: flash("Invalid form action %r" % action, F_ERROR) return redirect(url_for('events_page')) g.event = get_event(db) g.event_list = db.select_all('events', deleted=0, _order_by='event_id') g.default_date = datetime.date.today().isoformat() g.default_season = datetime.date.today().year g.rule_sets = scoring_rules.get_rule_sets() g.default_rule_set = db.reg_get('default_rule_set') return render_template('admin_events.html')
def timing_page(): db = get_db() g.event = get_event(db) g.rules = get_rules(g.event) if g.event is None: flash("No active event!", F_ERROR) return redirect(url_for('events_page')) if g.rules is None: flash("Invalid rule set for active event!", F_ERROR) return redirect(url_for('events_page')) if 'entry_filter' in request.args: session['entry_filter'] = request.args.get('entry_filter') if session['entry_filter'] not in (None, 'all', 'noassign'): try: session['entry_filter'] = int(session['entry_filter']) except ValueError: flash("Invalid entry filter", F_ERROR) session['entry_filter'] = None if 'started_filter' in request.args: session['started_filter'] = bool(request.args.get('started_filter')) if 'finished_filter' in request.args: session['finished_filter'] = bool(request.args.get('finished_filter')) if 'scored_filter' in request.args: session['scored_filter'] = bool(request.args.get('scored_filter')) if 'tossout_filter' in request.args: session['tossout_filter'] = bool(request.args.get('tossout_filter')) if 'run_limit' in request.args: session['run_limit'] = parse_int(request.args.get('run_limit'), 20) action = request.form.get('action') if action == 'toggle_start': db.reg_toggle('disable_start', True) return redirect(url_for('timing_page')) elif action == 'toggle_finish': db.reg_toggle('disable_finish', True) return redirect(url_for('timing_page')) elif action == 'set_next': if request.form.get('next'): db.reg_set('next_entry_id',request.form.get('next')) db.reg_set('next_entry_msg', None) return redirect(url_for('timing_page')) elif action == 'clear_next': db.reg_set('next_entry_id', None) db.reg_set('next_entry_msg', None) return redirect(url_for('timing_page')) elif action == 'set_filter': session['entry_filter'] = request.form.get('entry_filter') session['started_filter'] = bool(request.form.get('started_filter')) session['finished_filter'] = bool(request.form.get('finished_filter')) session['scored_filter'] = bool(request.form.get('scored_filter')) session['tossout_filter'] = bool(request.form.get('tossout_filter')) session['run_limit'] = parse_int(request.form.get('run_limit'), 20) if session['entry_filter'] not in (None, 'all', 'noassign'): try: session['entry_filter'] = int(session['entry_filter']) except ValueError: flash("Invalid entry filter", F_ERROR) session['entry_filter'] = None return redirect(url_for('timing_page')) elif action == 'reset_filter': session['entry_filter'] = None session['started_filter'] = True session['finished_filter'] = True session['scored_filter'] = True session['tossout_filter'] = True session['run_limit'] = 20 return redirect(url_for('timing_page')) elif action == 'set_max_runs': # FIXME consider removing this so we only set max runs on events page try: max_runs = int(request.form.get('max_runs','')) except ValueError: flash("Invalid max runs", F_ERROR) else: db.update('events', g.event['event_id'], max_runs=max_runs) # make sure we recalculate all entries event totals based on new max runs # this is mainly needed with drop runs > 0 db.set_event_recalc(g.event['event_id']) uwsgi.mule_msg('recalc') flash("Event scores recalculating") return redirect(url_for('timing_page')) elif action == 'update': run_id = request.form.get('run_id') if not db.run_exists(run_id): flash("Invalid run id", F_ERROR) return redirect(url_for('timing_page')) run_data = {'dns_dnf':0} # add default for dns_dnf checkboxes if not selected for key in db.table_columns('runs') + ['start_time','finish_time','split_1_time','split_2_time']: if key in ['run_id']: continue # ignore elif key == 'start_time': try: run_data['start_time_ms'] = parse_time_ex(request.form.get(key)) except: logging.debug(request.form.get(key)) flash("Invalid start time, start time not changed.", F_ERROR) elif key == 'finish_time': if request.form.get('old_state') != 'started': try: run_data['finish_time_ms'] = parse_time_ex(request.form.get(key)) except: logging.debug(request.form.get(key)) flash("Invalid finish time, finish time not changed.", F_ERROR) elif key == 'split_1_time': try: run_data['split_1_time_ms'] = parse_time_ex(request.form.get(key)) except: logging.debug(request.form.get(key)) flash("Invalid split 1 time, split 1 time not changed.", F_ERROR) elif key == 'split_2_time': try: run_data['split_2_time_ms'] = parse_time_ex(request.form.get(key)) except: logging.debug(request.form.get(key)) flash("Invalid split 2 time, split 2 time not changed.", F_ERROR) elif key == 'state' and request.form.get('state') == request.form.get('old_state'): pass # ignore setting state so we dont clobber a finish elif key == 'entry_id' and request.form.get(key) == 'None': run_data[key] = None elif key == 'dns_dnf' and request.form.get(key) and request.form.get('state') == 'started': pass # ignore setting dns_dnf if we are still in the started state elif key in request.form: run_data[key] = clean_str(request.form.get(key)) if request.form.get('state') == 'started' and run_data.get('start_time_ms') == 0: run_data['start_time_ms'] = None if request.form.get('state') == 'started' and run_data.get('finsih_time_ms') == 0: run_data['finish_time_ms'] = None if run_data.get('state', request.form.get('old_state')) == 'scored' and run_data.get('finish_time_ms') is None and run_data.get('dns_dnf') == 0: flash("Invalid raw time, unable to set state to 'scored'", F_ERROR) flash("Setting state to 'finished'", F_WARN) run_data['state'] = 'finished' db.update('runs', run_id, **run_data) flash("Run changes saved") old_entry_id = request.form.get('old_entry_id') if old_entry_id != request.form.get('entry_id') and old_entry_id != 'None': db.set_entry_recalc(old_entry_id) uwsgi.mule_msg('recalc') flash("Old entry recalc") db.set_run_recalc(run_id) uwsgi.mule_msg('recalc') flash("Run recalc") if 'entry_id' in run_data and run_data['entry_id'] is not None: db.set_entry_recalc(run_data['entry_id']) uwsgi.mule_msg('recalc') flash("Entry recalc") return redirect(url_for('timing_page')) elif action == 'insert': run_data = {} for key in db.table_columns('runs'): if key in ['run_id']: continue # ignore elif key in request.form: run_data[key] = clean_str(request.form.get(key)) run_id = db.insert('runs', **run_data) db.set_run_recalc(run_id) uwsgi.mule_msg('recalc') flash("Added new run [%r]" % run_id) return redirect(url_for('timing_page')) elif action is not None: flash("Invalid form action %r" % action, F_ERROR) return redirect(url_for('timing_page')) if 'entry_filter' not in session: session['entry_filter'] = None if 'started_filter' not in session: session['started_filter'] = True if 'finished_filter' not in session: session['finished_filter'] = True if 'scored_filter' not in session: session['scored_filter'] = True if 'tossout_filter' not in session: session['tossout_filter'] = True if 'hide_invalid_times' not in session: session['hide_invalid_times'] = False if 'run_limit' not in session: session['run_limit'] = 20 state_filter = [] if session['started_filter']: state_filter += ['started'] if session['finished_filter']: state_filter += ['finished'] if session['scored_filter']: state_filter += ['scored'] if session['tossout_filter']: state_filter += ['tossout'] g.run_list = db.run_list(event_id=g.event['event_id'], entry_id=session['entry_filter'], state=state_filter, sort='d', limit=session['run_limit']) g.entry_list = db.entry_list(g.event['event_id']) g.cars_started = db.run_count(event_id=g.event['event_id'], state='started') g.cars_finished = db.run_count(event_id=g.event['event_id'], state='finished') g.disable_start = db.reg_get_int('disable_start', 0) g.disable_finish = db.reg_get_int('disable_finish', 0) g.next_entry_id = db.reg_get_int('next_entry_id') g.next_entry = db.select_one('entries', entry_id=g.next_entry_id) if g.next_entry_id is None: g.next_entry_run_number = None else: # FIXME change this to max of run_number instead of count g.next_entry_run_number = 1 + db.run_count(entry_id=g.next_entry_id, state=('started','finished','scored')) g.barcode_scanner_status = db.reg_get('barcode_scanner_status') g.tag_heuer_status = db.reg_get('tag_heuer_status') g.rfid_reader_status = db.reg_get('rfid_reader_status') # FIXME change how watchdog is handled g.hardware_ok = True g.start_ready = g.hardware_ok and (g.tag_heuer_status == 'Open') and not g.disable_start g.finish_ready = g.hardware_ok and (g.tag_heuer_status == 'Open') and not g.disable_finish g.run_group = db.reg_get('run_group') g.next_entry_msg = db.reg_get('next_entry_msg') # create car description strings for entries g.car_dict = {} for entry in g.entry_list: g.car_dict[entry['entry_id']] = "%s %s %s %s" % (entry['car_color'], entry['car_year'], entry['car_make'], entry['car_model']) return render_template('admin_timing.html')
def send(self, message): uwsgi.mule_msg(message, self.id)
def send_message(self, msg): if self.uwsgi_available: uwsgi.mule_msg(msg) else: print('warning: uwsgi not available')
def log_uwsgi(self, sender, response, **kwargs): msg = self.create_log_message(response) picklestring = pickle.dumps(msg) uwsgi.mule_msg(picklestring,1)
def execute_async(self): mule_id = self.setup.get('mule', self.default_mule_id) return uwsgi.mule_msg(self.get_message_content(), mule_id)
def application(env, start_response): try: uwsgi.mule_msg(env["REQUEST_URI"], 1) except: pass req = uwsgi.workers()[uwsgi.worker_id() - 1]["requests"] uwsgi.setprocname("worker %d managed %d requests" % (uwsgi.worker_id(), req)) gc.collect(2) if DEBUG: print(env["wsgi.input"].fileno()) if routes.has_key(env["PATH_INFO"]): return routes[env["PATH_INFO"]](env, start_response) start_response("200 OK", [("Content-Type", "text/html")]) if DEBUG: print(env["wsgi.input"].fileno()) gc.collect(2) if DEBUG: print(len(gc.get_objects())) workers = "" for w in uwsgi.workers(): apps = '<table border="1"><tr><th>id</th><th>mountpoint</th><th>requests</th></tr>' for app in w["apps"]: apps += "<tr><td>%d</td><td>%s</td><td>%d</td></tr>" % (app["id"], app["mountpoint"], app["requests"]) apps += "</table>" workers += """ <tr> <td>%d</td><td>%d</td><td>%s</td><td>%d</td><td>%d</td><td>%d</td><td>%s</td> </tr> """ % ( w["id"], w["pid"], w["status"], w["running_time"] / 1000, w["avg_rt"] / 1000, w["tx"], apps, ) return """ <img src="/logo"/> version %s<br/> <hr size="1"/> Configuration<br/> <iframe src="/config"></iframe><br/> <br/> Dynamic options<br/> <iframe src="/options"></iframe><br/> <br/> Workers and applications<br/> <table border="1"> <tr> <th>wid</th><th>pid</th><th>status</th><th>running time</th><th>average</th><th>tx</th><th>apps</th> </tr> %s </table> """ % ( uwsgi.version, workers, )
def real_call(self, *args, **kwargs): uwsgi.mule_msg( pickle.dumps({"service": "uwsgi_mulefunc", "func": self.fname, "args": args, "kwargs": kwargs}), self.mule )
def application(env, start_response): try: uwsgi.mule_msg(env['REQUEST_URI'], 1) except: pass req = uwsgi.workers()[uwsgi.worker_id()-1]['requests'] uwsgi.setprocname("worker %d managed %d requests" % (uwsgi.worker_id(), req)) try: gc.collect(2) except: pass if DEBUG: print(env['wsgi.input'].fileno()) if env['PATH_INFO'] in routes: return routes[env['PATH_INFO']](env, start_response) if DEBUG: print(env['wsgi.input'].fileno()) try: gc.collect(2) except: pass if DEBUG: print(len(gc.get_objects())) workers = '' for w in uwsgi.workers(): apps = '<table border="1"><tr><th>id</th><th>mountpoint</th><th>startup time</th><th>requests</th></tr>' for app in w['apps']: apps += '<tr><td>%d</td><td>%s</td><td>%d</td><td>%d</td></tr>' % (app['id'], app['mountpoint'], app['startup_time'], app['requests']) apps += '</table>' workers += """ <tr> <td>%d</td><td>%d</td><td>%s</td><td>%d</td><td>%d</td><td>%d</td><td>%s</td> </tr> """ % (w['id'], w['pid'], w['status'], w['running_time']/1000, w['avg_rt']/1000, w['tx'], apps) output = """ <img src="/logo"/> version %s running on %s (remote user: %s)<br/> <hr size="1"/> Configuration<br/> <iframe src="/config"></iframe><br/> <br/> Dynamic options<br/> <iframe src="/options"></iframe><br/> <br/> Workers and applications<br/> <table border="1"> <tr> <th>wid</th><th>pid</th><th>status</th><th>running time</th><th>average</th><th>tx</th><th>apps</th> </tr> %s </table> """ % (uwsgi.version, uwsgi.hostname, env.get('REMOTE_USER','None'), workers) start_response('200 OK', [('Content-Type', 'text/html'), ('Content-Length', str(len(output)) )]) #return bytes(output.encode('latin1')) return output
def bluetooth_restart(): if has_uwsgi: uwsgi.mule_msg(b"bt restart", 1) return redirect(url_for(".bluetooth"))
def reload_mule(sender, **kwargs): from bot import MuleMessage uwsgi.mule_msg(MuleMessage.RELOAD)
def penalties_page(): db = get_db() g.event = get_event(db) g.rules = get_rules(g.event) if g.event is None: flash("No active event!", F_ERROR) return redirect(url_for('events_page')) if g.rules is None: flash("Invalid rule set for active event!", F_ERROR) return redirect(url_for('events_page')) action = request.form.get('action') if action == 'insert': entry_id = request.form.get('entry_id') penalty_time = request.form.get('penalty_time') penalty_note = request.form.get('penalty_note') if not db.entry_exists(entry_id): flash("Invalid entry id", F_ERROR) return redirect(url_for('penalties_page')) try: time_ms = parse_time_ex(penalty_time) except: flash("Invalid penalty time", F_ERROR) return redirect(url_for('penalties_page')) if time_ms is None: time_ms = 0 db.insert('penalties', event_id=g.event['event_id'], entry_id=entry_id, time_ms=time_ms, penalty_note=penalty_note) flash("Penalty added") db.set_entry_recalc(entry_id) uwsgi.mule_msg('recalc') flash("Entry recalculating") return redirect(url_for('penalties_page')) elif action == 'update': penalty_id = request.form.get('penalty_id') entry_id = request.form.get('entry_id') penalty_time = request.form.get('penalty_time') penalty_note = request.form.get('penalty_note') old_penalty = db.select_one('penalties', penalty_id=penalty_id) if penalty_id is None: flash("Invalid penalty id", F_ERROR) return redirect(url_for('penalties_page')) if not db.entry_exists(entry_id): flash("Invalid entry id", F_ERROR) return redirect(url_for('penalties_page')) try: time_ms = parse_time_ex(penalty_time) except: flash("Invalid penalty time", F_ERROR) return redirect(url_for('penalties_page')) if time_ms is None: time_ms = 0 db.update('penalties', penalty_id, entry_id=entry_id, time_ms=time_ms, penalty_note=penalty_note) flash("Penalty updated") if old_penalty['entry_id'] != parse_int(entry_id): # update previous entry flash("old entry recalc, %r != %r" % (old_penalty['entry_id'],entry_id)) db.set_entry_recalc(old_penalty['entry_id']) uwsgi.mule_msg('recalc') # update current entry db.set_entry_recalc(entry_id) uwsgi.mule_msg('recalc') flash("entry recalc") return redirect(url_for('penalties_page')) elif action == 'delete': penalty_id = request.form.get('penalty_id') old_penalty = db.select_one('penalties', penalty_id=penalty_id) if old_penalty is None: flash("Invalid penalty id", F_ERROR) return redirect(url_for('penalties_page')) db.update('penalties', penalty_id, deleted=1) flash("Penalty deleted") # update previous entry flash("old entry recalc") db.set_entry_recalc(old_penalty['entry_id']) uwsgi.mule_msg('recalc') return redirect(url_for('penalties_page')) elif action is not None: flash("Unkown form action %r" % action, F_ERROR) return redirect(url_for('penalties_page')) g.penalty_list = db.select_all('penalties', event_id=g.event['event_id'], deleted=0, _order_by="penalty_id") g.entry_list = db.entry_list(g.event['event_id']) return render_template('admin_penalties.html')
def add_onetime_job(self, func: Union[str, FunctionType], when: Optional[int] = None, scheduled_by: Optional[str] = None, **kwargs) -> int: """Schedule a job to run at a later time on the mule worker or local scheduler depending on setup. Some extra checks against kwargs are performed here. If kwarg with name 'dry_run' is included, (dry_run) is appended to function name. If kwarg job_comment or job_ticket_ref are included, those fields in the job will be populated. Args: func: The function to call when: Optional number of seconds to wait before starting job scheduled_by: Username that scheduled the job **kwargs: Arguments to pass through to called function Returns: int: job_id """ if when and isinstance(when, int): trigger = 'date' run_date = datetime.datetime.utcnow() + datetime.timedelta( seconds=when) else: trigger = None run_date = None if isinstance(func, FunctionType): func_qualname = str(func.__qualname__) else: func_qualname = str(func) func_name = func_qualname.split(':')[-1] try: json.dumps(kwargs) except TypeError as e: raise TypeError("Job args must be JSON serializable: {}".format(e)) # Append (dry_run) to function name if set, so we can distinguish dry_run jobs try: if kwargs['kwargs']['dry_run']: func_name += " (dry_run)" except Exception: pass with sqla_session() as session: job = Job() if run_date: job.scheduled_time = run_date job.function_name = func_name if scheduled_by is None: scheduled_by = 'unknown' job.scheduled_by = scheduled_by job_comment = kwargs['kwargs'].pop('job_comment', None) if job_comment and isinstance(job_comment, str): job.comment = job_comment[:255] job_ticket_ref = kwargs['kwargs'].pop('job_ticket_ref', None) if job_ticket_ref and isinstance(job_comment, str): job.ticket_ref = job_ticket_ref[:32] job.start_arguments = kwargs['kwargs'] session.add(job) session.flush() job_id = job.id kwargs['job_id'] = job_id kwargs['scheduled_by'] = scheduled_by if self.use_mule: try: import uwsgi except Exception as e: logger.exception("use_mule is set but not running in uwsgi") raise e args = dict(kwargs) args['func'] = func_qualname args['trigger'] = trigger args['when'] = when args['id'] = str(job_id) uwsgi.mule_msg(json.dumps(args)) return job_id else: self.add_local_job(func, trigger=trigger, kwargs=kwargs, id=str(job_id), run_date=run_date, name=func_qualname) return job_id