Skip to content

Commit 7a8ae26

Browse files
committed
LITE-30582 Increase backoff factor between tasks generation
1 parent 3843454 commit 7a8ae26

File tree

5 files changed

+58
-7
lines changed

5 files changed

+58
-7
lines changed

connect_bi_reporter/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# Delay in seconds for schedule to process Upload task
1010
SECONDS_DELAY = 120
1111
# Backoff factor in seconds between Upload tasks creation
12-
SECONDS_BACKOFF_FACTOR = 10
12+
SECONDS_BACKOFF_FACTOR = 120
1313
CREATE_UPLOADS_METHOD_NAME = 'create_uploads'
1414
PROCESS_UPLOADS_METHOD_NAME = 'process_upload'
1515
PROCESS_UPLOAD_TAKS_BASE_METHOD_PAYLOAD = {

connect_bi_reporter/scheduler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from datetime import datetime, timedelta
2-
from logging import Logger
32
import enum
3+
from logging import Logger
44
from typing import Any, Dict, Optional
55

66
from connect.client import ClientError, ConnectClient

connect_bi_reporter/uploads/services.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime, timedelta
2+
import copy
23

34
from sqlalchemy import util
45
from connect.client import ClientError
@@ -161,7 +162,7 @@ def create_uploads(db, client, logger, feeds):
161162

162163

163164
def get_process_upload_task_payload(installation_id, upload_id, account_id):
164-
payload = PROCESS_UPLOAD_TAKS_BASE_METHOD_PAYLOAD
165+
payload = copy.deepcopy(PROCESS_UPLOAD_TAKS_BASE_METHOD_PAYLOAD)
165166
payload.update({'name': f'Process Uploads - {account_id}'})
166167
parameters = {
167168
'installation_id': installation_id,

connect_bi_reporter/uploads/tasks.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@ def process_upload(self, schedule):
9090
if not upload:
9191
return ScheduledExecutionResponse.fail(output=f'Invalid upload `{upload_id}`.')
9292

93-
if upload.status != 'pending':
93+
if upload.status != Upload.STATUSES.pending:
9494
return ScheduledExecutionResponse.fail(
9595
output=f'Cannot process upload in status `{upload.status}`.',
9696
)
9797

98-
upload.status = 'processing'
98+
upload.status = Upload.STATUSES.processing
9999
db.add(upload)
100100
db.commit()
101101

@@ -112,14 +112,14 @@ def process_upload(self, schedule):
112112
)
113113
upload.size = uploaded_file_props.get('size', 0)
114114

115-
upload.status = 'uploaded'
115+
upload.status = Upload.STATUSES.uploaded
116116
upload.name = file_name
117117
db.add(upload)
118118
db.commit()
119119
return ScheduledExecutionResponse.done()
120120
except Exception:
121121
self.logger.exception(msg='Error processing upload')
122-
upload.status = 'failed'
122+
upload.status = Upload.STATUSES.failed
123123
db.add(upload)
124124
db.commit()
125125
return ScheduledExecutionResponse.fail()

tests/uploads/test_tasks.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from datetime import datetime, timedelta, timezone
12
import re
23
from unittest.mock import call
34

@@ -7,6 +8,7 @@
78
from connect.eaas.core.inject.models import Context
89
from sqlalchemy.exc import DBAPIError
910

11+
from connect_bi_reporter.constants import SECONDS_BACKOFF_FACTOR, SECONDS_DELAY
1012
from connect_bi_reporter.events import ConnectBiReporterEventsApplication
1113

1214

@@ -229,6 +231,11 @@ def test_create_upload_schedule_task(
229231
),
230232
)
231233
ext.get_installation_admin_client = lambda self: connect_client
234+
235+
_now = datetime(2024, 10, 15, 10, 0, 0, tzinfo=timezone.utc)
236+
p_datetime = mocker.patch('connect_bi_reporter.uploads.services.datetime')
237+
p_datetime.utcnow = lambda: _now
238+
232239
mocker.patch(
233240
'connect_bi_reporter.uploads.tasks.get_extension_owner_client',
234241
return_value=connect_client,
@@ -245,6 +252,9 @@ def test_create_upload_schedule_task(
245252
'connect_bi_reporter.scheduler.create_schedule_task',
246253
return_value=eaas_schedule_task,
247254
)
255+
p_get_task_payload = mocker.patch(
256+
'connect_bi_reporter.scheduler.EaasScheduleTask.get_task_payload',
257+
)
248258
feed1 = feed_factory(
249259
schedule_id=report_schedule['id'],
250260
account_id=installation['owner']['id'],
@@ -274,6 +284,44 @@ def test_create_upload_schedule_task(
274284
),
275285
],
276286
)
287+
delay = SECONDS_DELAY
288+
new_delay = SECONDS_DELAY + SECONDS_BACKOFF_FACTOR
289+
p_get_task_payload.assert_has_calls(
290+
[
291+
call(
292+
trigger_type='onetime',
293+
trigger_data={
294+
'date': (_now + timedelta(seconds=delay)).isoformat(),
295+
},
296+
method_payload={
297+
'method': 'process_upload',
298+
'description': 'This task will download the report from'
299+
' connect and published it in the respective storage.',
300+
'parameter': {
301+
'installation_id': 'EIN-8436-7221-8308',
302+
'upload_id': f'ULF-{feed1.id.split("-", 1)[-1]}-000',
303+
},
304+
'name': 'Process Uploads - PA-000-000',
305+
},
306+
),
307+
call(
308+
trigger_type='onetime',
309+
trigger_data={
310+
'date': (_now + timedelta(seconds=new_delay)).isoformat(),
311+
},
312+
method_payload={
313+
'method': 'process_upload',
314+
'description': 'This task will download the report from'
315+
' connect and published it in the respective storage.',
316+
'parameter': {
317+
'installation_id': 'EIN-8436-7221-8308',
318+
'upload_id': f'ULF-{feed2.id.split("-", 1)[-1]}-000',
319+
},
320+
'name': 'Process Uploads - PA-000-000',
321+
},
322+
),
323+
],
324+
)
277325
for idx, zipped in enumerate(zip(uploads, [feed1, feed2])):
278326
upload, feed = zipped
279327
assert result.status == 'success'
@@ -298,6 +346,8 @@ def test_create_upload_schedule_task(
298346
f' created for Upload `{uploads[1].id}`: '
299347
f'Will process Report File `{report_file[1]["id"]}`'
300348
)
349+
assert delay == 120
350+
assert new_delay == 240
301351

302352

303353
def test_create_upload_schedule_task_no_feeds(

0 commit comments

Comments
 (0)