def test_with_context(self): with ConsulServiceController().start_service() as service: consul_client = service.create_consul_client() lock_manager = ConsulLockManager(consul_client=consul_client) with lock_manager.acquire(KEY_1) as lock_information: self.assertEqual(lock_information, lock_manager.find(KEY_1)) self.assertIsNone(lock_manager.find(KEY_1))
def main(lock_key: str): lock_manager = ConsulLockManager() lock = lock_manager.find(lock_key) logger.debug(f"Lock with key \"{lock_key}\": {lock}") if lock is not None and lock.metadata is not None and JOB_ID_LOCK_METADATA_KEY in lock.metadata: job_id = lock.metadata[JOB_ID_LOCK_METADATA_KEY] logger.info(f"Lock currently held by CI job with ID: {job_id}") job_running = is_ci_job_running(job_id) logger.info(f"CI job with ID {job_id} {'is' if job_running else 'is not'} running") if not job_running: logger.info(f"Releasing lock for {lock.key} held by non-running job {job_id}") released = lock_manager.release(lock.key) logger.info("Released lock!" if released is not None else "Did not manage to release lock (someone else " "probably else released it before me)")
def main(lock_key: str): lock_manager = ConsulLockManager() lock = lock_manager.find(lock_key) logger.debug(f"Lock with key \"{lock_key}\": {lock}") if lock is not None and lock.metadata is not None and JOB_ID_LOCK_METADATA_KEY in lock.metadata: job_id = lock.metadata[JOB_ID_LOCK_METADATA_KEY] logger.info(f"Lock currently held by CI job with ID: {job_id}") job_running = is_ci_job_running(job_id) logger.info( f"CI job with ID {job_id} {'is' if job_running else 'is not'} running" ) if not job_running: logger.info( f"Releasing lock for {lock.key} held by non-running job {job_id}" ) released = lock_manager.release(lock.key) logger.info("Released lock!" if released is not None else "Did not manage to release lock (someone else " "probably else released it before me)")
def find(service: ConsulDockerisedService): lock_manager = ConsulLockManager(consul_client=service.create_consul_client()) found_lock = lock_manager.find(KEYS_1[0]) self.assertEqual(KEYS_1[0], found_lock.key)
def test_find_when_no_locks(self): with ConsulServiceController().start_service() as service: lock_manager = ConsulLockManager(consul_client=service.create_consul_client()) self.assertIsNone(lock_manager.find(KEY_1))
def find(service: ConsulDockerisedService): lock_manager = ConsulLockManager( consul_client=service.create_consul_client()) found_lock = lock_manager.find(KEYS_1[0]) self.assertEqual(KEYS_1[0], found_lock.key)
def test_find_when_no_locks(self): with ConsulServiceController().start_service() as service: lock_manager = ConsulLockManager( consul_client=service.create_consul_client()) self.assertIsNone(lock_manager.find(KEY_1))