Commit 3160cd7c authored by Ilya Simonov's avatar Ilya Simonov

add logging for send video to s3

parent 17c1347f
import requests import requests
import logging
from django.conf import settings from django.conf import settings
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
...@@ -13,6 +14,8 @@ from cp_video.celery import app ...@@ -13,6 +14,8 @@ from cp_video.celery import app
video_content_type = ContentType.objects.get_for_model(Video) video_content_type = ContentType.objects.get_for_model(Video)
playlist_content_type = ContentType.objects.get_for_model(Playlist) playlist_content_type = ContentType.objects.get_for_model(Playlist)
log = logging.getLogger('send_video_to_s3')
@app.task(soft_time_limit=60) @app.task(soft_time_limit=60)
def send_video_to_s3(): def send_video_to_s3():
...@@ -21,9 +24,11 @@ def send_video_to_s3(): ...@@ -21,9 +24,11 @@ def send_video_to_s3():
).prefetch_related('tags', 'categories') ).prefetch_related('tags', 'categories')
for local_video in local_videos: for local_video in local_videos:
log.info(f'Uploading video {local_video.id} to S3...')
video_name = local_video.local_file.name.split('/')[-1] video_name = local_video.local_file.name.split('/')[-1]
key = asset_upload(local_video, video_name) key = asset_upload(local_video, video_name)
file_path = settings.DOMAIN_NAME + settings.MEDIA_URL + key file_path = settings.DOMAIN_NAME + settings.MEDIA_URL + key
log.info(f'File path: {file_path}')
response = requests.get(file_path) response = requests.get(file_path)
content = response.content content = response.content
...@@ -32,6 +37,7 @@ def send_video_to_s3(): ...@@ -32,6 +37,7 @@ def send_video_to_s3():
local_video.status = READY local_video.status = READY
if not local_video.thumbnail and local_video.local_file: if not local_video.thumbnail and local_video.local_file:
log.info(f'Uploading thumbnail for video {local_video.id} to S3...')
thumbnail_name = generate_thumbnail(file_path, video_name) thumbnail_name = generate_thumbnail(file_path, video_name)
key = f'videos/{thumbnail_name}' key = f'videos/{thumbnail_name}'
thumbnail_path = settings.DOMAIN_NAME + settings.MEDIA_URL + thumbnail_name thumbnail_path = settings.DOMAIN_NAME + settings.MEDIA_URL + thumbnail_name
...@@ -39,6 +45,7 @@ def send_video_to_s3(): ...@@ -39,6 +45,7 @@ def send_video_to_s3():
content = response.content content = response.content
content_type = 'image/jpeg' content_type = 'image/jpeg'
local_video.thumbnail = upload_file(content, key, content_type) local_video.thumbnail = upload_file(content, key, content_type)
log.info(f'Thumbnail for video {local_video.id} uploaded to S3...')
local_video.save(update_fields=['s3_file', 'status', 'thumbnail']) local_video.save(update_fields=['s3_file', 'status', 'thumbnail'])
......
...@@ -168,3 +168,44 @@ else: ...@@ -168,3 +168,44 @@ else:
AWS_S3_CUSTOM_DOMAIN = env('AWS_S3_CUSTOM_DOMAIN') AWS_S3_CUSTOM_DOMAIN = env('AWS_S3_CUSTOM_DOMAIN')
DOMAIN_NAME = env('DOMAIN_NAME', default='http://localhost:8000') DOMAIN_NAME = env('DOMAIN_NAME', default='http://localhost:8000')
# Logs
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'default': {
'format': "%(asctime)s:%(name)s:%(levelname)s:%(message)s"
},
'verbose': {
'format': '%(levelname)s %(asctime)s %(name)s %(message)s',
'datefmt': '%Y-%m-%d %H:%M:%S',
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'stream': sys.stdout,
'formatter': 'verbose',
},
},
'loggers': {
'django': {
'handlers': ['console'],
'level': 'INFO',
'propagate': True,
},
'django.request': {
'handlers': ['console'],
'level': 'ERROR',
'propagate': False,
},
'send_video_to_s3': {
'handlers': ['console'],
'level': 'INFO',
'propagate': True,
},
},
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment