Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugfix] | GH-1749 -Fixing share expiration task #1750

Merged
merged 5 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
from dataall.modules.datasets_base.db.dataset_repositories import DatasetBaseRepository
from dataall.modules.notifications.db.notification_models import Notification
from dataall.modules.shares_base.db.share_object_models import ShareObjectItem, ShareObject
from dataall.modules.shares_base.services.shares_enums import ShareItemHealthStatus, PrincipalType, ShareableType
from dataall.modules.shares_base.services.shares_enums import (
ShareItemHealthStatus,
PrincipalType,
ShareableType,
ShareObjectStatus,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -486,7 +491,13 @@ def fetch_submitted_shares_with_notifications(session):
def get_all_active_shares_with_expiration(session):
return (
session.query(ShareObject)
.filter(and_(ShareObject.expiryDate.isnot(None), ShareObject.deleted.is_(None)))
.filter(
and_(
ShareObject.expiryDate.isnot(None),
ShareObject.deleted.is_(None),
ShareObject.status == ShareObjectStatus.Processed.value,
)
)
.all()
)

Expand Down
23 changes: 11 additions & 12 deletions backend/dataall/modules/shares_base/tasks/share_expiration_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from dataall.modules.shares_base.db.share_state_machines_repositories import ShareStatusRepository
from dataall.modules.shares_base.services.share_notification_service import ShareNotificationService
from dataall.modules.datasets_base.db.dataset_repositories import DatasetBaseRepository
from dataall.modules.shares_base.services.shares_enums import ShareObjectActions
from dataall.modules.shares_base.services.shares_enums import ShareObjectActions, ShareItemStatus
from dataall.modules.shares_base.services.sharing_service import SharingService

log = logging.getLogger(__name__)
Expand All @@ -27,24 +27,23 @@ def share_expiration_checker(engine):
try:
if share.expiryDate.date() < datetime.today().date():
log.info(f'Revoking share with uri: {share.shareUri} as it is expired')
# Put all share items in revoke state and then revoke
# If a share is expired, pull all the share items which are in Share_Succeeded state
# Update status for each share item to Revoke_Approved and Revoke the share
dlpzx marked this conversation as resolved.
Show resolved Hide resolved
share_items_to_revoke = ShareObjectRepository.get_all_share_items_in_share(
session, share.shareUri, ['Share_Succeeded']
)
item_uris = [share_item.shareItemUri for share_item in share_items_to_revoke]
revoked_items_states = ShareStatusRepository.get_share_items_states(
session, share.shareUri, item_uris
session, share.shareUri, [ShareItemStatus.Share_Succeeded.value]
)

# If the share doesn't have any share items in Share_Succeeded state then skip this share
if len(share_items_to_revoke) == 0:
continue

share_sm = ShareObjectSM(share.status)
new_share_state = share_sm.run_transition(ShareObjectActions.RevokeItems.value)

for item_state in revoked_items_states:
item_sm = ShareItemSM(item_state)
for item in share_items_to_revoke:
item_sm = ShareItemSM(item.status)
new_state = item_sm.run_transition(ShareObjectActions.RevokeItems.value)
for item in share_items_to_revoke:
if item.status == item_state:
item_sm.update_state_single_item(session, item, new_state)
item_sm.update_state_single_item(session, item, new_state)

share_sm.update_state(session, share, new_share_state)
SharingService.revoke_share(engine=engine, share_uri=share.shareUri)
Expand Down
Loading