def initialize(self): _outcome = None if _outcome is None: if self.initialized: _outcome = outcomes.fail("operation already initialized") if self.initialized is None: _outcome = outcomes.fail("operation lifecycle completed") if _outcome is None: self.transaction = None try: self.transaction = self.queue.store.environment.txn_begin( self.queue.transaction, db.DB_TXN_SYNC | db.DB_TXN_NOWAIT ) except: _outcome = outcomes.fail_with_traceback("operation failed to open BDB transaction") if _outcome is None: _outcome = self.initialize_() if not _outcome.succeeded: if self.transaction is not None: try: self.transaction.abort() except: print "!!!!!" del self.transaction else: self.initialized = True return _outcome
def initialize(self): _outcome = None if _outcome is None: if self.initialized: _outcome = outcomes.fail("queue already initialized") if self.initialized is None: _outcome = outcomes.fail("queue lifecycle completed") if _outcome is None: if self.session is not None: self.transaction = self.session.transaction else: self.transaction = None self.queue = None self.data = None self.recycle = None try: self.queue = db.DB(self.store.environment, 0) # self.queue.set_flags (db.DB_INORDER, 1) # self.queue.set_flags (db.DB_CHKSUM, 1) self.queue.set_re_len(128 / 8) self.queue.open( "queue.%s" % (self.uuid), None, db.DB_QUEUE, db.DB_THREAD | db.DB_AUTO_COMMIT | db.DB_CREATE, 0666, None, ) self.data = db.DB(self.store.environment, 0) # self.data.set_flags (db.DB_CHKSUM, 1) self.data.open( "queues.data", None, db.DB_HASH, db.DB_THREAD | db.DB_AUTO_COMMIT | db.DB_CREATE, 0666, None ) self.recycle = db.DB(self.store.environment, 0) # self.recycle.set_flags (db.DB_CHKSUM, 1) self.recycle.set_re_len(512 / 8) self.recycle.open( "queues.recycle", None, db.DB_QUEUE, db.DB_THREAD | db.DB_AUTO_COMMIT | db.DB_CREATE, 0666, None ) except: _outcome = outcomes.fail_with_traceback("queue failed to open either queue database, or data database") if self.queue is not None: try: self.queue.close() except: print "!!!!!" if self.data is not None: try: self.data.close() except: print "!!!!!" del self.transaction del self.queue del self.data del self.recycle if _outcome is None: self.operations = set() self.initialized = True _outcome = outcomes.succeed() return _outcome
def poll(self): if self.initialized: if not self.prepared: _outcome = None _data = None try: _record = self.queue.queue.consume(self.transaction) if _record is not None: _index, _uuid = _record self.queue.recycle.append(_uuid, self.transaction) _data = self.queue.data.get(_uuid, None, None, 0, -1, -1) if _data is None: _outcome = outcomes.fail("record is in queue but not in data") except: _outcome = outcomes.fail_with_traceback("operation failed from unknown reasons") if _outcome is None: if _data is not None: self.data = _data self.prepared = True else: self.prepared = False _outcome = outcomes.succeed(prepared=self.prepared) else: _outcome = outcomes.succeed(prepared=True) else: _outcome = outcomes.fail("operation not initialized") return _outcome
def select(self): if self.initialized: if self.prepared: _outcome = outcomes.succeed(data=self.data) else: _outcome = outcomes.fail("operation not prepared") else: _outcome = outcomes.fail("operation not initialized") return _outcome
def update(self, _data): if self.initialized: self.data = _data _outcome = outcomes.succeed() else: _outcome = outcomes.fail("operation not initialized") return _outcome
def main () : if len (sys.argv) > 0 : _command = sys.argv[1] _arguments = sys.argv[2:] if _command == 'enqueue' : _outcome = enqueue (_arguments) elif _command == 'dequeue' : _outcome = dequeue (_arguments) else : _outcome = outcomes.fail ('invalid command') else : _outcome = outcomes.fail ('void arguments') if _outcome.succeeded : _outcome = 0 else : print >> sys.stderr, '[ee]', _outcome.message _outcome = 1 sys.exit (_outcome)
def queue(self, _name): _outcome = None if _outcome is None: if not self.initialized: _outcome = outcomes.fail("store not initialized") if _outcome is None: _queue = Queue(self, None, _name) _outcome = _queue.initialize() if _outcome.succeeded: self.queues.add(_queue) _outcome = outcomes.succeed(queue=_queue) return _outcome
def dequeue(self): _outcome = None if _outcome is None: if not self.initialized: _outcome = outcomes.fail("queue not initialized") if _outcome is None: _operation = DequeueOperation(self) _outcome = _operation.initialize() if _outcome.succeeded: self.operations.add(_operation) _outcome = outcomes.succeed(operation=_operation) return _outcome
def session(self): _outcome = None if _outcome is None: if not self.initialized: _outcome = outcomes.fail("store not initialized") if _outcome is None: _session = Session(self) _outcome = _session.initialize() if _outcome.succeeded: self.sessions.add(_session) _outcome = outcomes.succeed(session=_session) return _outcome
def finalize(self): _outcome = None if _outcome is None: if not self.initialized: _outcome = outcomes.fail("queue not initialized") if _outcome is None: if len(self.operations) > 0: _outcome = outcomes.fail("queue has pending operations") if _outcome is None: try: self.queue.close() _outcome_1 = outcomes.succeed() except: _outcome_1 = outcomes.fail_with_traceback("queue failed to close BDB queue database") try: self.data.close() _outcome_2 = outcomes.succeed() except: _outcome_2 = outcomes.fail_with_traceback("queue failed to close BDB data database") try: self.recycle.close() _outcome_3 = outcomes.succeed() except: _outcome_3 = outcomes.fail_with_traceback("queue failed to close BDB data database") if self.session is not None: self.session.queues.remove(self) else: self.store.queues.remove(self) del self.store del self.session del self.name self.initialized = None del self.transaction del self.queue del self.data del self.recycle del self.operations _outcome = outcomes.succeed_if_all(_outcome_1, _outcome_2, _outcome_3) return _outcome
def initialize(self): _outcome = None if _outcome is None: if self.initialized: _outcome = outcomes.fail("store already initialized") if self.initialized is None: _outcome = outcomes.fail("store lifecycle completed") if _outcome is None: self.environment = None try: self.environment = db.DBEnv(0) self.environment.open( self.path, db.DB_INIT_LOCK | db.DB_INIT_LOG | db.DB_INIT_MPOOL | db.DB_INIT_TXN | db.DB_THREAD | db.DB_CREATE | db.DB_RECOVER | db.DB_REGISTER, 0666, ) except: _outcome = outcomes.fail_with_traceback("store failed to open BDB environment") if self.environment is not None: try: self.environment.close(0) except: print "!!!!!" del self.environment if _outcome is None: self.sessions = set() self.queues = set() self.initialized = True _outcome = outcomes.succeed() return _outcome
def finalize(self, _commit): _outcome = None if _outcome is None: if not self.initialized: _outcome = outcomes.fail("session not initialized") if _outcome is None: if len(self.queues) > 0: _outcome = outcomes.fail("session has opened queues") if _outcome is None: try: if _commit: self.transaction.commit(0) else: self.transaction.abort() except: _outcome = outcomes.fail_with_traceback("session failed to close BDB transaction") self.store.sessions.remove(self) del self.store self.initialized = None del self.transaction del self.queues if _outcome is None: _outcome = outcomes.succeed() return _outcome
def finalize(self): _outcome = None if _outcome is None: if not self.initialized: _outcome = outcomes.fail("store not initialized") if _outcome is None: if len(self.sessions) > 0: _outcome = outcomes.fail("store has pending sessions") if _outcome is None: if len(self.queues) > 0: _outcome = outcomes.fail("store has opened queues") if _outcome is None: try: self.environment.close(0) except: _outcome = outcomes.fail_with_traceback("store failed to close BDB environment") del self.path self.initialized = None del self.environment del self.sessions del self.queues if _outcome is None: _outcome = outcomes.succeed() return _outcome
def finalize(self, _commit): _outcome = None if _outcome is None: if not self.initialized: _outcome = outcomes.fail("operation not initialized") if _outcome is None: _outcome_1 = self.finalize_(_commit) try: if _commit: self.transaction.commit(0) else: self.transaction.abort() _outcome_2 = outcomes.succeed() except: _outcome_2 = outcomes.fail_with_traceback("operation failed to close BDB transaction") self.queue.operations.remove(self) del self.queue self.initialized = None del self.transaction _outcome = outcomes.succeed_if_all_0(_outcome_1, _outcome_2) return _outcome
def failed (self, _message, _errno) : return outcomes.fail (_message, errno = _errno)
def enqueue (_arguments) : _outcome = None if _outcome is None : if len (_arguments) == 3 : _environment_path = _arguments[0] _queue_name = _arguments[1] _data_path = _arguments[2] else : _outcome = outcomes.fail ('enqueue invalid arguments') _stream = None _data = None if _outcome is None : try : _stream = file (_data_path, 'rb') _data = _stream.read (-1) _stream.close () except : _outcome = outcomes.fail_with_traceback ('failed while opening or reading input file') if _stream is not None : try : _stream.close () except : pass _store = None if _outcome is None : _store = store.Store (_environment_path) _outcome = _store.initialize () if _outcome.succeeded : _outcome = None _session = None if _outcome is None : _outcome = _store.session () if _outcome.succeeded : _session = _outcome.session _outcome = None _queue = None if _outcome is None : _outcome = _session.queue (_queue_name) if _outcome.succeeded : _queue = _outcome.queue _outcome = None _operation = None if _outcome is None : _outcome = _queue.enqueue () if _outcome.succeeded : _operation = _outcome.operation _outcome = None if _outcome is None : _outcome = _operation.update (_data) if _outcome.succeeded : _outcome = None if _outcome is None : _outcome = _operation.finalize (True) _operation = None if _outcome.succeeded : _outcome = None if _outcome is None : _outcome = _queue.finalize () _queue = None if _outcome.succeeded : _outcome = None if _outcome is None : _outcome = _session.finalize (True) _session = None if _outcome.succeeded : _outcome = None if _outcome is None : _outcome = outcomes.succeed () _outcome_0 = _outcome if _operation is not None : _outcome_1 = _operation.finalize (False) _operation = None else : _outcome_1 = None if _queue is not None : _outcome_2 = _queue.finalize () _queue = None else : _outcome_2 = None if _session is not None : _outcome_3 = _session.finalize (False) _session = None else : _outcome_3 = None if _store is not None : _outcome_4 = _store.finalize () _store = None else : _outcome_4 = None _outcome = outcomes.succeed_if_all (_outcome_0, _outcome_1, _outcome_2, _outcome_3, _outcome_4) return _outcome
def dequeue (_arguments) : _outcome = None if _outcome is None : if len (_arguments) == 3 : _environment_path = _arguments[0] _queue_name = _arguments[1] _data_path = _arguments[2] else : _outcome = outcomes.fail ('dequeue invalid arguments') _store = None if _outcome is None : _store = store.Store (_environment_path) _outcome = _store.initialize () if _outcome.succeeded : _outcome = None _session = None if _outcome is None : _outcome = _store.session () if _outcome.succeeded : _session = _outcome.session _outcome = None _queue = None if _outcome is None : _outcome = _session.queue (_queue_name) if _outcome.succeeded : _queue = _outcome.queue _outcome = None _operation = None if _outcome is None : _outcome = _queue.dequeue () if _outcome.succeeded : _operation = _outcome.operation _outcome = None _data = None if _outcome is None : _outcome = _operation.poll () if _outcome.succeeded : if _outcome.prepared : _outcome = None else : _outcome = outcomes.fail ('dequeue unavailable data') if _outcome is None : _outcome = _operation.select () if _outcome.succeeded : _data = _outcome.data _outcome = None _stream = None if _outcome is None : try : _stream = file (_data_path, 'wb') _stream.write (_data) _stream.close () except : _outcome = outcomes.fail_with_traceback ('failed while opening or writing output file') if _stream is not None : try : _stream.close () except : pass if _outcome is None : _outcome = _operation.finalize (True) _operation = None if _outcome.succeeded : _outcome = None if _outcome is None : _outcome = _queue.finalize () _queue = None if _outcome.succeeded : _outcome = None if _outcome is None : _outcome = _session.finalize (True) _session = None if _outcome.succeeded : _outcome = None if _outcome is None : _outcome = outcomes.succeed () _outcome_0 = _outcome if _operation is not None : _outcome_1 = _operation.finalize (False) _operation = None else : _outcome_1 = None if _queue is not None : _outcome_2 = _queue.finalize () _queue = None else : _outcome_2 = None if _session is not None : _outcome_3 = _session.finalize (False) _session = None else : _outcome_3 = None if _store is not None : _outcome_4 = _store.finalize () _store = None else : _outcome_4 = None _outcome = outcomes.succeed_if_all (_outcome_0, _outcome_1, _outcome_2, _outcome_3, _outcome_4) return _outcome