Esempio n. 1
0
def init_env_and_config(app):
    if not app.config['FLATPAGES_ROOT']:
        app.config['FLATPAGES_ROOT'] = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            '../content')
    if app.config['CONTENT_URL']:
        init_repo(app.config["FLATPAGES_ROOT"], app.config['CONTENT_URL'])
    else:
        if not os.path.isdir(app.config['FLATPAGES_ROOT']):
            os.mkdir(app.config['FLATPAGES_ROOT'])

    try:
        import uwsgi
    except ImportError:
        logger.info("uwsgi package not found, uwsgi_timer hasn't been set")
    else:
        def update_uwsgi(signum):
            flatpages_root = app.config["FLATPAGES_ROOT"]
            logger.debug("Udpating git repository at %s", flatpages_root)
            hasToReload = update_repo(flatpages_root)
            if hasToReload:
                logger.debug("Reloading flatpages…")
                uwsgi.reload()

        logger.debug("Registering repo update to uwsgi signal")
        uwsgi.register_signal(20, "", update_uwsgi)
        uwsgi.add_timer(20, 300)

    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

    if not app.config.get('SECRET_KEY'):
        if not app.debug:
            logger.warning('SECRET_KEY not set. Using default Key.')
        app.config['SECRET_KEY'] = "yIhswxbuDCvK8a6EDGihW6xjNognxtyO85SI"
Esempio n. 2
0
 def set_periodically():
     print("定时")
     uwsgi.register_signal(SIG_REBUILD_INDEX, 'workers',
                           rebuild_periodically)
     # uwsgi.add_timer(SIG_REBUILD_INDEX, 5)
     #每周周一晚上一点重建索引
     uwsgi.add_cron(SIG_REBUILD_INDEX, 0, 1, -1, -1, 1)
Esempio n. 3
0
def init_env_and_config(app):
    if not app.config['FLATPAGES_ROOT']:
        app.config['FLATPAGES_ROOT'] = os.path.join(
            os.path.dirname(os.path.abspath(__file__)), '../content')
    if app.config['CONTENT_URL']:
        init_repo(app.config["FLATPAGES_ROOT"], app.config['CONTENT_URL'])
    else:
        if not os.path.isdir(app.config['FLATPAGES_ROOT']):
            os.mkdir(app.config['FLATPAGES_ROOT'])

    try:
        import uwsgi
    except ImportError:
        logger.info("uwsgi package not found, uwsgi_timer hasn't been set")
    else:

        def update_uwsgi(signum):
            flatpages_root = app.config["FLATPAGES_ROOT"]
            logger.debug("Udpating git repository at %s", flatpages_root)
            hasToReload = update_repo(flatpages_root)
            if hasToReload:
                logger.debug("Reloading flatpages…")
                uwsgi.reload()

        logger.debug("Registering repo update to uwsgi signal")
        uwsgi.register_signal(20, "", update_uwsgi)
        uwsgi.add_timer(20, 300)

    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

    if not app.config.get('SECRET_KEY'):
        if not app.debug:
            logger.warning('SECRET_KEY not set. Using default Key.')
        app.config['SECRET_KEY'] = "yIhswxbuDCvK8a6EDGihW6xjNognxtyO85SI"
Esempio n. 4
0
    def register_model_load_handler(cls, model_path, logger,
                                    within_uwsgi_context):
        logger.info("Model file path: {}".format(model_path))

        if within_uwsgi_context:
            cls._model_selector = ModelSelector(model_path)

            logger.info(
                "Register signal (model reloading): {} (worker, wid: {}, {})".
                format(UwsgiConstants.MODEL_RELOAD_SIGNAL_NUM, cls._wid,
                       UwsgiBroker._model_selector))
            uwsgi.register_signal(UwsgiConstants.MODEL_RELOAD_SIGNAL_NUM,
                                  "workers", cls._model_reload_signal)

            logger.info(
                "Model path to monitor (signal {}, wid: {}): {}".format(
                    UwsgiConstants.MODEL_RELOAD_SIGNAL_NUM, cls._wid,
                    cls._model_selector.model_env.sync_filepath))
            uwsgi.add_file_monitor(UwsgiConstants.MODEL_RELOAD_SIGNAL_NUM,
                                   cls._model_selector.model_env.sync_filepath)
        else:
            if os.path.isfile(model_path):
                UwsgiBroker._restful_comp.load_model_callback(model_path,
                                                              stream=None,
                                                              version=None)
Esempio n. 5
0
    def init_ws(app):
        bt_changed = Event()
        uwsgi.register_signal(1, "workers", lambda x: bt_changed.set())

        @ws.route("/bluetooth/ws")
        def bluetooth_ws(ws):
            with app.request_context(ws.environ):
                if not session.get("logged_in"):
                    return redirect(
                        with_query_string(url_for('auth.login'), "next",
                                          request.url))
                bt_changed.clear()
                db = get_db()
                with suppress(SystemError, OSError):
                    while True:
                        old = set(
                            filter(
                                lambda x: not x["hostdev"],
                                map(
                                    hashabledict,
                                    db.execute("SELECT * FROM bluetooth").
                                    fetchall())))
                        while not bt_changed.wait(1):
                            ws.recv_nb()
                        new = set(
                            filter(
                                lambda x: not x["hostdev"],
                                map(
                                    hashabledict,
                                    db.execute("SELECT * FROM bluetooth").
                                    fetchall())))
                        bt_changed.clear()
                        if len(new) == 0:
                            ws.send(json.dumps({"action": "clear"}))
                        else:
                            for device in old - new:
                                ws.send(
                                    json.dumps(dict(**device, action="del")))
                            for device in new - old:
                                ws.send(
                                    json.dumps(dict(**device, action="add")))

        @ws.route("/control/ws")
        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)
Esempio n. 6
0
    def register_signals(self):
        if not uwsgi:
            return

        def executor(signal_number):
            close_old_connections()
            result = Caller.objects.get(signal_number=signal_number).call()
            close_old_connections()
            return result

        callers = Caller.objects.annotate(
            crons=models.Count('cron')).prefetch_related('cron_set').exclude(
                crons=0)

        signal_number = 1
        for caller in callers:
            caller.signal_number = signal_number
            caller.save()

            uwsgi.register_signal(
                caller.signal_number,
                caller.spooler or 'worker',
                executor,
            )

            logger.debug(
                f'uwsgi.register_signal({signal_number}, {caller.callback})')

            signal_number += 1

        transaction.commit()
        return callers
Esempio n. 7
0
def schedule(delay, func, *args):
    """
    Can be implemented using a spooler.
    https://uwsgi-docs.readthedocs.io/en/latest/PythonDecorators.html

    To make the uwsgi configuration simple, use the alternative implementation.
    """
    global _last_signal

    def sighandler(signum):
        now = int(time.time())
        key = 'scheduler_call_time_signal_' + str(signum)
        uwsgi.lock()
        try:
            updating = uwsgi.cache_get(key)
            if updating is not None:
                updating = int.from_bytes(updating, 'big')
                if now - updating < delay:
                    return
            uwsgi.cache_update(key, now.to_bytes(4, 'big'))
        finally:
            uwsgi.unlock()
        func(*args)

    signal_num = _last_signal
    _last_signal += 1
    uwsgi.register_signal(signal_num, 'worker', sighandler)
    uwsgi.add_timer(signal_num, delay)
    return True
Esempio n. 8
0
    def __init__(self, conf):

        super(uWSGIMixin, self).__init__(conf)

        class Lock():

            def __enter__(self):
                uwsgi.lock()

            def __exit__(self, exc_type, exc_val, exc_tb):
                uwsgi.unlock()

        def spooler(args):
            try:
                self.mailer.sendmail(args["subject"].decode('utf-8'), args["body"].decode('utf-8'))
            except smtplib.SMTPConnectError:
                return uwsgi.SPOOL_RETRY
            else:
                return uwsgi.SPOOL_OK

        uwsgi.spooler = spooler

        self.lock = Lock()
        self.mailer = SMTP(conf)
        self.cache = uWSGICache

        timedelta = conf.getint("moderation", "purge-after")
        purge = lambda signum: self.db.comments.purge(timedelta)
        uwsgi.register_signal(1, "", purge)
        uwsgi.add_timer(1, timedelta)

        # run purge once
        purge(1)
Esempio n. 9
0
def v9_application():
    print 'Starting [%s] Service ....' % app_name
    config = openerp.tools.config
    # 使用celery去触发定时器.
    period_sec = 60
    if 'cron_by_celery_beats' in config.options:
        print "NO CRON TRIGGER,PLEASE ENABLE CELERY BEATS!!"

    elif 'cron_by_celery_worker' in config.options:
        print "Using UWSGI TIMER TO TRIGGER CRON IN CELERY WORKER!"
        uwsgi.register_signal(99, "mule1", celery_cron_trigger)
        uwsgi.add_rb_timer(99, period_sec)
    else:
        uwsgi.register_signal(99, "mule1", cron_works)
        uwsgi.add_rb_timer(99, period_sec)

    # else:
    #     uwsgi.register_signal(99, "mule1", cron_works)
    #     # uwsgi.register_signal(101,"mule2", notify_wechat_news)
    #     print 'Register Signal 99 for mule1 as Cron workers'

    # linux 2.6.25以下内核.不支持timerfd.所以使用rb_timer.

    # uwsgi.add_rb_timer(101,60)
    # uwsgi.register_signal(100, "workers", v7_reload)
    # uwsgi.register_signal(101, "workers", service_active)
    # uwsgi.register_signal(102, "workers", service_deactive)

    return application2
Esempio n. 10
0
    def __init__(self, target_temp):
        self._target_temp = target_temp
        self._timestep = 0
        self._MIN_VALID_TEMP = -5.0
        self._MAX_VALID_TEMP = 30.0
        self._READING_TICK = 5
        self._DELTA_OVERSHOOT_TEMP = 2.0
        self._SSR_PIN = 11
        self._compressor_state = True
        self._sensors = {}
        self._sensors['000001efbab6'] = 'top'
        self._sensors['000001efd9ac'] = 'bottom'
        self._sensors['000001eff556'] = 'beer'
        self._sensor_readings = deque(
            maxlen=int(60/self._READING_TICK)*len(W1.get_available_sensors())
        )

        logging.config.dictConfig(app.config['LOG_CONFIG'])
        self.logger = logging.getLogger('agent')

        if not app.config['DEBUG']:
            GPIO.setmode(GPIO.BOARD)
            GPIO.setup(self._SSR_PIN, GPIO.OUT)
            uwsgi.register_signal(9000, 'worker', self.run)
            uwsgi.add_timer(9000, 5)
            atexit.register(lambda: self.cleanup())

        if app.config['LOG_DEBUG']:
            self.logger.setLevel(logging.DEBUG)
        else:
            self.logger.setLevel(logging.WARN)

        self.logger.info("Agent started")
Esempio n. 11
0
    def init_socket_reaper(self):
        # start a greenlet that handle connection closing when idle
        logging.getLogger(__name__).info("spawning a socket reaper with gevent")
        gevent.spawn(self.socket_reaper_thread)

        # Use uwsgi timer if we are running in uwsgi without gevent.
        # When we are using uwsgi without gevent, idle workers won't run the greenlet, it will only
        # be scheduled when waiting for a response of an external service (kraken mostly)
        try:
            import uwsgi

            # In gevent mode we stop, no need to add a timer, the greenlet will be scheduled while waiting
            # for incomming request.
            if 'gevent' in uwsgi.opt:
                return

            logging.getLogger(__name__).info("spawning a socket reaper with  uwsgi timer")

            # Register a signal handler for the signal 1 of uwsgi
            # this signal will trigger the socket reaper and can be run by any worker
            def reaper_timer(signal):
                self.reap_sockets()

            uwsgi.register_signal(1, 'active-workers', reaper_timer)
            # Add a timer that trigger this signal every reaper_interval second
            uwsgi.add_timer(1, self.reaper_interval)
        except (ImportError, ValueError):
            # ImportError is triggered if we aren't in uwsgi
            # ValueError is raise if there is no more timer availlable: only 64 timers can be created
            # workers that didn't create a timer can still run the signal handler
            # if uwsgi dispatch the signal to them
            # signal are dispatched randomly to workers (not round robbin :()
            logging.getLogger(__name__).info(
                "No more uwsgi timer available or not running in uwsgi, only gevent will be used"
            )
Esempio n. 12
0
    def prepare_bot(self, bot_core, models, settings, roles, addons):
        global initial_signal
        bot = bot_core(models, settings, roles)
        for a in addons:
            bot.insert_addon(a)
        bot.bind()

        def check_tasks(signum):
            bot.check_tasks()

        uwsgi.register_signal(initial_signal, "", check_tasks)
        uwsgi.add_timer(initial_signal, 10)
        initial_signal -= 1

        for k, v in roles.items():
            if v == "ADMIN":
                if has_uwsgi:
                    try:
                        db_chat = bot.models["Chats"].objects.get(
                            bot__name=bot.name, user_name=k)
                        bot.send_message(db_chat, "Бот перезапущен")
                    except:
                        print("No admin found")

        return bot
Esempio n. 13
0
 def ready(self):      
     if has_uwsgi:
         from datacon.processing.base_economy import reduce_by_scheme
         print("Standard startup scheme - using uWSGI scheduler")
         uwsgi.register_signal(89, "", reduce_by_scheme)
         uwsgi.add_timer(89, 86400)
     else:
         print("Running without UWSGI (reduced functionality)")
 def _register_signals(self):
     if uwsgi.numproc > 1:
         self.has_workers = True
         # signal 0 will call all workers
         uwsgi.register_signal(0, 'workers', self._check_msg_in_cache)
         # others signals match each worker independently
         for i in range(1, uwsgi.numproc + 1):
             uwsgi.register_signal(i, 'worker%s' % i,
                                   self._check_msg_in_cache)
Esempio n. 15
0
 def __init__(self, path):
     try:
         import os
         import uwsgi
         signal = [signum for signum in range(0,256) if not uwsgi.signal_registered(signum)][0]
         uwsgi.register_signal(signal, '', uwsgi.reload)
         for path in [x[0] for x in os.walk(path)]:
             uwsgi.add_file_monitor(signal, path.decode(encoding='UTF-8'))
     except Exception as err:
         pass # Not running under uwsgi. The other supported alternative is gunicorn. 
Esempio n. 16
0
def initialize(signal_number=DEFAULT_TIMER_SIGNAL_NUMBER,
               update_period_s=DEFAULT_UPDATE_PERIOD_S):
    """Initialize metrics, must be invoked at least once prior to invoking any
    other method."""
    global initialized
    if initialized:
        return
    initialized = True
    uwsgi.add_timer(signal_number, update_period_s)
    uwsgi.register_signal(signal_number, MULE, emit)
Esempio n. 17
0
def initialize(signal_number=DEFAULT_TIMER_SIGNAL_NUMBER,
               update_period_s=DEFAULT_UPDATE_PERIOD_S):
    """Initialize metrics, must be invoked at least once prior to invoking any
    other method."""
    global initialized
    if initialized:
        return
    initialized = True
    uwsgi.add_timer(signal_number, update_period_s)
    uwsgi.register_signal(signal_number, MULE, emit)
Esempio n. 18
0
 def start(self, interval, now=False):
     signum = get_free_signal()
     uwsgi.register_signal(signum, '', self._handle_signal)
     uwsgi.add_timer(signum, interval)
     if now:
         try:
             self._handle_signal(signum)
         except Exception as e:
             print e
             pass
Esempio n. 19
0
    def __init__(self, conf):

        super(uWSGIMixin, self).__init__(conf)

        self.lock = multiprocessing.Lock()
        self.cache = uWSGICache

        timedelta = conf.getint("moderation", "purge-after")
        purge = lambda signum: self.db.comments.purge(timedelta)
        uwsgi.register_signal(1, "", purge)
        uwsgi.add_timer(1, timedelta)

        # run purge once
        purge(1)
Esempio n. 20
0
File: core.py Progetto: kod3r/isso
    def __init__(self, conf):

        super(uWSGIMixin, self).__init__(conf)

        self.lock = multiprocessing.Lock()
        self.cache = uWSGICache

        timedelta = conf.getint("moderation", "purge-after")
        purge = lambda signum: self.db.comments.purge(timedelta)
        uwsgi.register_signal(1, "", purge)
        uwsgi.add_timer(1, timedelta)

        # run purge once
        purge(1)
Esempio n. 21
0
    def set_periodically():
        if isinstance(update_after_seconds, int):

            uwsgi.register_signal(SIGNAL_SET_FAISS_INDEX, 'workers',
                                  set_faiss_index)

            if get_faiss_resources:
                uwsgi.register_signal(SIGNAL_SET_FAISS_RESOURCES, 'worker',
                                      set_faiss_resources)
                uwsgi.add_timer(SIGNAL_SET_FAISS_RESOURCES,
                                update_after_seconds)
            else:
                uwsgi.add_timer(SIGNAL_SET_FAISS_INDEX, update_after_seconds)

        else:
            print('Failed to set periodic faiss index updates')
            print('UPDATE_FAISS_AFTER_SECONDS must be an integer')
Esempio n. 22
0
def init_env_and_config(app):
    # default configuration
    app.config.from_pyfile(os.path.realpath("sipa/default_config.py"))
    # if local config file exists, load everything into local space.
    if 'SIPA_CONFIG_FILE' not in os.environ:
        os.environ['SIPA_CONFIG_FILE'] = os.path.realpath("config.py")
    try:
        app.config.from_envvar('SIPA_CONFIG_FILE', silent=True)
    except IOError:
        logger.warning("SIPA_CONFIG_FILE not readable: %s",
                       os.environ['SIPA_CONFIG_FILE'])
    else:
        logger.info("Successfully read config file %s",
                    os.environ['SIPA_CONFIG_FILE'])

    app.config.update({
        name[len("SIPA_"):]: value for name, value in os.environ.items()
        if name.startswith("SIPA_")
    })
    if app.config['FLATPAGES_ROOT'] == "":
        app.config['FLATPAGES_ROOT'] = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            '../content')
    if app.config['CONTENT_URL']:
        init_repo(app.config["FLATPAGES_ROOT"], app.config['CONTENT_URL'])
    else:
        if not os.path.isdir(app.config['FLATPAGES_ROOT']):
            os.mkdir(app.config['FLATPAGES_ROOT'])

    if os.getenv("SIPA_UWSGI", "False") == 'True':
        import uwsgi

        def update_uwsgi(signum):
            hasToReload = update_repo(app.config["FLATPAGES_ROOT"])
            if hasToReload:
                uwsgi.reload

        logger.info("Registering repo update to uwsgi_signal")
        uwsgi.register_signal(20, "", update_uwsgi)
        uwsgi.add_timer(20, 300)

    if not app.config['SECRET_KEY']:
        raise ValueError("SECRET_KEY must not be empty")
Esempio n. 23
0
def init_env_and_config(app):
    # default configuration
    app.config.from_pyfile(os.path.realpath("sipa/default_config.py"))
    # if local config file exists, load everything into local space.
    if 'SIPA_CONFIG_FILE' not in os.environ:
        os.environ['SIPA_CONFIG_FILE'] = os.path.realpath("config.py")
    try:
        app.config.from_envvar('SIPA_CONFIG_FILE', silent=True)
    except IOError:
        logger.warning("SIPA_CONFIG_FILE not readable: %s",
                       os.environ['SIPA_CONFIG_FILE'])
    else:
        logger.info("Successfully read config file %s",
                    os.environ['SIPA_CONFIG_FILE'])

    app.config.update({
        name[len("SIPA_"):]: value
        for name, value in os.environ.items() if name.startswith("SIPA_")
    })
    if app.config['FLATPAGES_ROOT'] == "":
        app.config['FLATPAGES_ROOT'] = os.path.join(
            os.path.dirname(os.path.abspath(__file__)), '../content')
    if app.config['CONTENT_URL']:
        init_repo(app.config["FLATPAGES_ROOT"], app.config['CONTENT_URL'])
    else:
        if not os.path.isdir(app.config['FLATPAGES_ROOT']):
            os.mkdir(app.config['FLATPAGES_ROOT'])

    if os.getenv("SIPA_UWSGI", "False") == 'True':
        import uwsgi

        def update_uwsgi(signum):
            hasToReload = update_repo(app.config["FLATPAGES_ROOT"])
            if hasToReload:
                uwsgi.reload

        logger.info("Registering repo update to uwsgi_signal")
        uwsgi.register_signal(20, "", update_uwsgi)
        uwsgi.add_timer(20, 300)

    if not app.config['SECRET_KEY']:
        raise ValueError("SECRET_KEY must not be empty")
Esempio n. 24
0
def init(app):
    def update_commands(signal_id):
        log.debug('Updating commands...')
        from pajbot.models.command import CommandManager
        bot_commands = CommandManager(
            socket_manager=None,
            module_manager=ModuleManager(None).load(),
            bot=None).load(load_examples=True)
        app.bot_commands_list = bot_commands.parse_for_web()

        app.bot_commands_list.sort(key=lambda x: (x.id or -1, x.main_alias))
        del bot_commands

    update_commands(26)
    try:
        import uwsgi
        from uwsgidecorators import thread, timer
        uwsgi.register_signal(26, 'worker', update_commands)
        uwsgi.add_timer(26, 60 * 10)

        @thread
        @timer(60)
        def get_highlight_thumbnails(no_clue_what_this_does):
            from pajbot.web.models.thumbnail import StreamThumbnailWriter
            with DBManager.create_session_scope() as db_session:
                highlights = db_session.query(StreamChunkHighlight).filter_by(
                    thumbnail=None).all()
                if len(highlights) > 0:
                    log.info('Writing {} thumbnails...'.format(
                        len(highlights)))
                    StreamThumbnailWriter(app.bot_config['main']['streamer'],
                                          [h.id for h in highlights])
                    log.info('Done!')
                    for highlight in highlights:
                        highlight.thumbnail = True
    except ImportError:
        log.exception('Import error, disregard if debugging.')
        pass
    pass
Esempio n. 25
0
File: timer.py Progetto: akx/mess
def start_insert_timer(app):
	global insert_timer_started
	if insert_timer_started:
		return False
	insert_timer_started = True

	interval = app.config["INSERT_INTERVAL"]

	if uwsgi:
		uwsgi.register_signal(131, "workers", insert_timer_tick)
		uwsgi.add_rb_timer(131, interval)
		return True
	else:
		import threading
		def insert_timer_tick_loop():
			while 1:
				sleep(interval)
				insert_timer_tick()

		thread = threading.Thread(target=insert_timer_tick_loop, name="insert timer")
		thread.setDaemon(True)
		thread.start()
		return True
Esempio n. 26
0
def init(app):
    def update_commands(signal_id):
        log.debug('Updating commands...')
        from pajbot.models.command import CommandManager
        bot_commands = CommandManager(
                socket_manager=None,
                module_manager=ModuleManager(None).load(),
                bot=None).load(load_examples=True)
        app.bot_commands_list = bot_commands.parse_for_web()

        app.bot_commands_list.sort(key=lambda x: (x.id or -1, x.main_alias))
        del bot_commands

    update_commands(26)
    try:
        import uwsgi
        from uwsgidecorators import thread, timer
        uwsgi.register_signal(26, 'worker', update_commands)
        uwsgi.add_timer(26, 60 * 10)

        @thread
        @timer(5)
        def get_highlight_thumbnails(no_clue_what_this_does):
            from pajbot.web.models.thumbnail import StreamThumbnailWriter
            with DBManager.create_session_scope() as db_session:
                highlights = db_session.query(StreamChunkHighlight).filter_by(thumbnail=None).all()
                if len(highlights) > 0:
                    log.info('Writing {} thumbnails...'.format(len(highlights)))
                    StreamThumbnailWriter(app.bot_config['main']['streamer'], [h.id for h in highlights])
                    log.info('Done!')
                    for highlight in highlights:
                        highlight.thumbnail = True
    except ImportError:
        log.exception('Import error, disregard if debugging.')
        pass
    pass
Esempio n. 27
0
 def register_signal(self, num, who, function):
     '''  
     num : the signal number to configure
     who :
         a magic string that will set which process/processes receive the signal.
         worker/worker0 will send the signal to the first available worker. This is the default if you specify an empty string.
         workers will send the signal to every worker.
         workerN (N > 0) will send the signal to worker N.
         mule/mule0 will send the signal to the first available mule. (See uWSGI Mules)
         mules will send the signal to all mules
         muleN (N > 0) will send the signal to mule N.
         cluster will send the signal to all the nodes in the cluster. Warning: not implemented.
         subscribed will send the signal to all subscribed nodes. Warning: not implemented.
         spooler will send the signal to the spooler.
         cluster and subscribed are special, as they will send the signal to the master of all cluster/subscribed nodes. 
         The other nodes will have to define a local handler though, to avoid a terrible signal storm loop.
     function : A callable that takes a single numeric argument.
     '''
     return uwsgi.register_signal(num, who, function)
Esempio n. 28
0
import os
import uwsgi
from CronOrder.ele import *
from CronOrder.ordermig import *
from CronOrder.method import *
from ProxyWork.method import *
import sys
reload(sys)
sys.setdefaultencoding('utf8')

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "SeaSite.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

uwsgi.register_signal(80, "", resetToken)
uwsgi.add_timer(80, 1800)
uwsgi.register_signal(82, "", getproxy)
uwsgi.add_cron(82, 0, 10, -1, -1, -1)
uwsgi.register_signal(84, "", getproxy)
uwsgi.add_cron(84, 0, 15, -1, -1, -1)
uwsgi.register_signal(86, "", checkproxy)
uwsgi.add_cron(86, 0, 9, -1, -1, -1)
uwsgi.register_signal(88, "", checkproxy)
uwsgi.add_cron(88, 0, 16, -1, -1, -1)
uwsgi.register_signal(90, "", migrateorder)
uwsgi.add_cron(90, 0, 0, -1, -1, -1)
Esempio n. 29
0
def start_uwsgi_timer(freq, type_, callable_):
    import uwsgi
    uwsgi.register_signal(66, type_, callable_)
    uwsgi.add_timer(66, freq)
Esempio n. 30
0
def start_uwsgi_timer(freq, type_, callable_):
    import uwsgi
    uwsgi.register_signal(66, type_, callable_)
    uwsgi.add_timer(66, freq)
Esempio n. 31
0
stack_dump_file = '/tmp/manhole-pid'
uwsgi_signal_number = 17

try:
    import uwsgi

    if not os.path.exists(stack_dump_file):
        open(stack_dump_file, 'w')

    def open_manhole(dummy_signum):
        with open(stack_dump_file, 'r') as fh:
            pid = fh.read().strip()
            if pid == str(os.getpid()):
                inst = manhole.install(strict=False, thread=False)
                inst.handle_oneshot(dummy_signum, dummy_signum)

    uwsgi.register_signal(uwsgi_signal_number, 'workers', open_manhole)
    uwsgi.add_file_monitor(uwsgi_signal_number, stack_dump_file)

    print("Listening for stack mahole requests via %r" % (stack_dump_file,), file=sys.stderr)
except ImportError:
    print("Not running under uwsgi; unable to configure manhole trigger", file=sys.stderr)
except IOError:
    print("IOError creating manhole trigger %r" % (stack_dump_file,), file=sys.stderr)


def application(env, sr):
    sr('200 OK', [('Content-Type', 'text/plain'), ('Content-Length', '2')])
    yield b'OK'
"""
WSGI config for NewRaPo project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/
"""

import os

import uwsgi
from app.blog_lab.proxy.method import get_proxy, check_proxy
from app.blog_lab.proxy.huiyuan import play

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "NewRaPo.settings.produce")

from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()

uwsgi.register_signal(82, "", get_proxy)
uwsgi.add_cron(82, 0, -1, -1, -1, -1)
uwsgi.register_signal(84, "", check_proxy)
uwsgi.add_cron(84, 30, -1, -1, -1, -1)
uwsgi.register_signal(86, "", play)
uwsgi.add_cron(86, 0, 8, -1, -1, -1)
Esempio n. 33
0
    return getattr(logging, request.args.get('level'))

def set_log_level(sig=None):
    'set log level from current request'
    logger = current_app.logger

    if sig is not None and sig != UWSGI_SIG_SET_LOG_LEVEL:
        logger.info('refusing to set log level on signal: %s', sig)
        return

    level = parse_log_level()
    level_name = logging.getLevelName(level)
    msg = 'Setting log level to: %s'

    logger.debug(msg, level_name)
    logger.info(msg, level_name)
    logger.warning(msg, level_name)

    logger.setLevel(level)

try:
    import uwsgi
    uwsgi.register_signal(UWSGI_SIG_SET_LOG_LEVEL, 'workers', set_log_level) 
except ImportError:
    pass


if __name__ == '__main__':
    app = create_app()
    app.run(debug=False)
Esempio n. 34
0
 def __call__(self, f):
     uwsgi.register_signal(self.num, self.target, f)
     uwsgi.add_file_monitor(self.num, self.fsobj)
     return f
Esempio n. 35
0
                                    # throttling
                                    time.sleep(60)
            except:
                logging.exception('Failed to update')
            time.sleep(60)
    finally:
        uwsgi.unlock(signum)


logging.basicConfig(level=logging.INFO)

logging.getLogger('botocore.vendored.requests.packages.urllib3.connectionpool').setLevel(logging.WARNING)
app = connexion.App(__name__)
app.add_api('swagger.yaml')
application = app.app

try:
    import uwsgi
    for i in range(1, 1 + PARALLEL):
        signum = i
        uwsgi.register_signal(signum, "", run_update)
        uwsgi.add_timer(signum, 10)

    # initialization for /metrics endpoint (ZMON support)
    uwsgi_metrics.initialize()
except Exception as e:
    print(e)

if __name__ == '__main__':
    app.run(port=8080)
Esempio n. 36
0
 def __call__(self, f):
     uwsgi.register_signal(self.num, self.target, f)
     uwsgi.add_cron(self.num, self.minute, self.hour,
         self.day, self.month, self.dayweek)
     return f
Esempio n. 37
0
from main.msgs import system_notify
import json
import traceback
import decimal
from decimal import Decimal, getcontext
import datetime
import calendar
import time
from datetime import timedelta
try:
    from main.my_cache_key import my_lock, my_release, LockBusyException, check_freq

    import uwsgi
except:
    traceback.print_exc()
"""
uwsgi.register_signal(99,  "worker1", gracefull_stop)
uwsgi.register_signal(98,  "worker1", gracefull_reload)
uwsgi.register_signal(97,  "worker1", gracefull_start)
uwsgi.register_signal(96,  "worker1", gracefull_suspend)
"""


def start(req):
    uwsgi.signal(97)
    return json_true(req, {})


def stop(req):
    uwsgi.signal(99)
    return json_true(req, {})
Esempio n. 38
0
    def register_ipython_console(self, name):
        trigger = IPYTHON_CONSOLE_TRIGGER % name
        os.close(os.open(trigger, os.O_WRONLY|os.O_CREAT, 0666))

        uwsgi.register_signal(IPYTHON_CONSOLE_SIGNAL, 'mule', activate_ipython_console)
        uwsgi.add_file_monitor(IPYTHON_CONSOLE_SIGNAL, trigger)
Esempio n. 39
0
jitsi = ServerList('jitsi_servers.lst', 'sounds/outgoingRinging.wav')
poll = ServerList('poll_servers.lst', 'images/date.png')
pad = ServerList('pad_servers.lst', None)
codimd = ServerList('codimd_servers.lst', 'screenshot.png')
cryptpad = ServerList('cryptpad_servers.lst', 'customize/images/AGPL.png')
etherpad = ServerList('etherpad_servers.lst', 'locales.json')
ethercalc = ServerList('ethercalc_servers.lst', 'static/img/davy/bg/home2.png')


def reload(signum):
    print("start reload")
    global jitsi
    global poll
    global pad
    global codimd
    global cryptpad
    global etherpad
    global ethercalc
    jitsi.renew()
    poll.renew()
    pad.renew()
    codimd.renew()
    cryptpad.renew()
    etherpad.renew()
    ethercalc.renew()
    print("finish reload")


uwsgi.register_signal(99, "", reload)
uwsgi.add_timer(99, 600)
Esempio n. 40
0
 def register_signal(self, handler=None):
     if uwsgi and self._signal_id is None:
         handler = handler or self.signal_handler
         uwsgi.register_signal(self.signal_id, self.target, handler)
         return True
     return False
Esempio n. 41
0
 def register_signal(self, handler=None):
     if uwsgi and self._signal_id is None:
         handler = handler or self.signal_handler
         uwsgi.register_signal(self.signal_id, self.target, handler)
         return True
     return False
Esempio n. 42
0
class Application(object):
	def __init__(self):
		self.i = 0
		print "init!!!", self.i

	def cleanup(self):
		print "cleanup!!!", self.i

	def __call__(self, env, start_response):
		start_response('200 OK', [('Content-Type','text/html')])
		self.i += 1
		sleep = random.random() * 3
		# pprint.pprint(locals())
		print os.getpid(), self.i, sleep

		time.sleep(sleep)
		print os.getpid(), self.i, "x"
		return "Hello World %s" % self.i

application = Application()
uwsgi.atexit = application.cleanup
atexit.register(myExit)
# h = handler()
# handler.old = signal.getsignal(9)
# signal.signal(9, h.handle)

signal._old = signal.getsignal(9)
uwsgi.register_signal(9, "workers", myHandle)
# signal.signal(15, myHandle)
Esempio n. 43
0
File: banny.py Progetto: akx/banny
                return ("200 OK", {"X-Accel-Redirect": img}, "OK")
            else:
                return ("404 Not Found", {}, "404")

        if pfx == "/c/":
            url = self.get_url(banner_id)
            if url:
                self.clk_cache[banner_id] += 1
                return ("302 OK", {"Location": url}, url)
            else:
                return ("404 Not Found", {}, "404")

    def __call__(self, environ, start_response):
        resp = self.serve(environ["PATH_INFO"])
        if resp:
            resp[1]["Content-Length"] = str(len(resp[2]))
            start_response(resp[0], resp[1].items())
            return resp[2]
        else:
            return self.next_application(environ, start_response)


def commit_banners(x):
    banner_server.commit()

from wsgiref.simple_server import demo_app
application = banner_server = BannerServer(demo_app)

uwsgi.register_signal(42, 'workers', commit_banners)
uwsgi.add_rb_timer(42, 5)
Esempio n. 44
0
File: uwsgi.py Progetto: esho/spire
    def register_signal(self, name, target=None, function=None):
        id = len(self.signals) + 10
        self.signals[id], self.signals[name] = name, id

        if target:
            uwsgi.register_signal(id, target, function)
Esempio n. 45
0
    print "i am the signal %d" % num


def hello_signal2(num, payload):
    print "i am the signal %d with payload: %s" % (num, payload)


def hello_file(num, filename):
    print "file %s has been modified !!!" % filename


def hello_timer(num, secs):
    print "%s seconds elapsed" % secs

# uwsgi.register_signal(30, uwsgi.SIGNAL_KIND_WORKER, hello_signal)
uwsgi.register_signal(30, "workers", hello_signal)
uwsgi.register_signal(22, "worker", hello_signal2, "*** PAYLOAD FOO ***")

uwsgi.register_file_monitor(3, "/tmp", "workers", hello_file)
uwsgi.register_timer(26, 2, "worker", hello_timer)
uwsgi.register_timer(17, 4, "worker2", hello_timer)
uwsgi.register_timer(5, 8, "worker3", hello_timer)


def application(env, start_response):

    start_response('200 Ok', [('Content-Type', 'text/html')])

    # this will send a signal to the master that will report it to the first available worker
    uwsgi.signal(30)
    uwsgi.signal(22)
Esempio n. 46
0
    from pajbot.models.command import CommandManager
    bot_commands = CommandManager(
            socket_manager=None,
            module_manager=ModuleManager(None).load(),
            bot=None).load(load_examples=True)
    bot_commands_list = bot_commands.parse_for_web()

    bot_commands_list = sorted(bot_commands_list, key=lambda x: (x.id or -1, x.main_alias))
    del bot_commands


update_commands(26)
try:
    import uwsgi
    from uwsgidecorators import thread, timer
    uwsgi.register_signal(26, "worker", update_commands)
    uwsgi.add_timer(26, 60 * 10)

    @thread
    @timer(5)
    def get_highlight_thumbnails(no_clue_what_this_does):
        from pajbot.web.models.thumbnail import StreamThumbnailWriter
        with DBManager.create_session_scope() as db_session:
            highlights = db_session.query(StreamChunkHighlight).filter_by(thumbnail=None).all()
            if len(highlights) > 0:
                log.info('Writing {} thumbnails...'.format(len(highlights)))
                StreamThumbnailWriter(config['main']['streamer'], [h.id for h in highlights])
                log.info('Done!')
                for highlight in highlights:
                    highlight.thumbnail = True
except ImportError:
Esempio n. 47
0
#app = Flask(__name__)
@rpc('hello')
def hellorpc(foo, bar):
	return "%s-%s-%s" (foo, bar)
def hello():
	return "Hello World"
uwsgi.register_rpc("hello", hello)
def helloapp(env, start_response):
	start_response('200 Ok', [('Content-Type', 'text/html')])
	return uwsgi.rpc('127.0.0.1:3031', 'hello')
def hello_file(num):
	print "/tmp has been modified !!!"
def get(key):
	if key not in domains:
		return DEFAULT_NODE
	# get the node to forward requests to
	nodes = domains[key]['nodes']
	current_node = domains[key]['node']
	value = nodes[current_node]
	# round robin :P
	next_node = current_node + 1
	if next_node >= len(nodes):
		next_node = 0
		domains[key]['node'] = next_node
		return value
#thr root: 
#application = ...
uwsgi.register_signal(17, "worker", hello_file)
uwsgi.add_file_monitor(17, "/tmp")
uwsgi.applications = {'': application, '/app': app, '/helloapp': helloapp}
Esempio n. 48
0
 def __call__(self, f):
     uwsgi.register_signal(self.num, self.target, f)
     return f
Esempio n. 49
0
    return "{} °C".format(temp)


def get_ble_addr():
    # get the bluetooth controller name from bluetoothctl worker
    qr = get_db().execute(
        "SELECT macaddr FROM bluetooth WHERE hostdev = 1").fetchall()
    if len(qr) > 0:
        return ", ".join(map(itemgetter("macaddr"), qr))
    else:
        return "Unknown"


if has_uwsgi:
    pong = Event()
    uwsgi.register_signal(3, "workers", lambda x: pong.set())

    def ping_worker():
        pong.clear()
        uwsgi.mule_msg(b"bt ping", 1)
        return pong.wait(3)

    def mule_msg_iter(timeout):
        end = monotonic() + timeout
        while True:
            timeout = max(0, end - monotonic())
            # secret timeout parameter that only cool kids know about!!!
            item = uwsgi.mule_get_msg(timeout=int(ceil(timeout)))
            if item is not None and len(item) > 0:
                yield item
            elif timeout == 0:
Esempio n. 50
0
 def __call__(self, f):
     uwsgi.register_signal(self.num, self.target, f)
     uwsgi.add_rb_timer(self.num, self.secs)
     return f
Esempio n. 51
0
# this module will update a carbon server (used by the graphite tool: http://graphite.wikidot.com/)
# with requests count made by a Django app (and can track graphite itself as it is a Django app ;)
#
#
 
import os
import uwsgi
import time
from django.core.handlers.wsgi import WSGIHandler
 
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
 
CARBON_SERVER = "127.0.0.1:2003"
 
def update_carbon(signum):
    # connect to the carbon server
    carbon_fd = uwsgi.connect(CARBON_SERVER)
    # send data to the carbon server
    uwsgi.send(carbon_fd, "uwsgi.%s.requests %d %d\n" % (uwsgi.hostname, uwsgi.total_requests(), int(time.time())))
    # close the connection with the carbon server
    uwsgi.close(carbon_fd)
 
# register a new uwsgi signal (signum: 17)
uwsgi.register_signal(17, '', update_carbon)
 
# attach a timer of 10 seconds to signal 17
uwsgi.add_timer(17, 10)
 
# the Django app
application = WSGIHandler()
Esempio n. 52
0
   os.path.abspath(os.path.join(os.path.dirname(__file__), '../../application/website')),
   os.path.abspath(os.path.join(os.path.dirname(__file__), '../../application')),
   os.path.abspath(os.path.join(os.path.dirname(__file__), '../../')),
   )

for path in paths:
   sys.path.insert(0,path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'application.settings.xsf_stage2'

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()


#################### Test uWSGI Cron begin ############################
## uwsgi.add_cron(signal, minute, hour, day, month, weekday)
## uwsgi.add_timer(signal, sec)

import uwsgi

from scripts.cron_job import *

for job_id, job in enumerate(jobs):
    uwsgi.register_signal(job_id, "", job['name'])
    if len(job['time']) == 1:
        uwsgi.add_timer(job_id, job['time'][0])
    else:
        uwsgi.add_cron(job_id, job['time'][0], job['time'][1], job['time'][2], job['time'][3], job['time'][4])

#################### Test uWSGI Cron end ############################
Esempio n. 53
0
import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "rcasite.settings")

# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()


try:
    import uwsgi
    from django.core.management import call_command
    print("We have a uWSGI")

    def make_task_runner(task):
        def task_runner(unused):
            if uwsgi.i_am_the_lord(os.getenv("CFG_APP_NAME")):
                print("I am the lord.")
                print("Running %s" % task)
                call_command(task, interactive=False)
            else:
                print("I am not the lord.")
        return task_runner

    uwsgi.register_signal(100, "", make_task_runner('set_page_random_order'))
    uwsgi.add_timer(100, 60 * 60)  # Run every hour
except ImportError:
    print("We have no uWSGI")