[~] Refactor
This commit is contained in:
parent
8b4d78ac53
commit
639e755dbd
@ -1,15 +1,19 @@
|
|||||||
from .config import tiktok_config
|
from .config import tiktok_config
|
||||||
|
from .utils import kombu_register_json_dataclass
|
||||||
|
|
||||||
import celery
|
import celery
|
||||||
import redis
|
import redis
|
||||||
|
|
||||||
c = tiktok_config()
|
c = tiktok_config()
|
||||||
|
|
||||||
|
kombu_register_json_dataclass()
|
||||||
|
|
||||||
app = celery.Celery(
|
app = celery.Celery(
|
||||||
__name__,
|
__name__,
|
||||||
broker=c.celery_broker,
|
broker=c.celery_broker,
|
||||||
result_backend=c.celery_result_backend,
|
result_backend=c.celery_result_backend,
|
||||||
accept_content=['pickle'],
|
accept_content=['json-dataclass', 'json',],
|
||||||
|
task_serializer='json-dataclass',
|
||||||
)
|
)
|
||||||
|
|
||||||
app.autodiscover_tasks(c.celery_imports)
|
app.autodiscover_tasks(c.celery_imports)
|
||||||
|
@ -1,16 +1,14 @@
|
|||||||
import celery.app.task
|
import celery.app.task
|
||||||
import importlib
|
import importlib
|
||||||
import kombu.serialization
|
|
||||||
import kombu.utils.json
|
import kombu.utils.json
|
||||||
from typing import (
|
from typing import (
|
||||||
Any,
|
Any,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class Task(celery.app.task.Task):
|
class Task(celery.app.task.Task):
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
res = super().__call__(*args, **kwargs)
|
res = super().__call__(*args, **kwargs)
|
||||||
return self._to_native(res)
|
return self._dumps(res)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _loads(self, data_str: str) -> Any:
|
def _loads(self, data_str: str) -> Any:
|
||||||
@ -42,10 +40,12 @@ class Task(celery.app.task.Task):
|
|||||||
else:
|
else:
|
||||||
return kombu.utils.json.dumps(data)
|
return kombu.utils.json.dumps(data)
|
||||||
|
|
||||||
kombu.serialization.register(
|
def kombu_register_json_dataclass():
|
||||||
|
import kombu.serialization
|
||||||
|
kombu.serialization.register(
|
||||||
'json-dataclass',
|
'json-dataclass',
|
||||||
Task._dumps,
|
Task._dumps,
|
||||||
Task._loads,
|
Task._loads,
|
||||||
content_type='application/json',
|
content_type='application/json',
|
||||||
content_encoding='text',
|
content_encoding='text',
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user