Exemple #1
0
 def connect(self):
   if self.isconnected and self.conn.ping():
     return True
   try:
     with open(self.lockfile) as master:
       config = master.read().split('\n')[0].split(',')
     if len(config) < 2:
       logging.error('[CacheClient] ERROR. Lock file is corrupt.')
       return False
     self.host = config[0]
     self.port = config[1]
     pool = redis.ConnectionPool(host=self.host, port=self.port, db=0, decode_responses=True)
     if self.isconnected:
       self.conn.connection_pool = pool
       logging.info('[CacheClient] Switched over to new master at %s on port %s', self.host, self.port)
     else:
       self.conn = redis.StrictRedis(host=self.host, port=self.port, connection_pool=pool, decode_responses=True)
       self.conn.client_setname(getUID())
       logging.info('[CacheClient] Connected as client to master at %s on port %s', self.host, self.port)
       self.isconnected = True
     return True
   except FileNotFoundError as ex:
     logging.error('[CacheClient] ERROR. Service is not running. No lockfile found: %s  (Returning False)', self.lockfile)
     return False
   except redis.BusyLoadingError as ex:
     logging.error('[CacheClient] Service is loading.')
     self.isconnected = True
     return True
Exemple #2
0
  def removeItems(self, key, itemlist):
    nullvalue = getUID()

    pipe = self.pipeline()
    for index in itemlist:
      pipe.lset(key, index, nullvalue)

    pipe.lrem(key, 0, nullvalue)
    pipe.execute()
Exemple #3
0
    def removeItems(self, key, itemlist):
        nullvalue = getUID()

        pipe = self.pipeline()
        for index in itemlist:
            pipe.lset(key, index, nullvalue)

        pipe.lrem(key, 0, nullvalue)
        pipe.execute()
Exemple #4
0
  def __init__(self, name):
    settings = systemsettings()
    self._name_app = name
    self.lockfile = '%s_%s.lock' % (self._name_app, 'RedisService')
    self.isconnected = False
    self.host = None
    self.port = None
    self.pool = None

    connect_wait = 0
    while True:
      try:
        with open(self.lockfile, 'r') as conn:
          conn_string = conn.read().split(',')
          if len(conn_string) < 5:
            raise OverlayNotAvailable
        host = conn_string[0]
        port = conn_string[1]
        ttl  = float(conn_string[3])
        state = conn_string[4]
        ts = dt.now()
        if ts.timestamp() > ttl:
          logging.warning("Overlay Service Master has expired. Using this at risk!")
        self.pool = redis.ConnectionPool(host=host, port=port, db=0, decode_responses=True)
        redis.StrictRedis.__init__(self, connection_pool=self.pool, decode_responses=True)
        logging.info('Connection is open... Checking to see if I can connect')
        self.host = host
        self.port = port
        # If set name suceeds. The Db is connected. O/W handle the error below accordingly
        self.client_setname(getUID())
        logging.info('[Redis Client] Connected as client to master at %s on port %s', host, port)
        self.isconnected = True
        break
      except FileNotFoundError:
        logging.warning('[Redis Client] No Lockfile Found. Service unavailable: %s', self.lockfile)
        raise OverlayNotAvailable
      except redis.ReadOnlyError as e:
        logging.warning('[Redis Client] Connecting as read only')
        self.isconnected = True
        break
      except redis.BusyLoadingError as e:
        logging.warning('[Redis Client] Current Master is starting up. Standing by.....')
        time.sleep(5)
        connect_wait += (dt.now()-start).total_seconds()
        continue
      except redis.RedisError:
        logging.warning('[Redis Client] Service is not running. Cannot get master from lockfile: %s', self.lockfile)
        raise OverlayNotAvailable
Exemple #5
0
  def lock_acquire(self, key, hold_time=30):
    """ Acquires a lock for the given key (concurrency control)
    """
    timeout = int(hold_time * 1.5)

    while True:
      lock = self.get(key + ':LOCK')
      if lock is None:
        logging.info('Acquiring Lock for %s', key)
        unique_key = getUID()
        self.set(key + ':LOCK', unique_key)
        self.expire(key + ':LOCK', hold_time)
        return unique_key   # Return unique key for this process
      timeout -= 1
      if timeout == 0:
        logging.warning('Timedout waiting to aqcuire lock on %s', key)
        break
      logging.info('Waiting to acquire Lock for %s.....', key)
      time.sleep(3)
    return None
Exemple #6
0
    def lock_acquire(self, key, hold_time=30):
        """ Acquires a lock for the given key (concurrency control)
    """
        timeout = int(hold_time * 1.5)

        while True:
            lock = self.get(key + ':LOCK')
            if lock is None:
                logging.info('Acquiring Lock for %s', key)
                unique_key = getUID()
                self.set(key + ':LOCK', unique_key)
                self.expire(key + ':LOCK', hold_time)
                return unique_key  # Return unique key for this process
            timeout -= 1
            if timeout == 0:
                logging.warning('Timedout waiting to aqcuire lock on %s', key)
                break
            logging.info('Waiting to acquire Lock for %s.....', key)
            time.sleep(3)
        return None
Exemple #7
0
    def __init__(self, name):
        settings = systemsettings()
        self._name_app = name
        self.lockfile = '%s_%s.lock' % (self._name_app, 'RedisService')
        self.isconnected = False
        self.host = None
        self.port = None
        self.pool = None

        connect_wait = 0
        while True:
            try:
                with open(self.lockfile, 'r') as conn:
                    conn_string = conn.read().split(',')
                    if len(conn_string) < 5:
                        raise OverlayNotAvailable
                host = conn_string[0]
                port = conn_string[1]
                ttl = float(conn_string[3])
                state = conn_string[4]
                ts = dt.now()
                if ts.timestamp() > ttl:
                    logging.warning(
                        "Overlay Service Master has expired. Using this at risk!"
                    )
                self.pool = redis.ConnectionPool(host=host,
                                                 port=port,
                                                 db=0,
                                                 decode_responses=True)
                redis.StrictRedis.__init__(self,
                                           connection_pool=self.pool,
                                           decode_responses=True)
                logging.info(
                    'Connection is open... Checking to see if I can connect')
                self.host = host
                self.port = port
                # If set name suceeds. The Db is connected. O/W handle the error below accordingly
                self.client_setname(getUID())
                logging.info(
                    '[Redis Client] Connected as client to master at %s on port %s',
                    host, port)
                self.isconnected = True
                break
            except FileNotFoundError:
                logging.warning(
                    '[Redis Client] No Lockfile Found. Service unavailable: %s',
                    self.lockfile)
                raise OverlayNotAvailable
            except redis.ReadOnlyError as e:
                logging.warning('[Redis Client] Connecting as read only')
                self.isconnected = True
                break
            except redis.BusyLoadingError as e:
                logging.warning(
                    '[Redis Client] Current Master is starting up. Standing by.....'
                )
                time.sleep(5)
                connect_wait += (dt.now() - start).total_seconds()
                continue
            except redis.RedisError:
                logging.warning(
                    '[Redis Client] Service is not running. Cannot get master from lockfile: %s',
                    self.lockfile)
                raise OverlayNotAvailable