Skip to content

Commit

Permalink
Non-JSON serializable args and kwargs deprecated
Browse files Browse the repository at this point in the history
The use of non-JSON serializable `args` and `kwargs` is now deprecated.

closes #8505
  • Loading branch information
bmbouter authored and mdellweg committed Apr 6, 2021
1 parent 3543838 commit 8f0095d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES/plugin_api/8505.deprecation
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The usage of non-JSON serializable types of ``args`` and ``kwargs`` to tasks is deprecated. Future
releases of pulpcore may discontinue accepting complex argument types. Note, UUID objects are not
JSON serializable. A deprecated warning is logged if a non-JSON serializable is used.
19 changes: 19 additions & 0 deletions pulpcore/tasking/tasks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging
import time
import uuid
Expand All @@ -10,6 +11,7 @@
from rq import Queue
from rq.job import Job, get_current_job

from pulpcore.app.logging import deprecation_logger
from pulpcore.app.models import (
ReservedResource,
ReservedResourceRecord,
Expand Down Expand Up @@ -143,6 +145,21 @@ def _queue_reserved_task(func, inner_task_id, resources, inner_args, inner_kwarg
task_status.set_failed(e, None)


class NonJSONWarningEncoder(json.JSONEncoder):
def default(self, obj):
try:
return json.JSONEncoder.default(self, obj)
except TypeError:
deprecation_logger.warn(
_(
"The argument {obj} is of type {type}, which is not JSON serializable. The use "
"of non JSON serializable objects for `args` and `kwargs` to tasks is "
"deprecated as of pulpcore==3.12. See the traceback below for more info."
).format(obj=obj, type=type(obj))
)
return None


def enqueue_with_reservation(
func, resources, args=None, kwargs=None, options=None, task_group=None
):
Expand Down Expand Up @@ -196,6 +213,8 @@ def as_url(r):
redis_conn = connection.get_redis_connection()
current_job = get_current_job(connection=redis_conn)
parent_kwarg = {}
json.dumps(args, cls=NonJSONWarningEncoder)
json.dumps(kwargs, cls=NonJSONWarningEncoder)
if current_job:
# set the parent task of the spawned task to the current task ID (same as rq Job ID)
parent_kwarg["parent_task"] = Task.objects.get(pk=current_job.id)
Expand Down

0 comments on commit 8f0095d

Please sign in to comment.