예제 #1
0
def batch_deleting_expired_offers_in_algolia(client: Redis,
                                             process_all_expired: bool = False
                                             ) -> None:
    page = 0
    has_still_offers = True
    one_day_before_now = datetime.utcnow() - timedelta(days=1)
    two_days_before_now = datetime.utcnow() - timedelta(days=2)
    arbitrary_oldest_date = datetime(2000, 1, 1)
    from_date = two_days_before_now if not process_all_expired else arbitrary_oldest_date

    while has_still_offers:
        expired_offer_ids_as_tuple = offer_queries.get_paginated_offer_ids_given_booking_limit_datetime_interval(
            limit=settings.ALGOLIA_DELETING_OFFERS_CHUNK_SIZE,
            page=page,
            from_date=from_date,
            to_date=one_day_before_now,
        )
        expired_offer_ids_as_int = from_tuple_to_int(
            offer_ids=expired_offer_ids_as_tuple)

        if len(expired_offer_ids_as_int) > 0:
            logger.info(
                "[ALGOLIA] processing deletion of expired offers from page %s...",
                page)
            delete_expired_offers(client=client,
                                  offer_ids=expired_offer_ids_as_int)
            logger.info("[ALGOLIA] expired offers from page %s processed!",
                        page)
        else:
            has_still_offers = False
            logger.info("[ALGOLIA] deleting expired offers finished!")
        page += 1
예제 #2
0
    def test_should_not_delete_expired_offers_from_algolia_when_no_offer_id(
            self, mock_delete_objects, mock_delete_indexed_offers, app):
        # Given
        client = MagicMock()

        # When
        delete_expired_offers(client=client, offer_ids=[])

        # Then
        assert mock_delete_objects.call_count == 0
        assert mock_delete_indexed_offers.call_count == 0
예제 #3
0
    def test_should_not_delete_expired_offers_from_algolia_when_at_least_one_offer_id_but_offers_were_not_indexed(
            self, mock_delete_objects, mock_check_offer_exists,
            mock_delete_indexed_offers, app):
        # Given
        client = MagicMock()
        mock_check_offer_exists.side_effect = [False, False, False]

        # When
        delete_expired_offers(client=client, offer_ids=[])

        # Then
        assert mock_delete_objects.call_count == 0
        assert mock_delete_indexed_offers.call_count == 0
예제 #4
0
    def test_should_delete_expired_offers_from_algolia_when_at_least_one_offer_id_and_offers_were_indexed(
            self, mock_delete_objects, mock_check_offer_exists,
            mock_delete_indexed_offers, app):
        # Given
        client = MagicMock()
        mock_check_offer_exists.side_effect = [True, True, True]

        # When
        delete_expired_offers(client=client, offer_ids=[1, 2, 3])

        # Then
        assert mock_delete_objects.call_count == 1
        assert mock_delete_objects.call_args_list == [
            call(object_ids=["AE", "A9", "AM"])
        ]
        assert mock_delete_indexed_offers.call_count == 1
        assert mock_delete_indexed_offers.call_args_list == [
            call(client=client, offer_ids=[1, 2, 3])
        ]
예제 #5
0
def deactivate_offers(offers: List[Offer]):
    for offer in offers:
        offer.isActive = False
    repository.save(*offers)
    offer_ids = [offer.id for offer in offers]
    delete_expired_offers(app.redis_client, offer_ids)