コード例 #1
0
ファイル: Reporter.py プロジェクト: moliqingwa/PyAid
    def flush(self):
        if not self._buffer:
            return

        if sys.platform.startswith('win'):
            return

        items = []
        for b in self._buffer:
            try:
                d    = DictUtils.merge(self._meta, b['data'])
                item = b['prefix'] + ' ' + JSON.asString(d)
            except Exception as err:
                item = '>> EXCEPTION: JSON ENCODING FAILED >> ' + str(err).replace('\n', '\t')

            try:
                item = item.encode('utf8', 'ignore')
            except Exception as err:
                item = '>> EXCEPTION: UNICODE DECODING FAILED >> ' + str(err).replace('\n', '\t')

            items.append(item)

        count   = self._fileCount
        offset  = random.randint(0, count - 1)
        success = False
        path    = self.getReportFolder() + self._timeCode + '/'
        if not os.path.exists(path):
            os.makedirs(path)

        for i in range(count):
            index = (i + offset) % count
            p     = path + str(index) + '.report'
            lock  = FileLock(p)
            if lock.i_am_locking() and i < count - 1:
                continue

            try:
                lock.acquire()
            except Exception:
                continue

            try:
                out = StringUtils.toUnicode('\n'.join(items) + '\n')
                f   = open(p, 'a+')
                f.write(out.encode('utf8'))
                f.close()
                success = True
            except Exception as err:
                print("REPORTER ERROR: Unable to write report file.")
                print(err)

            lock.release()
            if success:
                break

        self.clear()
        return success
コード例 #2
0
def getFileLock(location, filename):
    lock = FileLock("%s/%s" % (location, filename))
    while not lock.i_am_locking():
        try:
            lock.acquire(timeout=60)
        except:
            lock.break_lock()
            lock.acquire()
    return lock
コード例 #3
0
ファイル: functions.py プロジェクト: MrTyton/RSS-Reader
def getFileLock(location, filename):
    lock = FileLock("%s/%s" % (location, filename))
    while not lock.i_am_locking():
        try:
            lock.acquire(timeout=60)
        except:
            lock.break_lock()
            lock.acquire()
    return lock
コード例 #4
0
ファイル: __init__.py プロジェクト: zackives/opensubmit
def _acquire_lock(config):
    '''
        Determines if the executor is configured to run fetched jobs one after the other.
        If this is the case, then check if another script instance is still running.
    '''
    if config.getboolean("Execution", "serialize"):
        lock = FileLock(config.get("Execution", "pidfile"))
        try:
            if lock.is_locked() and not lock.i_am_locking():
                logger.debug("Already locked")
                return False
            else:
                lock.acquire()
                logger.debug("Got the script lock")
        except Exception as e:
            logger.error("ERROR locking. " + str(e))
            return False
    return True
コード例 #5
0
def get_lock():
    '''Get a file-lock, breaking the lock if it has been held for too long.

:return: A lock, which may be locked. Use the
         :meth:`lockfile.FileLock.i_am_locking` method to find out if the
         lock is held, or not.
:rtype: :class:`lockfile.FileLock`

It is more than possible for Postfix to consume every single thread on the
sever. By adding a lock, with a short timeout, we slow down the consumption of
threads by the asynchronous email UI; this allows the Web UI to be responsive.

We break the lock if the lock is very old because it is more than possible for
something to crash with the lock taken. (It does assume that no email will
take more than :const:`BREAK_LOCK_AGE` to process.)

If the file is still locked, after we wait for something to finish and check
that the lock is not too old, then we exit. Postfix will try running the
script with the same arguments again later.

**Example**::

    from gs.group.messsages.add.smtp2gs import locker
    lock = locker.get_lock()
    if lock.i_am_locking():
        # Do stuff.
'''
    # TODO: Use Redis for the locking
    # (We have the config for Redis in the config file)

    create_file(LOCK_NAME)
    # The following is a modification of the example from the lockfile
    # documentation <http://packages.python.org/lockfile/lockfile.html>
    lock = FileLock(LOCK_NAME)
    lock_file = LOCK_NAME + '.lock'
    if not lock.i_am_locking():
        if (isfile(lock_file) and (age(lock_file) >= BREAK_LOCK_AGE)):
            lock.break_lock()

    try:
        lock.acquire(timeout=MAX_LOCK_TIMEOUT)
    except LockTimeout:
        pass
    return lock
コード例 #6
0
def upload_directory(directory,
                     album_name,
                     notify_emails_on_album_creation=False,
                     ext='.jpg',
                     auto_delete_oldest=False,
                     with_prefix=None,
                     **kwargs):
    assert os.path.isdir(directory), "'%s' is not a valid directory." % (directory,)
    from lockfile import FileLock, LockTimeout
    try:
        # Acquire lock, so multiple instances of this aren't uploading twice.
        lock = FileLock(UPLOAD_DIRECTORY_LOCK)
        lock_acquired = False
        while not lock.i_am_locking():
            try:
                lock.acquire(timeout=5)
                lock_acquired = True
            except LockTimeout:
                return
        # Post-lock-acquired.
        while 1:
            files = sorted([fn for fn in os.listdir(directory) if fn.endswith(ext)])
            if files:
                for fn in files:
                    upload(path=os.path.join(directory,fn),
                           album_name=album_name,
                           notify_emails_on_album_creation=notify_emails_on_album_creation,
                           auto_delete_oldest=auto_delete_oldest,
                           with_prefix=with_prefix,
                           delete_original=1)
            else:
                # No more files, so exit.
                break
    finally:
        if lock_acquired:
            lock.release()
コード例 #7
0
ファイル: dlrd.py プロジェクト: selimppc/jasminsms
    lock = None
    try:
        options = Options()
        options.parseOptions()

        # Must not be executed simultaneously (c.f. #265)
        lock = FileLock("/tmp/dlrd-%s" % options['id'])

        # Ensure there are no paralell runs of this script
        lock.acquire(timeout=2)

        # Prepare to start
        dlr_d = DlrDaemon(options)
        # Setup signal handlers
        signal.signal(signal.SIGINT, dlr_d.sighandler_stop)
        # Start DlrDaemon
        dlr_d.start()

        reactor.run()
    except usage.UsageError as errortext:
        print('%s: %s' % (sys.argv[0], errortext))
        print('%s: Try --help for usage details.' % (sys.argv[0]))
    except LockTimeout:
        print("Lock not acquired ! exiting")
    except AlreadyLocked:
        print("There's another instance on dlrd running, exiting.")
    finally:
        # Release the lock
        if lock is not None and lock.i_am_locking():
            lock.release()
コード例 #8
0
                            if k == 'bound_rx_count':
                                v = r['bind_receiver']
                            elif k == 'bound_tx_count':
                                v = r['bind_transmitter']
                            elif k == 'bound_trx_count':
                                v = r['bind_transceiver']
                        else:
                            v = get_stats_value(response, k, stat_type = 'SMPP Server')
                        metrics.append(Metric(jcli['host'], 'jasmin[user.smppsapi.%s,%s]' % (k, uid), v))

        #print metrics
        # Send packet to zabbix
        send_to_zabbix(metrics, zabbix_host, zabbix_port)
    except LockTimeout:
        print 'Lock not acquired, exiting'
    except AlreadyLocked:
        print 'Already locked, exiting'
    except Exception, e:
        print type(e)
        print 'Error: %s' % e
    finally:
        if tn is not None and tn.get_socket():
            tn.close()

        # Release the lock
        if lock.i_am_locking():
            lock.release()

if __name__ == '__main__':
    main()
コード例 #9
0
if __name__ == '__main__':
    # Must not be executed simultaneously (c.f. #265)
    lock = FileLock("/tmp/jasmind.lock")

    try:
        options = Options()
        options.parseOptions()

        # Ensure there are no paralell runs of this script
        lock.acquire(timeout=2)

        # Prepare to start
        ja_d = JasminDaemon(options)
        # Setup signal handlers
        signal.signal(signal.SIGINT, ja_d.sighandler_stop)
        # Start JasminDaemon
        ja_d.start()

        reactor.run()
    except usage.UsageError, errortext:
        print '%s: %s' % (sys.argv[0], errortext)
        print '%s: Try --help for usage details.' % (sys.argv[0])
    except LockTimeout:
        print "Lock not acquired ! exiting"
    except AlreadyLocked:
        print "There's another instance on jasmind running, exiting."
    finally:
        # Release the lock
        if lock.i_am_locking():
            lock.release()
コード例 #10
0
    total_posted = 0

    logging.info("Writing to sink: %s", sinkname)

    message_archive_filename = home + "/.navierstokes/message_archive_" + sinkname + ".txt"
    if not os.path.exists(message_archive_filename):
        open(message_archive_filename, 'w').close()
        pass

    lock = FileLock(message_archive_filename)

    lock_try_count = 0
    max_lock_try_count = 6
    obtained_lock = False
    while (not lock.i_am_locking()) and lock_try_count <= max_lock_try_count:
        try:
            lock.acquire(timeout=10)
        except LockTimeout:
            lock_try_count += 1
            logging.info(
                "Lock acquisition: %s",
                "Will try again to acquire a file lock on the message archive. (Try count = %d)"
                % (lock_try_count))
            pass

        if lock.i_am_locking():
            obtained_lock = True
            break

        elif lock_try_count == max_lock_try_count - 1:
コード例 #11
0
ファイル: dlrlookupd.py プロジェクト: jookies/jasmin
    lock = None
    try:
        options = Options()
        options.parseOptions()

        # Must not be executed simultaneously (c.f. #265)
        lock = FileLock("/tmp/dlrlookupd-%s" % options['id'])

        # Ensure there are no paralell runs of this script
        lock.acquire(timeout=2)

        # Prepare to start
        ja_d = DlrlookupDaemon(options)
        # Setup signal handlers
        signal.signal(signal.SIGINT, ja_d.sighandler_stop)
        # Start DlrlookupDaemon
        ja_d.start()

        reactor.run()
    except usage.UsageError as errortext:
        print('%s: %s' % (sys.argv[0], errortext))
        print('%s: Try --help for usage details.' % (sys.argv[0]))
    except LockTimeout:
        print("Lock not acquired ! exiting")
    except AlreadyLocked:
        print("There's another instance on dlrlookupd running, exiting.")
    finally:
        # Release the lock
        if lock is not None and lock.i_am_locking():
            lock.release()
コード例 #12
0
ファイル: Reporter.py プロジェクト: hannahp/PyAid
                item = '>> EXCEPTION: UNICODE DECODING FAILED >> ' + str(err).replace('\n', '\t')

            items.append(item)

        count   = self._fileCount
        offset  = random.randint(0, count - 1)
        success = False
        path    = self.getReportFolder() + self._timeCode + '/'
        if not os.path.exists(path):
            os.makedirs(path)

        for i in range(count):
            index = (i + offset) % count
            p     = path + str(index) + '.report'
            lock  = FileLock(p)
            if lock.i_am_locking() and i < count - 1:
                continue

            try:
                lock.acquire()
            except Exception, err:
                continue

            try:
                out = unicode(u'\n'.join(items) + u'\n')
                f   = open(p, 'a+')
                f.write(out.encode('utf8'))
                f.close()
                success = True
            except Exception, err:
                print "REPORTER ERROR: Unable to write report file."