-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #214 from tukcomCD2024/refact/task2class-B-#212
refact : task들을 class단위로 분리
- Loading branch information
Showing
8 changed files
with
143 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from celery import Celery | ||
from kombu import Queue | ||
|
||
from application.config.queue_config import QueueConfig | ||
from application.task.make_animation import MakeAnimation | ||
from application.task.save_capsule_skin import SaveCapsuleSkin | ||
from application.task.send_notification import SendNotification | ||
|
||
queue_config = QueueConfig() | ||
celery = Celery('application', | ||
broker=queue_config.get_queue_url(), | ||
include=['application.task']) | ||
|
||
celery.conf.result_expires = 300 | ||
celery.conf.task_queues = ( | ||
Queue('makeAnimation.queue'), | ||
Queue('saveCapsuleSkin.queue'), | ||
Queue('sendNotification.queue') | ||
) | ||
|
||
celery.register_task(MakeAnimation()) | ||
celery.register_task(SaveCapsuleSkin()) | ||
celery.register_task(SendNotification()) |
File renamed without changes.
41 changes: 41 additions & 0 deletions
41
backend/AnimatedDrawings/application/task/make_animation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import os | ||
import shutil | ||
from pathlib import Path | ||
|
||
import requests | ||
|
||
from application.config.s3_config import S3Config | ||
from application.s3.s3_connection import get_object_wrapper | ||
from application.task.base_task import LogErrorsTask | ||
from examples.annotations_to_animation import annotations_to_animation | ||
from examples.image_to_annotations import image_to_annotations | ||
|
||
|
||
class MakeAnimation(LogErrorsTask): | ||
name = 'make_animation' | ||
|
||
def __init__(self): | ||
self.s3_bucket_name = S3Config().s3_bucket_name | ||
|
||
def run(self, *args, **kwargs): | ||
img_bytes = requests.get(kwargs['input_data']['imageUrl']).content | ||
|
||
output_directory = 'capsuleSkin/' + kwargs['input_data']['memberId'] | ||
result = Path(output_directory) | ||
result.mkdir(exist_ok=True) | ||
|
||
image_to_annotations(img_bytes, result) | ||
annotations_to_animation(output_directory, | ||
kwargs['input_data']['motionName'], | ||
kwargs['input_data']['retarget']) | ||
|
||
with open(output_directory + '/video.gif', 'rb') as image: | ||
gif_bytes = bytearray(image.read()) | ||
|
||
output_wrapper = get_object_wrapper(self.s3_bucket_name, | ||
'%s/%s' % (output_directory, | ||
kwargs['filename'])) | ||
output_wrapper.put(gif_bytes) | ||
|
||
if os.path.exists(output_directory): | ||
shutil.rmtree(output_directory) |
29 changes: 29 additions & 0 deletions
29
backend/AnimatedDrawings/application/task/save_capsule_skin.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from sqlalchemy import create_engine | ||
from sqlalchemy.orm import Session | ||
|
||
from application.config.database_config import DatabaseConfig | ||
from application.task.base_task import LogErrorsTask | ||
from application.model.capsule_skin import CapsuleSkin | ||
from application.model.motion import Motion | ||
from application.model.retarget import Retarget | ||
|
||
|
||
class SaveCapsuleSkin(LogErrorsTask): | ||
name = 'save_capsule_skin' | ||
database_config = DatabaseConfig() | ||
engine = create_engine(database_config.get_database_url()) | ||
|
||
def run(self, *args, **kwargs): | ||
capsule_skin = CapsuleSkin(skin_name=kwargs['input_data']['skinName'], | ||
image_url='capsuleSkin/%s/%s' % ( | ||
kwargs['input_data']['memberId'], | ||
kwargs['filename']), | ||
motion_name=Motion( | ||
kwargs['input_data']['motionName']).name, | ||
retarget=Retarget( | ||
kwargs['input_data']['retarget']).name, | ||
member_id=kwargs['input_data']['memberId']) | ||
|
||
with Session(self.engine) as session: | ||
session.add(capsule_skin) | ||
session.commit() |
25 changes: 25 additions & 0 deletions
25
backend/AnimatedDrawings/application/task/send_notification.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import requests | ||
|
||
from application.task.base_task import LogErrorsTask | ||
|
||
|
||
class SendNotification(LogErrorsTask): | ||
name = 'send_notification' | ||
|
||
def __init__(self): | ||
self.title = '캡슐 스킨 생성이 완료되었습니다!' | ||
self.notification_server_url = 'https://notification.archive-timecapsule.kro.kr/api/notification/capsule_skin/send' | ||
|
||
def run(self, *args, **kwargs): | ||
request_data = { | ||
'memberId': kwargs['input_data']['memberId'], | ||
'skinName': kwargs['input_data']['skinName'], | ||
'title': self.title, | ||
'text': f"{kwargs['input_data']['skinName']}이 생성되었습니다. ARchive에서 확인해보세요!", | ||
'skinUrl': kwargs['filename'] | ||
} | ||
|
||
requests.post(self.notification_server_url, | ||
json=request_data, | ||
verify=False, | ||
timeout=5) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters