def send_stopped_tread_notification(cls, credential_id): cls.get_logger().warning( f'Stoping follower updating proccess with {credential_id}.') SlackHelper().post_message_to_channel( "Un thread del servicio TweetUpdateService dejo de funcionar.", "#errors") CredentialService().unlock_credential(credential_id, cls.__name__)
def initialize_tweet_update_service_with_credentials(cls): """ Update tweet of some candidates' followers. """ cls.get_logger().info('Starting follower updating process.') try: credentials = CredentialService().get_all_credentials_for_service( cls.__name__) except CredentialsAlreadyInUseError as caiue: cls.get_logger().error(caiue.message) cls.get_logger().warning('Tweets updating process skipped.') return cls.run_process_with_credentials(credentials)
def restart_credential(cls, credential_id): cls.get_logger().error('After waiting, restart credential.') try: # Use TweetUpdateService class name due to the credential is unlocked in that class. credential = CredentialService( ).get_credential_with_id_for_service( credential_id, TweetUpdateService.__class__.__name__) except NoAvailableCredentialsError: cls.get_logger().error('Can not restart credential.') return cls.get_logger().error( f'Restarting credential with id: {credential_id}.') cls.run_process_with_credentials([credential])
def update_followers(cls): """ Update all candidates' followers. """ # Get credentials for service cls.get_logger().info('Starting follower updating process.') try: credentials = CredentialService().get_all_credentials_for_service( cls.__name__) except CredentialsAlreadyInUseError as caiue: cls.get_logger().error(caiue.message) cls.get_logger().warning('Follower updating process skipped.') return # Run follower update process AsyncThreadPoolExecutor().run(cls.update_with_credential, credentials) cls.get_logger().info('Finished follower updating.')
def initialize_context(cls): """ Create instances of all environment services in a Spring-like fashion.""" cls.LOGGER.info('Instantiating context services and components.') ConfigurationManager() ConcurrencyUtils() Scheduler() CandidateDAO() RawFollowerDAO() CandidatesFollowersDAO() CredentialService() CandidateService() FollowerUpdateService() TweetUpdateService() FollowersQueueService()
def update_with_credential(cls, credential): """ Update followers with an specific Twitter API credential. """ cls.get_logger().info( f'Starting follower updating with credential {credential.id}.') # Create Twython instance for credential twitter = cls.twitter(credential) # While there are candidates to update, get and update candidate = cls.next_candidate() while candidate is not None: # Update followers cls.update_followers_for_candidate(twitter, candidate) # Finish using candidate CandidateService().finish_follower_updating(candidate) # Get next candidate candidate = cls.next_candidate() # Unlock credential for this service CredentialService().unlock_credential(credential.id, cls.__name__) cls.get_logger().info( f'Finished updating followers with credential {credential.id}')
class TestCredentialService(CustomTestCase): def setUp(self) -> None: super(TestCredentialService, self).setUp() path = f"{abspath(join(dirname(__file__), '../..'))}/resources/test_credentials.json" CredentialService.CREDENTIALS_PATH = path self.target = CredentialService() def tearDown(self) -> None: # This has to be done because we are testing a Singleton CredentialService._instances.clear() def test_get_credentials_available_when_none_occupied(self): credential = self.target.get_credential_for_service('test-service') assert credential is not None assert credential.id == 'rdr' def test_get_credentials_available_when_one_occupied(self): _ = self.target.get_credential_for_service('test-service') credential = self.target.get_credential_for_service('test-service') assert credential is not None assert credential.id == 'fg' def test_get_credentials_raise_exception_when_all_occupied(self): _ = self.target.get_credential_for_service('test-service') _ = self.target.get_credential_for_service('test-service') with self.assertRaises(NoAvailableCredentialsError) as context: _ = self.target.get_credential_for_service('test-service') assert context.exception is not None assert context.exception.message == 'All credentials are being used by service: test-service.' def test_get_credentials_available_for_service_when_occupied_for_other_service( self): credential_service1 = self.target.get_credential_for_service( 'test-service-1') credential_service2 = self.target.get_credential_for_service( 'test-service-2') assert credential_service1 is not None assert credential_service2 is not None assert credential_service1.id == credential_service2.id def test_get_credentials_available_for_service_when_other_service_saturated( self): _ = self.target.get_credential_for_service('test-service-1') _ = self.target.get_credential_for_service('test-service-1') credential = self.target.get_credential_for_service('test-service-2') assert credential is not None def test_unlock_credentials_ok(self): credential = self.target.get_credential_for_service('test-service') self.target.unlock_credential(credential.id, 'test-service') def test_unlock_non_used_credential_raises_exception(self): with self.assertRaises(CredentialCurrentlyAvailableError) as context: self.target.unlock_credential('some-id', 'test-service') assert context.exception is not None assert context.exception.message == 'Credential with key some-id-test-service is currently available.' def test_get_unlock_get_again_ok(self): credential = self.target.get_credential_for_service('test-service') first_id = credential.id self.target.unlock_credential(credential.id, 'test-service') credential = self.target.get_credential_for_service('test-service') assert credential.id == first_id def test_get_all_credentials_returns_all(self): credentials = self.target.get_all_credentials_for_service( 'test-service') assert len(credentials) == 2 def test_get_all_credentials_already_in_use_raises_exception(self): self.target.in_use.add('fg-test-service') with self.assertRaises(CredentialsAlreadyInUseError) as context: _ = self.target.get_all_credentials_for_service('test-service') assert context.exception is not None assert context.exception.message == 'Service test-service is already using its credentials.' def test_get_all_credentials_not_blocking_credentials_on_exception(self): self.target.in_use.add('fg-test-service') with self.assertRaises(CredentialsAlreadyInUseError): _ = self.target.get_all_credentials_for_service('test-service') assert len(self.target.in_use) == 1 def test_get_all_credentials_blocks_credentials_for_service(self): _ = self.target.get_all_credentials_for_service('test-service') with self.assertRaises(NoAvailableCredentialsError) as context: _ = self.target.get_credential_for_service('test-service') assert context.exception is not None assert context.exception.message == 'All credentials are being used by service: test-service.'
def setUp(self) -> None: super(TestCredentialService, self).setUp() path = f"{abspath(join(dirname(__file__), '../..'))}/resources/test_credentials.json" CredentialService.CREDENTIALS_PATH = path self.target = CredentialService()