This repository has been archived by the owner on Aug 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
205 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import datetime | ||
|
||
from dateutil.relativedelta import relativedelta | ||
from google.appengine.api.taskqueue import Task | ||
|
||
from src.backup.datastore.Table import Table | ||
from src.commons.config.configuration import configuration | ||
from src.commons.tasks import Tasks | ||
|
||
|
||
class OrganizationRetention(object): | ||
QUERY_PAGE_SIZE = 2500 | ||
|
||
@classmethod | ||
def schedule_retention_tasks_starting_from_cursor(cls, table_cursor): | ||
results, next_cursor, more = Table.query() \ | ||
.filter(OrganizationRetention.__table_with_backup_predicate()) \ | ||
.order(Table.last_checked, Table.key) \ | ||
.fetch_page( | ||
page_size=cls.QUERY_PAGE_SIZE, | ||
start_cursor=table_cursor | ||
) | ||
tasks = [cls.__create_table_retention_task(result) | ||
for result in results] | ||
Tasks.schedule(queue_name='table-retention', tasks=tasks) | ||
if more and next_cursor: | ||
task = Task( | ||
method='GET', | ||
url='/cron/retention', | ||
params={ | ||
'cursor': next_cursor.urlsafe(), | ||
}) | ||
|
||
Tasks.schedule(queue_name='table-retention-scheduler', tasks=[task]) | ||
|
||
@staticmethod | ||
def __table_with_backup_predicate(): | ||
months_after_table_should_not_have_any_backups = \ | ||
configuration.grace_period_after_source_table_deletion_in_months + 1 | ||
|
||
age_threshold_datetime = datetime.datetime.today() - relativedelta( | ||
months=months_after_table_should_not_have_any_backups) | ||
|
||
return Table.last_checked >= age_threshold_datetime | ||
|
||
@staticmethod | ||
def __create_table_retention_task(table): | ||
params = {'projectId': table.project_id, | ||
'datasetId': table.dataset_id, | ||
'tableId': table.table_id, | ||
'tableKey': table.key.urlsafe()} | ||
if table.partition_id: | ||
params['partitionId'] = table.partition_id | ||
return Task( | ||
method='GET', | ||
url='/tasks/retention/table', | ||
params=params) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,11 @@ | ||
import webapp2 | ||
from google.appengine.api.taskqueue import Task | ||
from google.appengine.datastore.datastore_query import Cursor | ||
|
||
from src.commons.tasks import Tasks | ||
from src.backup.datastore.Table import Table | ||
from src.retention.organization_retention import OrganizationRetention | ||
|
||
|
||
class OrganizationRetentionHandler(webapp2.RequestHandler): | ||
|
||
QUERY_PAGE_SIZE = 2500 | ||
|
||
def get(self): | ||
cursor = Cursor(urlsafe=self.request.get('cursor')) | ||
self.__schedule_retention_starting_from_cursor(cursor) | ||
|
||
@classmethod | ||
def __schedule_retention_starting_from_cursor(cls, table_cursor): | ||
results, next_cursor, more = Table.query().fetch_page( | ||
page_size=cls.QUERY_PAGE_SIZE, | ||
start_cursor=table_cursor | ||
) | ||
tasks = [cls.__create_table_retention_task(result) | ||
for result in results] | ||
Tasks.schedule(queue_name='table-retention', tasks=tasks) | ||
if more and next_cursor: | ||
task = Task( | ||
method='GET', | ||
url='/cron/retention', | ||
params={ | ||
'cursor': next_cursor.urlsafe(), | ||
}) | ||
|
||
Tasks.schedule(queue_name='table-retention-scheduler', tasks=[task]) | ||
|
||
@staticmethod | ||
def __create_table_retention_task(table): | ||
params = {'projectId': table.project_id, | ||
'datasetId': table.dataset_id, | ||
'tableId': table.table_id, | ||
'tableKey': table.key.urlsafe()} | ||
if table.partition_id: | ||
params['partitionId'] = table.partition_id | ||
return Task( | ||
method='GET', | ||
url='/tasks/retention/table', | ||
params=params) | ||
OrganizationRetention.schedule_retention_tasks_starting_from_cursor(cursor) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.