Skip to content

Commit f5bd996

Browse files
committed
fix logging and emails
1 parent fced96b commit f5bd996

6 files changed

Lines changed: 44 additions & 9 deletions

File tree

notifications/file_event_notifications.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def perform(self):
275275
'user_fullname': self.user.fullname,
276276
'message': self.html_message,
277277
'profile_image_url': self.profile_image_url,
278-
'localized_timestamp': self.timestamp,
278+
'localized_timestamp': str(self.timestamp),
279279
'url': self.url,
280280
},
281281
is_digest=True,
@@ -312,7 +312,7 @@ def perform(self):
312312
'user_fullname': self.user.fullname,
313313
'message': self.html_message,
314314
'profile_image_url': self.profile_image_url,
315-
'localized_timestamp': self.timestamp,
315+
'localized_timestamp': str(self.timestamp),
316316
'url': self.url,
317317
},
318318
is_digest=True,
@@ -332,7 +332,7 @@ def perform(self):
332332
'user_fullname': self.user.fullname,
333333
'message': self.html_message,
334334
'profile_image_url': self.profile_image_url,
335-
'localized_timestamp': self.timestamp,
335+
'localized_timestamp': str(self.timestamp),
336336
'url': self.url,
337337
},
338338
is_digest=True,

osf_tests/test_archiver.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
ARCHIVER_INITIATED,
1717
)
1818
from website.archiver import utils as archiver_utils
19+
from website.archiver import tasks as archiver_tasks
1920
from website.app import * # noqa: F403
2021
from website.archiver import listeners
2122
from website.archiver.tasks import * # noqa: F403
@@ -458,6 +459,15 @@ def test_stat_addon(self):
458459
assert res.target_name == 'osfstorage'
459460
assert res.disk_usage == 128 + 256
460461

462+
def test_compact_traceback_uses_last_lines(self):
463+
traceback_text = '\n'.join(f'line {line_num}' for line_num in range(50))
464+
compact = archiver_tasks._compact_traceback(traceback_text, max_lines=5, max_chars=1000)
465+
466+
assert compact == '\n'.join(f'line {line_num}' for line_num in range(45, 50))
467+
468+
def test_compact_traceback_handles_empty(self):
469+
assert archiver_tasks._compact_traceback(None) is None
470+
461471
@mock.patch('website.archiver.tasks.archive_addon.delay')
462472
def test_archive_node_pass(self, mock_archive_addon):
463473
settings.MAX_ARCHIVE_SIZE = 1024 ** 3
@@ -959,8 +969,7 @@ def test_archive_callback_done_errors(self):
959969
assert call_args[1] == self.src
960970
assert call_args[2] == self.dst
961971
assert call_args[3] == self.user
962-
assert call_args[3] == self.user
963-
assert list(call_args[4]) == list(self.dst.archive_job.target_addons.all())
972+
assert call_args[4] == self.dst.archive_job.target_info()
964973

965974
def test_archive_callback_updates_archiving_state_when_done(self):
966975
proj = factories.NodeFactory()

tests/test_events.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ def test_user_performing_action_no_email(self):
450450
self.event.perform()
451451
assert len(notifications['emits']) == 1
452452
assert notifications['emits'][0]['type'] == NotificationTypeEnum.ADDON_FILE_COPIED
453+
assert isinstance(notifications['emits'][0]['kwargs']['event_context']['localized_timestamp'], str)
453454

454455

455456
class TestSubscriptionManipulations(OsfTestCase):

website/archiver/listeners.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def archive_callback(dst):
5353
root.registered_from,
5454
root,
5555
root.registered_user,
56-
dst.archive_job.target_addons.all(),
56+
dst.archive_job.target_info(),
5757
)
5858

5959

website/archiver/tasks.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def create_app_context():
5959

6060
logger = get_task_logger(__name__)
6161

62+
6263
class ArchiverSizeExceeded(Exception):
6364

6465
def __init__(self, result):
@@ -88,12 +89,13 @@ class ArchiverTask(celery.Task):
8889

8990
def on_failure(self, exc, task_id, args, kwargs, einfo):
9091
job = ArchiveJob.load(kwargs.get('job_pk'))
92+
compact_traceback = utils.compact_traceback(einfo)
9193
if not job:
9294
archiver_state_exc = ArchiverStateError({
9395
'exception': exc,
9496
'args': args,
9597
'kwargs': kwargs,
96-
'einfo': einfo,
98+
'einfo': compact_traceback,
9799
})
98100
sentry.log_exception(archiver_state_exc)
99101
raise archiver_state_exc
@@ -102,7 +104,7 @@ def on_failure(self, exc, task_id, args, kwargs, einfo):
102104
# already captured
103105
return
104106

105-
src, dst, user = job.info()
107+
src, dst, _ = job.info()
106108
errors = []
107109
if isinstance(exc, ArchiverSizeExceeded):
108110
dst.archive_status = ARCHIVER_SIZE_EXCEEDED
@@ -122,15 +124,23 @@ def on_failure(self, exc, task_id, args, kwargs, einfo):
122124
}
123125
else:
124126
dst.archive_status = ARCHIVER_UNCAUGHT_ERROR
125-
errors = [str(einfo)] if einfo else []
127+
errors = [f'{exc.__class__.__name__}: {exc}']
128+
if compact_traceback:
129+
errors.append(f'Traceback tail:\n{compact_traceback}')
126130
dst.save()
127131

132+
# Always capture full exception; keep log_message payload compact.
133+
sentry.log_exception(exc)
128134
sentry.log_message(
129135
f'An error occurred while archiving node: {src._id} and registration: {dst._id}',
130136
extra_data={
131137
'source node guid': src._id,
132138
'registration node guid': dst._id,
133139
'task_id': task_id,
140+
'task_name': self.name,
141+
'exception_type': exc.__class__.__name__,
142+
'exception_message': str(exc),
143+
'traceback_tail': compact_traceback,
134144
'errors': errors,
135145
},
136146
)

website/archiver/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,3 +384,18 @@ def _validate_updated_responses(registration, file_input_qids, updated_responses
384384
registration=registration,
385385
missing_files=missing_responses
386386
)
387+
388+
389+
def compact_traceback(einfo, max_lines=25, max_chars=4000):
390+
"""Return a short traceback tail for logs/notifications."""
391+
if not einfo:
392+
return None
393+
traceback_text = str(einfo)
394+
if not traceback_text:
395+
return None
396+
397+
lines = traceback_text.splitlines()
398+
compact = '\n'.join(lines[-max_lines:])
399+
if len(compact) > max_chars:
400+
compact = compact[-max_chars:]
401+
return compact

0 commit comments

Comments
 (0)