Skip to content

Commit

Permalink
(BSR)[API] feat: public api: add @atomic to products module
Browse files Browse the repository at this point in the history
Add @atomic to routes and some nested functions called by them.
  • Loading branch information
jbaudet-pass committed Dec 17, 2024
1 parent 12733e3 commit 396df39
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 92 deletions.
5 changes: 4 additions & 1 deletion api/src/pcapi/core/bookings/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,10 @@ def _cancel_booking(
technical_message_id="booking.cancelled",
)
batch.track_booking_cancellation(booking)
external_bookings_api.send_booking_notification_to_external_service(booking, BookingAction.CANCEL)

on_commit(
partial(external_bookings_api.send_booking_notification_to_external_service, booking, BookingAction.CANCEL)
)

update_external_user(booking.user)
update_external_pro(booking.venue.bookingEmail)
Expand Down
5 changes: 4 additions & 1 deletion api/src/pcapi/core/external/batch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
from functools import partial
import logging
import textwrap

Expand All @@ -9,6 +10,7 @@
import pcapi.core.fraud.models as fraud_models
import pcapi.core.offers.models as offers_models
import pcapi.notifications.push as push_notifications
from pcapi.repository import on_commit
from pcapi.tasks import batch_tasks


Expand Down Expand Up @@ -167,7 +169,8 @@ def track_booking_cancellation(booking: bookings_models.Booking) -> None:
"offer_price": booking.total_amount,
}
payload = batch_tasks.TrackBatchEventRequest(event_name=event_name, event_payload=event_payload, user_id=user.id)
batch_tasks.track_event_task.delay(payload)

on_commit(partial(batch_tasks.track_event_task.delay, payload))


def format_offer_attributes(offer: offers_models.Offer) -> dict:
Expand Down
39 changes: 20 additions & 19 deletions api/src/pcapi/core/offers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,10 @@ def update_offer(
withdrawal_updated = updates_set & withdrawal_fields
oa_updated = "offererAddress" in updates
if should_send_mail and (withdrawal_updated or oa_updated):
transactional_mails.send_email_for_each_ongoing_booking(offer)
on_commit(partial(transactional_mails.send_email_for_each_ongoing_booking, offer))

reason = search.IndexationReason.OFFER_UPDATE

search.async_index_offer_ids([offer.id], reason=reason, log_extra={"changes": updates_set})

return offer
Expand Down Expand Up @@ -774,10 +775,8 @@ def create_stock(
offer.lastValidationPrice = price
repository.add_to_session(created_stock, *created_activation_codes, offer)
db.session.flush()
search.async_index_offer_ids(
[offer.id],
reason=search.IndexationReason.STOCK_CREATION,
)

search.async_index_offer_ids([offer.id], reason=search.IndexationReason.STOCK_CREATION)

return created_stock

Expand Down Expand Up @@ -865,6 +864,7 @@ def edit_stock(
finance_api.update_finance_event_pricing_date(stock)

repository.add_to_session(stock)

search.async_index_offer_ids(
[stock.offerId],
reason=search.IndexationReason.STOCK_UPDATE,
Expand Down Expand Up @@ -925,10 +925,7 @@ def publish_offer(
else:
if offer.publicationDate:
offers_repository.delete_future_offer(offer.id)
search.async_index_offer_ids(
[offer.id],
reason=search.IndexationReason.OFFER_PUBLICATION,
)
search.async_index_offer_ids([offer.id], reason=search.IndexationReason.OFFER_PUBLICATION)
logger.info(
"Offer has been published",
extra={"offer_id": offer.id, "venue_id": offer.venueId, "offer_status": offer.status},
Expand Down Expand Up @@ -962,7 +959,7 @@ def update_offer_fraud_information(offer: AnyOffer, user: users_models.User | No
and not venue_already_has_validated_offer
and isinstance(offer, models.Offer)
):
transactional_mails.send_first_venue_approved_offer_email_to_pro(offer)
on_commit(partial(transactional_mails.send_first_venue_approved_offer_email_to_pro, offer))


def _invalidate_bookings(bookings: list[bookings_models.Booking]) -> list[bookings_models.Booking]:
Expand All @@ -985,10 +982,17 @@ def _delete_stock(stock: models.Stock, author_id: int | None = None, user_connec
)
if cancelled_bookings:
for booking in cancelled_bookings:
transactional_mails.send_booking_cancellation_by_pro_to_beneficiary_email(booking)
transactional_mails.send_booking_cancellation_confirmation_by_pro_email(cancelled_bookings)
on_commit(partial(transactional_mails.send_booking_cancellation_by_pro_to_beneficiary_email, booking))

on_commit(partial(transactional_mails.send_booking_cancellation_confirmation_by_pro_email, cancelled_bookings))
if not FeatureToggle.WIP_DISABLE_CANCEL_BOOKING_NOTIFICATION.is_active():
push_notification_job.send_cancel_booking_notification.delay([booking.id for booking in cancelled_bookings])
on_commit(
partial(
push_notification_job.send_cancel_booking_notification.delay,
[booking.id for booking in cancelled_bookings],
)
)

search.async_index_offer_ids(
[stock.offerId],
reason=search.IndexationReason.STOCK_DELETION,
Expand Down Expand Up @@ -1046,10 +1050,7 @@ def create_mediation(
)
_delete_mediations_and_thumbs(previous_mediations)

search.async_index_offer_ids(
[offer.id],
reason=search.IndexationReason.MEDIATION_CREATION,
)
search.async_index_offer_ids([offer.id], reason=search.IndexationReason.MEDIATION_CREATION)

return mediation

Expand Down Expand Up @@ -1321,10 +1322,10 @@ def set_offer_status_based_on_fraud_criteria(offer: AnyOffer) -> models.OfferVal
status = models.OfferValidationStatus.PENDING
offer.flaggingValidationRules = flagging_rules
if isinstance(offer, models.Offer):
compliance.update_offer_compliance_score(offer, is_primary=True)
on_commit(partial(compliance.update_offer_compliance_score, offer, is_primary=True))
else:
if isinstance(offer, models.Offer):
compliance.update_offer_compliance_score(offer, is_primary=False)
on_commit(partial(compliance.update_offer_compliance_score, offer, is_primary=False))

logger.info("Computed offer validation", extra={"offer": offer.id, "status": status.value})
return status
Expand Down
26 changes: 18 additions & 8 deletions api/src/pcapi/core/search/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import abc
import datetime
import enum
from functools import partial
import logging
import typing

Expand All @@ -19,6 +20,7 @@
from pcapi.core.search.backends import base
from pcapi.models import db
from pcapi.models.feature import FeatureToggle
from pcapi.repository import on_commit
from pcapi.utils import requests
from pcapi.utils.module_loading import import_string

Expand Down Expand Up @@ -132,14 +134,22 @@ def async_index_offer_ids(
This function returns quickly. The "real" reindexation will be
done later through a cron job.
"""
_log_async_request("offers", offer_ids, reason, log_extra)
backend = _get_backend()
try:
backend.enqueue_offer_ids(offer_ids)
except Exception: # pylint: disable=broad-except
if not settings.CATCH_INDEXATION_EXCEPTIONS:
raise
logger.exception("Could not enqueue offer ids to index", extra={"offers": offer_ids})

def enqueue(
offer_ids: abc.Collection[int],
reason: IndexationReason,
log_extra: dict | None = None,
) -> None:
_log_async_request("offers", offer_ids, reason, log_extra)
backend = _get_backend()
try:
backend.enqueue_offer_ids(offer_ids)
except Exception: # pylint: disable=broad-except
if not settings.CATCH_INDEXATION_EXCEPTIONS:
raise
logger.exception("Could not enqueue offer ids to index", extra={"offers": offer_ids})

on_commit(partial(enqueue, offer_ids, reason, log_extra))


def async_index_collective_offer_template_ids(
Expand Down
Loading

0 comments on commit 396df39

Please sign in to comment.