Skeleton Code commit
This commit is contained in:
56
workspace/worker.py
Normal file
56
workspace/worker.py
Normal file
@@ -0,0 +1,56 @@
|
||||
import json
|
||||
import time
|
||||
|
||||
import convert_obj_to_md
|
||||
import redis
|
||||
|
||||
UPLOAD_DIR = "data"
|
||||
OUTPUT_DIR = "converted"
|
||||
|
||||
|
||||
redis_client = redis.StrictRedis(host="localhost", port=6379, decode_responses=True)
|
||||
|
||||
|
||||
def process_task(task):
|
||||
"""
|
||||
태스크 정보를 기반으로 파일 확장자에 맞는 변환 함수를 호출.
|
||||
"""
|
||||
file_ext = task.get("extension")
|
||||
input_path = task.get("input_path")
|
||||
output_path = task.get("output_path")
|
||||
|
||||
converters = {
|
||||
"txt": convert_obj_to_md.convert_txt_to_md,
|
||||
"hwp": convert_obj_to_md.convert_hwp_to_md,
|
||||
"docx": convert_obj_to_md.convert_docx_to_md,
|
||||
"pdf": convert_obj_to_md.convert_pdf_to_md,
|
||||
"ppt": convert_obj_to_md.convert_ppt_to_md,
|
||||
"excel": convert_obj_to_md.convert_excel_to_md,
|
||||
}
|
||||
converter = converters.get(file_ext)
|
||||
if converter:
|
||||
converter(input_path, output_path)
|
||||
else:
|
||||
print(f"지원하지 않는 파일 형식: {file_ext}")
|
||||
|
||||
|
||||
def worker():
|
||||
"""Redis 큐에서 태스크를 가져와 파일 변환 작업을 수행"""
|
||||
while True:
|
||||
# rpop: 큐의 오른쪽에서 태스크를 꺼냄
|
||||
task_json = redis_client.rpop("task_queue")
|
||||
if task_json:
|
||||
try:
|
||||
task = json.loads(task_json)
|
||||
print(f"작업 처리 중: {task}")
|
||||
process_task(task)
|
||||
except Exception as e:
|
||||
print(f"작업 처리 중 에러: {e}")
|
||||
else:
|
||||
print("큐에 작업이 없습니다. 5초 후 재시도...")
|
||||
time.sleep(5)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Redis 워커 시작!")
|
||||
worker()
|
||||
Reference in New Issue
Block a user