Esempio n. 1
0
def main():
    if len(sys.argv) < 2:
        print 'Usage: %s CONFIG_FILE' % sys.argv[0]
        sys.exit(1)

    signal.signal(signal.SIGINT, sigint_handler)

    from iris_api.api import get_api, load_config_file

    config = load_config_file(sys.argv[1])
    app = get_api(config)

    server = config['server']
    print 'LISTENING: %(host)s:%(port)d' % server
    server = WSGIServer((server['host'], server['port']), app)

    if HAS_INOTIFY:
        fd = inotify.init()

        for dirname, subfolders, _ in os.walk('.'):
            if '.git' in subfolders:
                subfolders.remove('.git')
            inotify.add_watch(fd, dirname, inotify.IN_MODIFY)

        gevent.spawn(event_producer, fd, server)
    else:
        print 'Missing inotify, disable watch support.'
    server.serve_forever()
Esempio n. 2
0
 def _work(args, jobs):
     if args.raven:
         import raven
         RAVEN = raven.Client(args.raven)
     else:
         RAVEN = None
     import gevent_inotifyx as inotifyx
     fd = inotifyx.init()
     # NOTE: .git/logs/HEAD is the last thing updated after a git pull/merge
     inotifyx.add_watch(fd, '../.git/logs/HEAD', inotifyx.IN_MODIFY)
     inotifyx.add_watch(fd, '.reloader', inotifyx.IN_MODIFY | inotifyx.IN_ATTRIB)
     db = THRIFT_CONSTRUCTOR()
     while 1:
         try:
             work = jobs.get_work(args.queues, timeout=5)
             if work:
                 jobs.add_work(True, 'old' + work[0], **work[1])
                 job_worker(db=db, **work[1])
             if inotifyx.get_events(fd, 0):
                 print('Shutting down due to new update')
                 break
         except:
             if RAVEN:
                 RAVEN.captureException()
             raise
Esempio n. 3
0
 def _work(args, jobs):
     if args.raven:
         import raven
         RAVEN = raven.Client(args.raven)
     else:
         RAVEN = None
     import gevent_inotifyx as inotifyx
     fd = inotifyx.init()
     # NOTE: .git/logs/HEAD is the last thing updated after a git pull/merge
     inotifyx.add_watch(fd, '../.git/logs/HEAD', inotifyx.IN_MODIFY)
     inotifyx.add_watch(fd, '.reloader',
                        inotifyx.IN_MODIFY | inotifyx.IN_ATTRIB)
     db = THRIFT_CONSTRUCTOR()
     while 1:
         try:
             work = jobs.get_work(args.queues, timeout=5)
             if work:
                 jobs.add_work(True, 'old' + work[0], **work[1])
                 job_worker(db=db, **work[1])
             if inotifyx.get_events(fd, 0):
                 print('Shutting down due to new update')
                 break
         except:
             if RAVEN:
                 RAVEN.captureException()
             raise
Esempio n. 4
0
    def __init__(self, config_manager, filename, interval=3):
        # TODO: check file existance and read-permission.
        self.filename = filename
        self.interval = interval

        self.config_manager = config_manager
        self.notifier = inotify.init()
        inotify.add_watch(self.notifier, filename, inotify.IN_CLOSE_WRITE)
Esempio n. 5
0
    def watch(self):
        fd = inotifyx.init()

        for name in self.pickup_dir:
            try:
                self.wd_to_name[inotifyx.add_watch(fd, name, inotifyx.IN_CLOSE_WRITE | inotifyx.IN_MOVE)] = name
            except IOError, e:
                logger.warn('Caught IOError `%s`, name:`%s`', format_exc(e), name)
Esempio n. 6
0
    def __init__(self, logger, directory="rules/"):
        self.logger = logger
        self.directory = directory

        if not os.access(self.directory, os.R_OK):
            raise Exception("Directory '%s' is not readable. Please verify." % (self.directory))

        self.fd = inotify.init()
        self.wb = inotify.add_watch(self.fd, self.directory, inotify.IN_CLOSE_WRITE + inotify.IN_CREATE + inotify.IN_DELETE + inotify.IN_MODIFY + inotify.IN_MOVE)
Esempio n. 7
0
    def watch(self):
        fd = inotifyx.init()

        for name in self.pickup_dir:
            try:
                self.wd_to_name[inotifyx.add_watch(
                    fd, name,
                    inotifyx.IN_CLOSE_WRITE | inotifyx.IN_MOVE)] = name
            except IOError, e:
                logger.warn('Caught IOError `%s`, name:`%s`', format_exc(e),
                            name)
Esempio n. 8
0
    def start(self):
        self.fd = inotify.init()
        self.ddirs = {}
        for startpath in self.paths:
            for ddir in j.system.fs.listDirsInDir(startpath, True):
                ddirname = j.system.fs.getDirName(ddir + "/", True)
                # print ddirname
                ok = True
                if ddirname.find(".") == 0 or ddir.find(".cache") != -1:
                    ok = False

                if ok:
                    try:
                        wd = inotify.add_watch(
                            self.fd, ddir,
                            inotify.IN_CREATE | inotify.IN_DELETE
                            | inotify.IN_DELETE_SELF | inotify.IN_MODIFY
                            | inotify.IN_MOVE_SELF | inotify.IN_MOVED_FROM
                            | inotify.IN_MOVED_TO)
                        # print "add notify %s" % ddir
                        self.ddirs[wd] = ddir
                    except:
                        print "ERROR COULD NOT ADD notify %s" % ddir

        print "started"
        while True:
            events = inotify.get_events(self.fd)
            for event in events:
                # j.put(event)
                if event.name != None:
                    path = self.ddirs[event.wd] + "/" + event.name
                else:
                    path = self.ddirs[event.wd]
                    print "restarted for %s" % path
                    self.start()

                if path.find(".goutputstream") != -1:
                    continue

                print "received event:", event.get_mask_description(), path

                if j.system.fs.isFile(path):
                    if j.system.fs.getFileExtension(path).lower() in [
                            "wiki", "py", "spec"
                    ]:
                        try:
                            self.contentmgr.notifychange(path)
                            print "notified %s" % path
                        except:
                            print "could not notify change for %s" % path
                elif j.system.fs.isDir(path):
                    print "restarted for dir %s" % path
                    self.start()
Esempio n. 9
0
    def reloader():
        import gevent_inotifyx as inotifyx

        fd = inotifyx.init()
        # NOTE: .git/logs/HEAD is the last thing updated after a git pull/merge
        inotifyx.add_watch(fd, "../.git/logs/HEAD", inotifyx.IN_MODIFY)
        inotifyx.add_watch(fd, ".reloader", inotifyx.IN_MODIFY | inotifyx.IN_ATTRIB)
        inotifyx.get_events(fd)
        logging.warn("Shutting down due to new update")
        SERVER.stop_accepting()
        logging.warn("Free Count[%d] (will kill outstanding processes in 120 sec.)" % SERVER.pool.free_count())
        SERVER.pool.join(timeout=120)
        SERVER.close()
        logging.warn("Shut down successful")
Esempio n. 10
0
    def start(self):
        self.fd = inotify.init()
        self.ddirs = {}
        for startpath in self.paths:
            for ddir in j.system.fs.listDirsInDir(startpath, True):
                ddirname = j.system.fs.getDirName(ddir + "/", True)
                # print ddirname
                ok = True
                if ddirname.find(".") == 0 or ddir.find(".cache") != -1:
                    ok = False

                if ok:
                    try:
                        wd = inotify.add_watch(self.fd, ddir, inotify.IN_CREATE | inotify.IN_DELETE
                                               | inotify.IN_DELETE_SELF | inotify.IN_MODIFY
                                               | inotify.IN_MOVE_SELF | inotify.IN_MOVED_FROM
                                               | inotify.IN_MOVED_TO)
                        # print "add notify %s" % ddir
                        self.ddirs[wd] = ddir
                    except:
                        print "ERROR COULD NOT ADD notify %s" % ddir

        print "started"
        while True:
            events = inotify.get_events(self.fd)
            for event in events:
                # j.put(event)
                if event.name != None:
                    path = self.ddirs[event.wd] + "/" + event.name
                else:
                    path = self.ddirs[event.wd]
                    print "restarted for %s" % path
                    self.start()

                if path.find(".goutputstream") != -1:
                    continue

                print "received event:", event.get_mask_description(), path

                if j.system.fs.isFile(path):
                    if j.system.fs.getFileExtension(path).lower() in ["wiki", "py", "spec"]:
                        try:
                            self.contentmgr.notifychange(path)
                            print "notified %s" % path
                        except:
                            print "could not notify change for %s" % path
                elif j.system.fs.isDir(path):
                    print "restarted for dir %s" % path
                    self.start()
Esempio n. 11
0
    def __init__(self, logger, directory="rules/"):
        self.logging = logger
        self.directory = directory

        if not os.access(self.directory, os.R_OK):
            raise Exception("Directory '%s' is not readable. Please verify." %
                            (self.directory))

        self.fd = inotify.init()
        self.wb = inotify.add_watch(
            self.fd, self.directory,
            inotify.IN_CLOSE_WRITE + inotify.IN_CREATE + inotify.IN_DELETE +
            inotify.IN_MODIFY + inotify.IN_MOVE)
        self.logging.info("Monitoring directory '%s' for changes" %
                          (directory))
Esempio n. 12
0
    def watch(self):
        fd = inotifyx.init()
        inotifyx.add_watch(fd, self.pickup_dir, inotifyx.IN_CLOSE_WRITE | inotifyx.IN_MOVE)
        
        while self.keep_running:
            try:
                events = inotifyx.get_events(fd, 1.0)
                for event in events:
                    self.pickup_event_processor.process(event)

                sleep(0.1)
            except KeyboardInterrupt:
                self.keep_running = False

        os.close(fd)
Esempio n. 13
0
    def watch(self):
        fd = inotifyx.init()
        inotifyx.add_watch(fd, self.pickup_dir, inotifyx.IN_CLOSE_WRITE | inotifyx.IN_MOVE)

        while self.keep_running:
            try:
                events = inotifyx.get_events(fd, 1.0)
                for event in events:
                    self.pickup_event_processor.process(event)

                sleep(0.1)
            except KeyboardInterrupt:
                self.keep_running = False

        os.close(fd)
Esempio n. 14
0
 def reloader():
     import gevent_inotifyx as inotifyx
     fd = inotifyx.init()
     # NOTE: .git/logs/HEAD is the last thing updated after a git pull/merge
     inotifyx.add_watch(fd, '../.git/logs/HEAD', inotifyx.IN_MODIFY)
     inotifyx.add_watch(fd, '.reloader',
                        inotifyx.IN_MODIFY | inotifyx.IN_ATTRIB)
     inotifyx.get_events(fd)
     logging.warn('Shutting down due to new update')
     SERVER.stop_accepting()
     logging.warn(
         'Free Count[%d] (will kill outstanding processes in 120 sec.)' %
         SERVER.pool.free_count())
     SERVER.pool.join(timeout=120)
     SERVER.close()
     logging.warn('Shut down successful')
Esempio n. 15
0
    def monitorDirectory(self):
        '''Monitors the given directory for changes.'''

        fd = inotify.init()
        wb = inotify.add_watch(fd, self.directory,
                               inotify.IN_CLOSE_WRITE + inotify.IN_DELETE)
        self.__rules = self.readDirectory()

        while True:
            self.wait.clear()
            events = inotify.get_events(fd)
            current_rules = self.readDirectory()

            if cmp(current_rules, self.__rules) != 0:
                self.__rules = current_rules
                self.wait.set()
Esempio n. 16
0
    def run(self):
        glt = ezRPGreenlet.EzReverseProxyGreenlet(self._logger, self._sfh)
        self._logger.info("starting greenlet for thrift service...")
        clientGreenlet = gevent.spawn(glt.clientServiceGreenlet)
        self._logger.info("started")

        self._logger.info("starting greenlet to monitor zookeeper...")
        kzGreenlet = gevent.spawn(glt.kzMonitorGreenlet)
        self._logger.info("started")

        self._logger.info(
            "starting greenlet to write out nginx configuration changes...")
        cfgProcessorGreenlet = gevent.spawn(
            glt.configurationChangeQueueGreenlet)
        self._logger.info("started")

        self._logger.info("starting greenlet to monitor for shutdown...")
        fd = gevent_inotifyx.init()
        wd = gevent_inotifyx.add_watch(fd, gConfig.shutdownFile,
                                       gevent_inotifyx.IN_DELETE)
        wGreenlet = gevent.spawn(glt.watchGreenlet, fd)
        self._logger.info("started")

        gConfig.addGreenlets(clientGreenlet, kzGreenlet, cfgProcessorGreenlet,
                             wGreenlet)

        gevent.joinall([clientGreenlet])
        self._logger.warn("joined thrift service greenlet")
        gevent.joinall([kzGreenlet])
        self._logger.warn("joined zookeeper monitoring greenlet")
        gConfig.run = False
        while not gConfig.configurationChangeQueue.empty():
            print "queue not empty"
            gConfig.configurationChangeQueue.get()
            print "got"
            gConfig.configurationChangeQueue.task_done()
        print "joining conf queue"
        gConfig.configurationChangeQueue.join()
        self._logger.warn("joined configuration queue")
        gevent.joinall([cfgProcessorGreenlet])
        self._logger.warn("joined configuration change greenlet")
        gConfig.wGreenlet.join()
        self._logger.warn("joined shutdown monitor greenlet")
        ServiceDiscoveryClient(gConfig.zk).unregister_endpoint(
            gConfig.appName, EzFrontendServiceName, gConfig.internal_hostname,
            gConfig.thriftPort)
        self._logger.warn("unregistered from discovery service")
Esempio n. 17
0
def inotify_event(file_path, rules):
    fd = inotify.init()
    (config_path, config_name) = os.path.split(file_path)
    if not config_path:
        config_path = os.getcwd()
    file_name = config_path + '/' + config_name
    wd = inotify.add_watch(
        fd, config_path, inotify.IN_MODIFY | inotify.IN_CREATE)
    rules.update(reload_config(file_name))
    while True:
        events = inotify.get_events(fd)
        update = False
        for event in events:
            if event.name == config_name:
                update = True
        if update:
            rules.update(reload_config(file_name))
    def run(self):
      glt = ezRPGreenlet.EzReverseProxyGreenlet(self._logger, self._sfh)
      self._logger.info("starting greenlet for thrift service...")
      clientGreenlet = gevent.spawn(glt.clientServiceGreenlet)
      self._logger.info("started")

      self._logger.info("starting greenlet to monitor zookeeper...")
      kzGreenlet = gevent.spawn(glt.kzMonitorGreenlet)
      self._logger.info("started")

      self._logger.info("starting greenlet to write out nginx configuration changes...")
      cfgProcessorGreenlet = gevent.spawn(glt.configurationChangeQueueGreenlet)
      self._logger.info("started")

      self._logger.info("starting greenlet to monitor for shutdown...")
      fd = gevent_inotifyx.init()
      wd = gevent_inotifyx.add_watch(fd, gConfig.shutdownFile, gevent_inotifyx.IN_DELETE)
      wGreenlet = gevent.spawn(glt.watchGreenlet, fd)
      self._logger.info("started")

      gConfig.addGreenlets(clientGreenlet, kzGreenlet, cfgProcessorGreenlet, wGreenlet)


      gevent.joinall([clientGreenlet])
      self._logger.warn("joined thrift service greenlet")
      gevent.joinall([kzGreenlet])
      self._logger.warn("joined zookeeper monitoring greenlet")
      gConfig.run = False
      while not gConfig.configurationChangeQueue.empty():
          print "queue not empty"
          gConfig.configurationChangeQueue.get()
          print "got"
          gConfig.configurationChangeQueue.task_done()
      print "joining conf queue"
      gConfig.configurationChangeQueue.join()
      self._logger.warn("joined configuration queue")
      gevent.joinall([cfgProcessorGreenlet])
      self._logger.warn("joined configuration change greenlet")
      gConfig.wGreenlet.join()
      self._logger.warn("joined shutdown monitor greenlet")
      ServiceDiscoveryClient(gConfig.zk).unregister_endpoint(gConfig.appName,
                                                             EzFrontendServiceName,
                                                             gConfig.internal_hostname,
                                                             gConfig.thriftPort)
      self._logger.warn("unregistered from discovery service")
Esempio n. 19
0
    def wait_for_plugin_changes(self):
        import gevent_inotifyx as inotify

        fd = inotify.init()
        inotify.add_watch(fd, 'rowboat/plugins/', inotify.IN_MODIFY)
        while True:
            events = inotify.get_events(fd)
            for event in events:
                # Can't reload core.py sadly
                if event.name == 'core.py':
                    continue

                plugin_name = '{}Plugin'.format(
                    event.name.split('.', 1)[0].title())
                if plugin_name in self.bot.plugins:
                    self.log.info('Detected change in %s, reloading...',
                                  plugin_name)
                    self.bot.plugins[plugin_name].reload()
Esempio n. 20
0
    def __init__(self, server, config):
        self.server = server
        self.config = config
        self.keep_running = True
        self.watchers = []
        self.infx_fd = infx.init()
        self._parser_cache = {}

        # Maps inotify's watch descriptors to paths
        self.wd_to_path = {}

        # Unlike the main config dictionary, this one is keyed by incoming directories
        self.callback_config = Bunch()

        for stanza, section_config in self.config.items():
            cb_config = self.callback_config.setdefault(
                section_config.pickup_from, Bunch())
            cb_config.update(section_config)
            cb_config.stanza = stanza
Esempio n. 21
0
    def _run(self):
        if not use_inotify:
            log.warn("gevent_inotifyx not loaded; not using inotify")
            return

        fd = inotify.init()
        wd = inotify.add_watch(fd, self.opts.spool_dir,
                inotify.IN_CLOSE_WRITE | inotify.IN_MOVED_TO)
        while True:
            events = inotify.get_events(fd)
            for event in events:
                path = os.path.sep.join([self.opts.spool_dir,
                                         event.name])

                # Filter out inotify events generated for files that
                # have been already unlinked from the filesystem
                # (IN_EXCL_UNLINK emulation)
                if os.path.exists(path):
                    self.on_find(path)
Esempio n. 22
0
    def wait_for_plugin_changes(self):
        import gevent_inotifyx as inotify

        fd = inotify.init()
        inotify.add_watch(fd, 'rowboat/plugins/', inotify.IN_MODIFY)
        while True:
            events = inotify.get_events(fd)
            for event in events:
                # Can't reload core.py sadly
                if event.name.startswith('core.py'):
                    continue

                plugin_name = '{}Plugin'.format(event.name.split('.', 1)[0].title())
                plugin = next((v for k, v in self.bot.plugins.items() if k.lower() == plugin_name.lower()), None)
                if plugin:
                    self.log.info('Detected change in %s, reloading...', plugin_name)
                    try:
                        plugin.reload()
                    except Exception:
                        self.log.exception('Failed to reload: ')
Esempio n. 23
0
 def __init__(self, file_path):
     self.path = file_path
     self.parent = pth.dirname(pth.abspath(self.path))
     self.q = Queue()
     self.fd = inotify.init()
Esempio n. 24
0
        DEBUG = True
    elif o in ("-r", "--run"):
        if a == 'live':
            STREAM_SERVER = '216.18.184.22:1935'
            STREAM_USER = '******'
            PIC_PATH = '/srv/LIVE_model_imgs_ramfs/'
            ONLINE_MODELS_URL = 'http://www.seeme.com/onlinemodels'
            PID_FILE = '/tmp/LIVE_ffmpeg_websocket_server.pid'
            PORT = 8023
            FLASH_PORT = 10843
    else:
        assert False, "unhandled option"

q = Queue()
q.maxsize = 1000
fd = inotify.init()
inotify.add_watch(fd, PIC_PATH, inotify.IN_CREATE)
stream_dumper = StreamDumper()
gevent.spawn(event_producer, fd, q)

signal.signal(signal.SIGTERM, exit_cleanup)
signal.signal(signal.SIGINT, exit_cleanup)
signal.signal(signal.SIGQUIT, exit_cleanup)
atexit.register(exit_cleanup)

if DEBUG: print("writing pid file: %s" % PID_FILE)
with open(PID_FILE, 'w') as f:
    f.write(str(os.getpid()))

# start ffmpeg processes for online models
r = requests.get(ONLINE_MODELS_URL)
 def setUp(self):
     self.workdir = os.path.abspath(os.getcwd())
     self.testdir = os.path.join(self.workdir, 'test')
     os.mkdir(self.testdir)
     os.chdir(self.testdir)
     self.fd = inotify.init()
Esempio n. 26
0
 def prepare(self):
     super(FileWatcher, self).prepare()
     self._inotify_fd = inotify.init()
     self._wd = inotify.add_watch(self._inotify_fd, self.watch_path,
                                  inotify.IN_CLOSE_WRITE)
     logger.info("Listening path: %s" % self.watch_path)