Skip to content

Commit

Permalink
Merge pull request #21 from Volumental/tech/exception-serialization
Browse files Browse the repository at this point in the history
Catch exceptions from exception serialization
  • Loading branch information
simonhugosson authored Sep 4, 2023
2 parents 8952249 + 1204cd3 commit 56d7ba2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ __pycache__/

# test app
db.sqlite3

.idea/
19 changes: 13 additions & 6 deletions django_leek/helpers.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import pickle
import base64
import pickle

from . import models


def unpack(pickled_task):
def unpack(pickled_task: bytes):
new_task = pickle.loads(base64.b64decode(pickled_task))
return new_task


def serialize(task):
return base64.b64encode(pickle.dumps(task))
def serialize(obj) -> bytes:
return base64.b64encode(pickle.dumps(obj))


def serialize_exception(e: Exception) -> bytes:
try:
return serialize(e)
except Exception:
return serialize(Exception("Failed to serialize exception: %s" % e))


def load_task(task_id) -> models.Task:
def load_task(task_id: int) -> models.Task:
return models.Task.objects.get(pk=task_id)


def save_task_to_db(new_task, pool_name):
def save_task_to_db(new_task, pool_name: str) -> models.Task:
pickled_task = serialize(new_task)
t = models.Task(pickled_task=pickled_task, pool=pool_name)
t.save()
Expand Down
2 changes: 1 addition & 1 deletion django_leek/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def target(queue):
except Exception as e:
log.exception("...task failed")
task.finished_at = timezone.now()
task.pickled_exception = helpers.serialize(e)
task.pickled_exception = helpers.serialize_exception(e)
task.save()


Expand Down
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

setup(
name='django-leek',
version='1.0.4',
version='1.0.5',
packages=find_packages(exclude=['test_app']),
install_requires=['django>=1.11'],
install_requires=['django>=3.2'],
include_package_data=True,
license='MIT License',
description='A simple Django app to offload tasks from main web server',
Expand All @@ -25,12 +25,12 @@
classifiers=[
'Environment :: Web Environment',
'Framework :: Django',
'Framework :: Django :: 1.11',
'Framework :: Django :: 3.2',
'Intended Audience :: Developers',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.10',
'Topic :: Internet :: WWW/HTTP',
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
],
Expand Down

0 comments on commit 56d7ba2

Please sign in to comment.