Example #1
0
def resources():
    group = gevent.pool.Group()
    source_view = gevent.spawn(
        flask.copy_current_request_context(cloud_view),
        hooks.source,
    )
    if "destination" not in flask.current_app.config["CLOUDS"]:
        gevent.wait([source_view])
        return flask.jsonify(
            reset=flask.current_app.config["CLOUDS_RESET"],
            source=source_view.value,
            # TODO(akscram): A set of hosts that don't belong to any cloud.
            hosts=[],
            # TODO(akscram): A set of current events.
            events=[],
        )
    destination_view = gevent.spawn(
        flask.copy_current_request_context(cloud_view),
        hooks.destination,
    )
    gevent.wait([source_view, destination_view])
    return flask.jsonify(
        reset=flask.current_app.config["CLOUDS_RESET"],
        source=source_view.value,
        destination=destination_view.value,
        # TODO(akscram): A set of hosts that don't belong to any cloud.
        hosts=[],
        # TODO(akscram): A set of current events.
        events=[],
    )
Example #2
0
def _(*args):
    def wrapuser():
        uniq_events = set()
        uniq_events.update([('1#enter', 'talk', 'get')])
        uniq_events.remove(('1#enter', 'talk', 'get'))
        event_data = {}
        for ev in uniq_events:
            comp = getattr(chat, ev[1])
            if ev[2] is None:
                ename = ev[0]
                raise GetterNotDefined('{ctype} has no getter associated with event "on_{ename}"'
                                       .format(ctype=type(comp), ename=ename[ename.find('#') + 1:]))
            getter = getattr(comp, ev[2])
            event_data[ev[0]] = getter()

        event_data['1#enter'] = chat.talk._get(
            msgpack.unpackb(bytes(args[0]['data']), encoding='utf8')
        )

        user_args = []
        user_args.append(event_data['1#enter'])
        chat.entered(*user_args)

    foo = copy_current_request_context(wrapuser)
    eventlet.spawn(foo)
Example #3
0
def index():
    if request.method == 'GET':
        # 查看当前用户是否登陆
        if not session.get("username"):
            return redirect(url_for('login'), 302)
        if 'username' in session:
            print("用户连接成功,session为:%s" % session['username'])
            # 管理员添加任务
            host = request.args.get('host')
            if host:
                username = request.args.get('username')
                password = request.args.get('password')
                command = request.args.get('command')
                link_server = flask.copy_current_request_context(
                    remote_read_log.link_server_client)
                if host and username and password and command is not None:
                    index_threads = []
                    index_th = threading.Thread(target=link_server,
                                                args=(host, username, password,
                                                      command, 'custom'))
                    index_threads.append(index_th)
                    for i in range(len(index_threads)):
                        index_threads[i].start()
                    for i in range(len(index_threads)):
                        index_threads[i].join()
            else:
                return render_template('index.html', name=session['username'])
    else:
        if 'username' in session:
            return render_template('index.html', name=session['username'])
Example #4
0
 def _prepare_fn(self, fn, force_copy=False, skip_request_context=False):
     if isinstance(self._self, concurrent.futures.ThreadPoolExecutor) \
         or force_copy:
         if not skip_request_context:
             fn = copy_current_request_context(fn)
         fn = copy_current_app_context(fn)
     return fn
Example #5
0
            def handler(*args):
                def wrapuser():
                    event_data = {}
                    for event in uniq_events:
                        comp = COMPONENT_REGISTRY[event.uuid]
                        # we already checked that this component has a getter
                        event_data[event.signal] = getattr(comp, event.getter)()

                    # if there is no getter, then there is no data to unpack
                    # if there is a getter, then we need to unpack the data sent
                    main_getter = main_event.getter
                    if main_getter is not None:
                        comp = COMPONENT_REGISTRY[main_event.uuid]
                        event_data[main_event.signal] = getattr(comp, '_' + main_getter)(
                            msgpack.unpackb(args[0], encoding='utf8')
                        )

                    # gather the remaining data from the other events through their getter methods
                    for events, func in supports:
                        if main_getter is not None:
                            func(*(event_data[event.signal] for event in events))
                        else:
                            func()

                # TODO replace with flask socketio start_background_task
                eventlet.spawn(copy_current_request_context(wrapuser))
Example #6
0
def start_voucher_job(customer_list):
    LOGGER.info(customer_list)
    # process_voucher_list(customer_list.decode('utf-8'))
    # Process list in background
    job = copy_current_request_context(process_voucher_list)
    executor.submit(job, customer_list.decode('utf-8'))
    return True
Example #7
0
	def async_func(*args, **kwargs):
		func_hl = Thread(
            target = copy_current_request_context(func),
            args = args,
            kwargs = kwargs)
		func_hl.start()
		return func_hl
Example #8
0
File: work.py Project: Answeror/aip
def group_app_task(redis, lock, name, appops, timeout):
    log.debug('group app task {} start', name)
    import pickle
    from flask import copy_current_request_context
    from . import make_slave_app
    from .local import core
    app = make_slave_app(appops)
    with app.app_context():
        while True:
            refresh_group_app_task_lock(redis, lock, name)
            message = redis.blpop(
                ':'.join([core.group_app_task_key, name]),
                timeout=timeout
            )
            if message is None:
                break
            task, args, kargs = pickle.loads(message[1])
            try:
                with app.test_request_context():
                    nonblock_call(
                        copy_current_request_context(task),
                        args=args,
                        kargs=kargs,
                        bound='io',
                    )
            except:
                log.exception('group task {} failed', task.__name__)
    log.debug('group app task {} done', name)
Example #9
0
            def handler(*args):
                def wrapuser():
                    event_data = {}
                    for event in uniq_events:
                        comp = COMPONENT_REGISTRY[event.uuid]
                        # we already checked that this component has a getter
                        event_data[event.signal] = getattr(comp, event.getter)()

                    # if there is no getter, then there is no data to unpack
                    # if there is a getter, then we need to unpack the data sent
                    main_getter = main_event.getter
                    if main_getter is not None:
                        comp = COMPONENT_REGISTRY[main_event.uuid]
                        event_data[main_event.signal] = getattr(comp, '_' + main_getter)(
                            msgpack.unpackb(args[0], encoding='utf8')
                        )

                    # gather the remaining data from the other events through their getter methods
                    for events, func in supports:
                        if main_getter is not None:
                            func(*(event_data[event.signal] for event in events))
                        else:
                            func()

                # TODO replace with flask socketio start_background_task
                eventlet.spawn(copy_current_request_context(wrapuser))
Example #10
0
def socket_answer(question_num, answer):
    user = data.users[flask.request.sid]

    if question_num == user.session.question_num:
        if isinstance(user.session.questions[user.session.question_num], data.SpeedQuestion):
            if question_num in user.answers:
                # Only one shot!
                return

        user.answers[question_num] = answer

        if isinstance(user.session.questions[user.session.question_num], data.MCQQuestion):
            flask_socketio.emit('update', render_question(user, user.session, user.session.question_num), room=user.sid)

        # Hurry!
        if isinstance(user.session.questions[user.session.question_num], data.SpeedQuestion):
            if user.session.questions[user.session.question_num].timer_thread is None:
                user.session.questions[user.session.question_num].timer_thread = data.SpeedQuestionTimerThread(
                    flask.copy_current_request_context(do_goto_question), user.session, user.session.question_num + 1)
                user.session.questions[user.session.question_num].timer_thread.start()

        # Relay change
        for _, other_user in data.iterate_users():
            if other_user.session == user.session:
                flask_socketio.emit('update_left', render_sidebar(other_user, user.session), room=other_user.sid)
                if isinstance(user.session.questions[user.session.question_num], data.SpeedQuestion):
                    flask_socketio.emit('update', render_question(other_user, user.session, user.session.question_num),
                                        room=other_user.sid)
        for _, _admin in data.iterate_admins():
            if _admin.session == user.session:
                flask_socketio.emit('update', render_question_admin(user.session, user.session.question_num),
                                    room=_admin.sid)
                flask_socketio.emit('update_left', render_sidebar(_admin, user.session), room=_admin.sid)
Example #11
0
def _(*args):
    def wrapuser():
        uniq_events = set()
        uniq_events.update([('3#click', 3, 'get')])
        uniq_events.remove(('3#click', 3, 'get'))
        event_data = {}
        for ev in uniq_events:
            comp = COMPONENT_REGISTRY[ev[1]]
            if ev[2] is None:
                ename = ev[0]
                raise GetterNotDefined(
                    '{ctype} has no getter associated with event "on_{ename}"'.
                    format(ctype=type(comp),
                           ename=ename[ename.find('#') + 1:]))
            getter = getattr(comp, ev[2])
            event_data[ev[0]] = getter()

        # if there is no getter, then there is no data to unpack
        # if there is a getter, then we need to unpack the data sent
        event_data['3#click'] = COMPONENT_REGISTRY[3]._get(
            msgpack.unpackb(bytes(args[0]['data']), encoding='utf8'))

        # gather the remaining data from the other events through their getter methods
        user_args = []
        user_args.append(event_data['3#click'])

        # finally call the user method
        yelp.vizplace(*user_args)

    foo = copy_current_request_context(wrapuser)
    eventlet.spawn(foo)
 def auto_add_tags(cls, artifact):
     """Adds automatically generated tags to the artifact.
     Returns nothing and will not raise exceptions or errors encountered during the process"""
     if has_request_context():
         temp = copy_current_request_context(cls._work_for_artifact)
         spawn_n(temp, artifact)
     else:
         cls._work_for_artifact(artifact)
Example #13
0
    def post(self):
        host = request.form.get('host')
        command = request.form.get('command')
        uid = uuid.uuid4().hex
        th = threading.Thread(target=copy_current_request_context(do_run_command), args=(host, command, uid))
        th.start()

        return {'uid': uid}
Example #14
0
 def _prepare_fn(self, fn, force_copy=False):
     if isinstance(self._self, concurrent.futures.ThreadPoolExecutor) \
         or force_copy:
         fn = copy_current_request_context(fn)
         if current_app.config[self.EXECUTOR_PUSH_APP_CONTEXT]:
             fn = push_app_context(fn)
         else:
             fn = copy_current_app_context(fn)
     return fn
def send_email(to, template, **kwargs):
    msg = Message(current_app.config['CIRR_MAIL_SUBJECT_PREFIX'],
                  sender=current_app.config['CIRR_MAIL_SENDER'],
                  recipients=[to])
    msg.body = render_template(template + '.txt', **kwargs)
    thr = Thread(target=copy_current_request_context(send_async_email),
                 args=[current_app._get_current_object(), msg])
    thr.start()
    return thr
Example #16
0
def emit_state(attrs, broadcast=False, callback=None):
    assert not (callback
                and broadcast), "Cannot have a callback when broadcasting!"
    state = {}
    if 'tickets' in attrs:
        tickets = Ticket.query.filter(
            Ticket.status.in_(active_statuses),
            Ticket.course == get_course(),
        ).all()
        state['tickets'] = [ticket_json(ticket) for ticket in tickets]

        pendingTickets = Ticket.query.filter(
            Ticket.status.in_([TicketStatus.pending])).all()

        activeStaffList = list(user_presence[get_course()]['staff'])
        for ticket in tickets:
            if ticket not in pendingTickets:
                ticketHelper = (ticket.helper.email, ticket.helper.name)
                if ticketHelper in activeStaffList:
                    activeStaffList.remove(ticketHelper)

        state['waitTimes'], state['stddev'] = avgWaitTimeList(
            "./oh_queue/query_result_ticket_event.csv",
            len(pendingTickets) + 1, len(activeStaffList))

    if 'assignments' in attrs:
        assignments = Assignment.query.filter_by(course=get_course()).all()
        state['assignments'] = [
            assignment_json(assignment) for assignment in assignments
        ]
    if 'locations' in attrs:
        locations = Location.query.filter(Location.course == get_course(),
                                          Location.name != "Online").all()
        state['locations'] = [
            location_json(location) for location in locations
        ]
        state["locations"].append(location_json(get_online_location()))
    if 'config' in attrs:
        state['config'] = config_json()
    if 'appointments' in attrs:
        appointments = Appointment.query.filter(
            Appointment.status != AppointmentStatus.resolved,
            Appointment.course == get_course(),
        ).order_by(Appointment.id).all()
        state['appointments'] = [
            appointments_json(appointment) for appointment in appointments
        ]

    if not broadcast and 'current_user' in attrs:
        state['current_user'] = student_json(current_user)
    if broadcast:
        socketio.emit('state', state, room=get_course())
    elif callback:
        emit('state', state, callback=copy_current_request_context(callback))
    else:
        emit('state', state)
Example #17
0
    def _gevent_pool_search(self, searchlist, searchfunc):
        gpool = GeventPool(current_app.config.get('GEVENT_POOL_SIZE', 10))
        gqueue = GeventQueue(current_app.config.get('GEVENT_QUEUE_SIZE', 200))
        def init_gevent_pool():
            for search in searchlist:
                gpool.wait_available()
                gpool.spawn(copy_current_request_context(searchfunc), search, gqueue)

        gpool.spawn(copy_current_request_context(init_gevent_pool))
        return gpool, gqueue
Example #18
0
def run_with_context(func):
    if _request_ctx_stack.top is not None:
        func = copy_current_request_context(func)
    app_ctx = current_app.app_context()

    def wrapper(*args, **kwargs):
        with app_ctx:
            return func(*args, **kwargs)

    return update_wrapper(wrapper, func)
Example #19
0
    def _gevent_pool_search(self, searchlist, searchfunc):
        gpool = GeventPool(current_app.config.get('GEVENT_POOL_SIZE', 10))
        gqueue = GeventQueue(current_app.config.get('GEVENT_QUEUE_SIZE', 200))

        def init_gevent_pool():
            for search in searchlist:
                gpool.wait_available()
                gpool.spawn(copy_current_request_context(searchfunc), search,
                            gqueue)

        gpool.spawn(copy_current_request_context(init_gevent_pool))
        return gpool, gqueue
Example #20
0
def run_command():
    form = flask.request.form
    host = form['host']
    username = form['username']
    password = form['password']
    command = form['command']
    uid = uuid.uuid4().hex
    th = threading.Thread(
        target=flask.copy_current_request_context(do_run_command),
        args=(host, username, password, command, uid))
    th.start()
    return {'uid': uid}
Example #21
0
def run_script_through_websocket(script, session, host):
    status_code = 200
    default_msg = json.dumps({"status": 0, "output": ""})
    sub_step = session.add_sub_step(control_line=status_code, body=default_msg)

    def on_open(_ws):
        def run():
            _ws.send(script)
            log.info('RunScript: Open websocket and send message %s '
                     'to vmmaster-agent on vm %s' % (script, host))

        _t = Thread(target=run)
        _t.daemon = True
        _t.start()

    def on_message(_ws, message):
        _ws.output += message
        if sub_step:
            msg = json.dumps({"status": _ws.status, "output": _ws.output})
            update_log_step(sub_step, message=msg)

    def on_close(_ws):
        if sub_step and ws.output:
            msg = json.dumps({"status": _ws.status, "output": _ws.output})
            update_log_step(sub_step, message=msg)
        log.info("RunScript: Close websocket on vm %s" % host)

    def on_error(_ws, message):
        global status_code
        status_code = 500
        _ws.status = 1
        _ws.output += repr(message)
        log.debug("RunScript error: %s" % message)

    ws = websocket.WebSocketApp(host,
                                on_message=on_message,
                                on_close=on_close,
                                on_open=on_open,
                                on_error=on_error)
    ws.output = ""
    ws.status = 0
    session.ws = ws

    t = Thread(target=copy_current_request_context(ws.run_forever))
    t.daemon = True
    t.start()

    while t.isAlive():
        yield None, None, None

    full_msg = json.dumps({"status": ws.status, "output": ws.output})
    yield status_code, {}, full_msg
Example #22
0
    def student_information(self, student_id, paper_id, test_room, test_number,
                            test_name, test_type):
        """ 判断考生选取的题目是否可选,判断考生是否已经选题
        """
        print(paper_id + " " + student_id)
        self.cur.execute("select paper_flag from test_paper where id = " +
                         paper_id)
        is_choose_message = self.cur.fetchall()
        print(is_choose_message)
        if is_choose_message[0]['paper_flag'] == 1:
            return None

        self.cur.execute(
            "select test_paper1, test_paper2 from test_student where test_id = '"
            + student_id + "'")
        is_student_choose_message = self.cur.fetchall()
        print(is_student_choose_message)
        """ 根据考生抽取到的题号更新考生表
            如果sql语句中的条件值是变量,变量要定义成str类型,在sql语句中用'"+s+"'表示
            更新题目的状态
        """
        if is_student_choose_message[0]['test_paper1'] != 0:
            if is_student_choose_message[0]['test_paper2'] != 0:
                return None
            else:
                update = "update test_student set test_paper2 = '" + paper_id + "', test_room = '" \
                         + test_room + "', test_number = '" + test_number + "' where test_id='" + student_id + "' "
        else:
            update = "update test_student set test_paper1 = '" + paper_id + "', test_room = '" \
                     + test_room + "', test_number = '" + test_number + "' where test_id='" + student_id + "' "
        self.cur.execute(update)
        self.con.commit()

        paper_update = "update test_paper set paper_flag = 1 where id = " + paper_id
        self.cur.execute(paper_update)
        self.con.commit()

        Q = self.paper_list_everyone(paper_id)

        # 将选题信息写入word文档
        doxManagement = DoxManagement()
        doxManagement.born_word(test_name, test_type, test_number,
                                Q[0]['question_content'],
                                Q[1]['question_content'],
                                Q[2]['question_content'],
                                Q[3]['question_content'],
                                Q[4]['question_content'], student_id)
        gevent.spawn(
            copy_current_request_context(doxManagement.born_pdf(student_id)))
        return Q
Example #23
0
    def _wrapper(*args, **kwargs):
        call_result = Future()

        def _run():
            loop = asyncio.new_event_loop()
            try:
                result = loop.run_until_complete(func(*args, **kwargs))
            except Exception as error:
                call_result.set_exception(error)
            else:
                call_result.set_result(result)
            finally:
                loop.close()

        loop_executor = ThreadPoolExecutor(max_workers=10)
        if has_request_context():
            _run = copy_current_request_context(_run)
        loop_future = loop_executor.submit(_run)
        loop_future.result()
        return call_result.result()
Example #24
0
def emit_state(attrs, broadcast=False, callback=None):
    assert not (callback
                and broadcast), "Cannot have a callback when broadcasting!"
    state = {}
    if 'tickets' in attrs:
        tickets = Ticket.query.filter(
            Ticket.status.in_(active_statuses),
            Ticket.course == get_course(),
        ).all()
        state['tickets'] = [ticket_json(ticket) for ticket in tickets]
    if 'assignments' in attrs:
        assignments = Assignment.query.filter_by(course=get_course()).all()
        state['assignments'] = [
            assignment_json(assignment) for assignment in assignments
        ]
    if 'locations' in attrs:
        locations = Location.query.filter(Location.course == get_course(),
                                          Location.name != "Online").all()
        state['locations'] = [
            location_json(location) for location in locations
        ]
        state["locations"].append(location_json(get_online_location()))
    if 'config' in attrs:
        state['config'] = config_json()
    if 'appointments' in attrs:
        appointments = Appointment.query.filter(
            Appointment.status != AppointmentStatus.resolved,
            Appointment.course == get_course(),
        ).order_by(Appointment.id).all()
        state['appointments'] = [
            appointments_json(appointment) for appointment in appointments
        ]

    if not broadcast and 'current_user' in attrs:
        state['current_user'] = student_json(current_user)
    if broadcast:
        socketio.emit('state', state, room=get_course())
    elif callback:
        emit('state', state, callback=copy_current_request_context(callback))
    else:
        emit('state', state)
Example #25
0
def _(*args):
    def wrapuser():
        uniq_events = set()
        uniq_events.update([('2#click', 'click', None)])
        uniq_events.remove(('2#click', 'click', None))
        event_data = {}
        for ev in uniq_events:
            comp = getattr(chat, ev[1])
            if ev[2] is None:
                ename = ev[0]
                raise GetterNotDefined('{ctype} has no getter associated with event "on_{ename}"'
                                       .format(ctype=type(comp), ename=ename[ename.find('#') + 1:]))
            getter = getattr(comp, ev[2])
            event_data[ev[0]] = getter()


        user_args = []
        chat.clicked(*user_args)

    foo = copy_current_request_context(wrapuser)
    eventlet.spawn(foo)
Example #26
0
 def init_gevent_pool():
     for search in searchlist:
         gpool.wait_available()
         gpool.spawn(copy_current_request_context(searchfunc), search,
                     gqueue)
Example #27
0
def i_have_a_dream(func, *args, **kwargs):
    """
    异步任务处理。本函数会立即返回,并使用 gevent 的新线程执行 func 函数(带上下文)。
    """
    return gevent.spawn(copy_current_request_context(func), *args, **kwargs)
Example #28
0
 def init_gevent_pool():
     for search in searchlist:
         gpool.wait_available()
         gpool.spawn(copy_current_request_context(searchfunc), search, gqueue)
Example #29
0
def run_cmd():
    threads = []
    cmd = 'tail -f -n -1 /var/log/auth.log'
    cmd_login_win = 'powershell get-content -path C:/logfile/ids-event.txt -wait -tail 1 -Encoding utf8'
    sensive_dir = '/etc/resolv.conf /etc/passwd /etc/shadow /etc/httpd/httpd.conf'  # 敏感目录
    cmd_mondir = "inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f %e' -e modify /etc/passwd " \
                 "/etc/shadow /var/spool/cron/crontabs /proc/version /usr/share/iptables /etc/group /etc/hosts.allow /etc/hosts.deny " \
                 "/etc/ssh/sshd_config "
    cmd_monusage = "powershell -nop -c \"iex(New-Object Net.WebClient).DownloadString(" \
                   "'youip') "
    cmd_linux_monusage = "cd /root/temptool && ./linux_mon.sh "
    cmd_toids = '''powershell get-content -path C:/logfile/ids-event.txt -wait -tail 1 -Encoding utf8'''
    cmd_tovirus = '''powershell get-content -path C:/logfile/virus-event.txt -wait -tail 1 -Encoding utf8'''
    # linux监视资源线程
    th_linux_monusage = threading.Thread(
        target=flask.copy_current_request_context(
            remote_read_log.link_server_client),
        args=('xxx.xxx.xxx.xxx', 'admin', 'admin', cmd_linux_monusage,
              'monlinux_usage'))
    # IP ,SSH用户名密码
    threads.append(th_linux_monusage)
    # windows监视资源线程
    th_windows_monusage = threading.Thread(
        target=flask.copy_current_request_context(
            remote_read_log.link_server_client),
        args=('xxx.xxx.xxx.xxx', 'admin', 'admin', cmd_monusage,
              'monwin_usage'))
    threads.append(th_windows_monusage)
    # linux登陆日志采集线程
    th = threading.Thread(target=flask.copy_current_request_context(
        remote_read_log.link_server_client),
                          args=('xxx.xxx.xxx.xxx', 'admin', 'admin', cmd,
                                'auth'),
                          name='linux_auth')
    threads.append(th)
    # linux敏感目录线程
    th3 = threading.Thread(target=flask.copy_current_request_context(
        remote_read_log.link_server_client),
                           args=('xxx.xxx.xxx.xxx', 'admin', 'admin',
                                 cmd_mondir, 'mondir'),
                           name='linux_mondir')
    threads.append(th3)
    # windows 登陆日志采集线程
    th_winlogin = threading.Thread(target=flask.copy_current_request_context(
        remote_read_log.link_server_client),
                                   args=('xxx.xxx.xxx.xxx', 'admin', 'admin',
                                         cmd_login_win, 'login'),
                                   name='linux_mondir')
    threads.append(th_winlogin)
    #IDS、virus
    key = ['virus', 'ids']
    # ls_viruslog = [1, 3, 4, 4, 4, 4]
    # ls_idslog = [2, 3, 4, 4, 6]
    # 这里填本地或远程的地址
    ids_log = "C:/Users/J/Desktop/毕设系统/ids-event.txt"
    virus_log = "C:/Users/J/Desktop/毕设系统/virus-event.txt"
    cataline = r"C:\Users\J\Desktop\毕设系统\catalina.2020-05-04.log"
    th4 = threading.Thread(target=flask.copy_current_request_context(
        readlog.follow_log),
                           args=(ids_log, 'ids'))
    threads.append(th4)
    th5 = threading.Thread(target=flask.copy_current_request_context(
        readlog.follow_log),
                           args=(virus_log, 'virus'))
    threads.append(th5)
    # th4 = threading.Thread(
    #     target=flask.copy_current_request_context(readlog.follow_log),
    #     args=(cataline,'business')
    # )
    threads.append(th4)
    for i in range(len(threads)):
        threads[i].start()
    return "success connect"
Example #30
0
def run_task(func, *args, **kwargs):
    if gevent and current_app.config.get('ZERQU_ASYNC'):
        gevent.spawn(copy_current_request_context(func), *args, **kwargs)
    else:
        func(*args, **kwargs)
Example #31
0
    def __init__(
        self,
        action: str,
        target: Optional[Callable] = None,
        name: Optional[str] = None,
        args: Optional[Iterable[Any]] = None,
        kwargs: Optional[Dict[str, Any]] = None,
        daemon: bool = True,
        default_stop_timeout: int = 5,
        log_len: int = 100,
    ):
        threading.Thread.__init__(
            self,
            group=None,
            target=target,
            name=name,
            args=args or (),
            kwargs=kwargs or {},
            daemon=daemon,
        )

        # Action resource corresponding to this action object
        self.action = action

        # A UUID for the ActionThread (not the same as the threading.Thread ident)
        self._ID = uuid.uuid4()  # Task ID

        # Event to track if the task has started
        self.started: threading.Event = threading.Event()
        # Event to track if the user has requested stop
        self.stopping: threading.Event = threading.Event()
        self.default_stop_timeout: int = default_stop_timeout

        # Make _target, _args, and _kwargs available to the subclass
        self._target: Optional[Callable] = target
        self._args: Iterable[Any] = args or ()
        self._kwargs: Dict[str, Any] = kwargs or {}

        # Nice string representation of target function
        self.target_string: str = (
            f"{self._target}(args={self._args}, kwargs={self._kwargs})")

        # copy_current_request_context allows threads to access flask current_app
        if has_request_context():
            logging.debug("Copying request context to %s", self._target)
            self._target = copy_current_request_context(self._target)
            try:
                self.input = request.json
            except BadRequest:
                self.input = None
        else:
            logging.debug("No request context to copy")
            self.input = None

        # Private state properties
        self._status: str = "pending"  # Task status
        self._return_value: Optional[Any] = None  # Return value
        self._request_time: datetime.datetime = datetime.datetime.now()
        self._start_time: Optional[datetime.datetime] = None  # Task start time
        self._end_time: Optional[datetime.datetime] = None  # Task end time

        # Public state properties
        self.progress: Optional[int] = None  # Percent progress of the task
        self.data: dict = {}  # Dictionary of custom data added during the task
        self._log = LockableDeque(
            None, log_len
        )  # The log will hold dictionary objects with log information

        # Stuff for handling termination
        self._running_lock = (threading.Lock()
                              )  # Lock obtained while self._target is running
Example #32
0
    def _endpoints(self):
        def generate_sio_handler(main_event, supports):
            # get all events from all subscriptions associated with this event
            uniq_events = set()
            for events, _ in supports:
                uniq_events.update(events)
            uniq_events.remove(main_event)

            for event in uniq_events:
                comp = COMPONENT_REGISTRY[event.uuid]
                if event.getter is None:
                    raise AttributeError(
                        f'{comp} has no getter associated with event "on_{event.name}"'
                    )

            def handler(*args):
                def wrapuser():
                    event_data = {}
                    for event in uniq_events:
                        comp = COMPONENT_REGISTRY[event.uuid]
                        # we already checked that this component has a getter
                        event_data[event.signal] = getattr(comp, event.getter)()

                    # if there is no getter, then there is no data to unpack
                    # if there is a getter, then we need to unpack the data sent
                    main_getter = main_event.getter
                    if main_getter is not None:
                        comp = COMPONENT_REGISTRY[main_event.uuid]
                        event_data[main_event.signal] = getattr(comp, '_' + main_getter)(
                            msgpack.unpackb(args[0], encoding='utf8')
                        )

                    # gather the remaining data from the other events through their getter methods
                    for events, func in supports:
                        if main_getter is not None:
                            func(*(event_data[event.signal] for event in events))
                        else:
                            func()

                # TODO replace with flask socketio start_background_task
                eventlet.spawn(copy_current_request_context(wrapuser))
            return handler

        for event, supports in self._subscriptions.items():
            self._socketio.on(event.signal)(generate_sio_handler(event, supports))

        if self._init is not None:
            self._socketio.on('INITIALIZE')(lambda: eventlet.spawn(
                copy_current_request_context(self._init)
            ))

        def gen_upload(func):
            def upload():
                upfile = request.files['file']
                retval = func(upfile.filename, upfile.stream)
                if retval:
                    return make_response(jsonify(), 400)
                return make_response(jsonify(), 200)
            return upload

        for uuid, func in self._uploads.items():
            self.app.add_url_rule(
                f'/upload{uuid}', f'upload{uuid}', gen_upload(func), methods=['POST']
            )

        for page, func in self._pages.items():
            # pylint: disable=protected-access
            self._socketio.on(f'resp#{page._uuid}')(lambda: eventlet.spawn(
                copy_current_request_context(func)
            ))

        # bundle route
        @self.app.route('/bowtie/bundle.js')
        def bowtiebundlejs():  # pylint: disable=unused-variable
            bundle_path = self.app.root_path + '/build/bundle.js'
            bundle_path_gz = bundle_path + '.gz'

            try:
                if os.path.getmtime(bundle_path) > os.path.getmtime(bundle_path_gz):
                    return open(bundle_path, 'r').read()
                bundle = open(bundle_path_gz, 'rb').read()
                response = flask.make_response(bundle)
                response.headers['content-encoding'] = 'gzip'
                response.headers['vary'] = 'accept-encoding'
                response.headers['content-length'] = len(response.data)
                return response
            except FileNotFoundError:
                if os.path.isfile(bundle_path_gz):
                    bundle = open(bundle_path_gz, 'rb').read()
                    response = flask.make_response(bundle)
                    response.headers['Content-Encoding'] = 'gzip'
                    response.headers['Vary'] = 'Accept-Encoding'
                    response.headers['Content-Length'] = len(response.data)
                    return response
                return open(bundle_path, 'r').read()

        for schedule in self._schedules:
            schedule.start()
Example #33
0
 def _prepare_func(self, func):
     if isinstance(self._executor, concurrent.futures.ThreadPoolExecutor):
         if _request_ctx_stack.top is not None:
             func = copy_current_request_context(func)
         func = self._copy_current_app_context(func)
     return func
Example #34
0
    def _endpoints(self):
        def generate_sio_handler(main_event, supports):
            # get all events from all subscriptions associated with this event
            uniq_events = set()
            for events, _ in supports:
                uniq_events.update(events)
            uniq_events.remove(main_event)

            for event in uniq_events:
                comp = COMPONENT_REGISTRY[event.uuid]
                if event.getter is None:
                    raise AttributeError(
                        f'{comp} has no getter associated with event "on_{event.name}"'
                    )

            def handler(*args):
                def wrapuser():
                    event_data = {}
                    for event in uniq_events:
                        comp = COMPONENT_REGISTRY[event.uuid]
                        # we already checked that this component has a getter
                        event_data[event.signal] = getattr(comp, event.getter)()

                    # if there is no getter, then there is no data to unpack
                    # if there is a getter, then we need to unpack the data sent
                    main_getter = main_event.getter
                    if main_getter is not None:
                        comp = COMPONENT_REGISTRY[main_event.uuid]
                        event_data[main_event.signal] = getattr(comp, '_' + main_getter)(
                            msgpack.unpackb(args[0], encoding='utf8')
                        )

                    # gather the remaining data from the other events through their getter methods
                    for events, func in supports:
                        if main_getter is not None:
                            func(*(event_data[event.signal] for event in events))
                        else:
                            func()

                # TODO replace with flask socketio start_background_task
                eventlet.spawn(copy_current_request_context(wrapuser))
            return handler

        for event, supports in self._subscriptions.items():
            self._socketio.on(event.signal)(generate_sio_handler(event, supports))

        if self._init is not None:
            self._socketio.on('INITIALIZE')(lambda: eventlet.spawn(
                copy_current_request_context(self._init)
            ))

        def gen_upload(func):
            def upload():
                upfile = request.files['file']
                retval = func(upfile.filename, upfile.stream)
                if retval:
                    return make_response(jsonify(), 400)
                return make_response(jsonify(), 200)
            return upload

        for uuid, func in self._uploads.items():
            self.app.add_url_rule(
                f'/upload{uuid}', f'upload{uuid}', gen_upload(func), methods=['POST']
            )

        for page, func in self._pages.items():
            # pylint: disable=protected-access
            self._socketio.on(f'resp#{page._uuid}')(lambda: eventlet.spawn(
                copy_current_request_context(func)
            ))

        # bundle route
        @self.app.route('/bowtie/bundle.js')
        def bowtiebundlejs():  # pylint: disable=unused-variable
            bundle_path = self.app.root_path + '/build/bundle.js'
            bundle_path_gz = bundle_path + '.gz'

            try:
                if os.path.getmtime(bundle_path) > os.path.getmtime(bundle_path_gz):
                    return open(bundle_path, 'r').read()
                bundle = open(bundle_path_gz, 'rb').read()
                response = flask.make_response(bundle)
                response.headers['content-encoding'] = 'gzip'
                response.headers['vary'] = 'accept-encoding'
                response.headers['content-length'] = len(response.data)
                return response
            except FileNotFoundError:
                if os.path.isfile(bundle_path_gz):
                    bundle = open(bundle_path_gz, 'rb').read()
                    response = flask.make_response(bundle)
                    response.headers['Content-Encoding'] = 'gzip'
                    response.headers['Vary'] = 'Accept-Encoding'
                    response.headers['Content-Length'] = len(response.data)
                    return response
                return open(bundle_path, 'r').read()

        for schedule in self._schedules:
            schedule.start()
Example #35
0
def i_have_a_dream(func, *args, **kwargs):
    """
    异步任务处理。本函数会立即返回,并使用 gevent 的新线程执行 func 函数(带上下文)。
    """
    return gevent.spawn(copy_current_request_context(func), *args, **kwargs)
Example #36
0
def async_func(func, *args, **kwargs):
    """
        异步任务处理。本函数会立即返回,并使用 gevent 的新线程执行 func 函数(带 request 上下文)。
    """
    return gevent.spawn(copy_current_request_context(func), *args, **kwargs)
Example #37
0
def _():
    foo = copy_current_request_context(chat.update_chat)
    eventlet.spawn(foo)
Example #38
0
def run_task(func, *args, **kwargs):
    if gevent and current_app.config.get('ZERQU_ASYNC'):
        gevent.spawn(copy_current_request_context(func), *args, **kwargs)
    else:
        func(*args, **kwargs)
Example #39
0
 def inner(*args, **kwargs):
     return gevent.spawn(copy_current_request_context(f), *args, **kwargs)