Пример #1
0
    def send(self, key, ts, message):
        dt = datetime.utcfromtimestamp(ts)
        filename = os.path.join(self.path,
            self.fmt.format(key=key, ts=ts, dt=dt, kwargs=self.kwargs))

        dname = os.path.dirname(filename)
        if not os.path.exists(dname):
            log.info('%s: Creating directory %s' % (self, dname))
            os.makedirs(dname)

        if filename in self.openfiles:
            fd = self.openfiles[filename]
        else:
            fd = file(filename, 'a')
            self.openfiles[filename] = fd

            # If half of the soft max open file limit is reached, close one
            if len(self.openfiles) > (resource.getrlimit(
                resource.RLIMIT_NOFILE)[0] / 2):
                p = self.openfiles.popitem()
                if p != fd:
                    log.info('%s: Closing %s to conserve file descriptors' % \
                        (self, p.name))
                    p.flush()
                    p.close()

        line = self.msgfmt.format(key=key, ts=ts, dt=dt, message=message)
        with fd:
            fd.write(line)
            if self.sync:
                fd.flush()
Пример #2
0
 def run(self):
     self.sock = socket.socket()
     self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     self.sock.bind(('0.0.0.0', self.port))
     self.sock.listen(2)
     while self.running:
         try:
             newsock, address = self.sock.accept()
             log.info('%s: Accepted connection from %s' % (self, address[0]))
             eventlet.spawn_n(self.handle, newsock.makefile('rw'))
         except socket.error, e:
             if self.running:
                 log.error('%s: Error accepting connection: %s' % (self, e))
             break
Пример #3
0
 def run(self):
     log.debug('%s: Spawning %s' % (self, self.command))
     self.proc = subprocess.Popen(self.command, shell=True, bufsize=0,
                                  stdout=subprocess.PIPE)
     while True:
         line = self.proc.stdout.readline()
         if not line:
             break
         self.process(self.key, int(time.time()), line.rstrip('\r\n '))
     
     ret = self.proc.wait()
     if ret != 0:
         log.warning('%s: Exited with return code %i' % (self,
             ret))
     else:
         log.info('%s: Exited with return code %i' % (self, ret))
Пример #4
0
 def stop(self):
     log.info('%s: Closing %i files' % (self, len(self.openfiles)))
     for filename, fd in self.openfiles.iteritems():
         fd.flush()
         fd.close()
     self.openfiles = {}