172 lines
6.5 KiB
Python
Executable File
172 lines
6.5 KiB
Python
Executable File
import socket
|
|
import time
|
|
import boto3
|
|
import subprocess
|
|
import json
|
|
import os
|
|
import pandas as pd
|
|
import threading
|
|
from django.core.management.base import BaseCommand
|
|
from django.core.management import call_command
|
|
from mnfapp.models import ScriptTranslations
|
|
from MNF.settings import BasePath
|
|
from conversion.translation.external_conversion import Conversion
|
|
from scriptAudit.views import run_audit_in_counter
|
|
from django.core.files.base import ContentFile
|
|
from django.contrib.auth import get_user_model
|
|
from users.models import UserCredentialsForBlockchain
|
|
from django.core.files.base import File as DjangoFile
|
|
import tempfile
|
|
from MNF import settings
|
|
from io import BytesIO
|
|
from auto_email.views import sendmail
|
|
from lpp.certificate.createCertificate import certificateGenrate
|
|
|
|
|
|
basePath = BasePath()
|
|
|
|
User = get_user_model()
|
|
|
|
def background_execution(obj):
|
|
obj.convert()
|
|
|
|
|
|
|
|
def run_conversion(msg):
|
|
body_dict = json.loads(msg.body)
|
|
msg.delete()
|
|
translated_script = ScriptTranslations.objects.get(translation_id=body_dict["translation_id"])
|
|
object_key = "INPUT/" + (translated_script.script_link_id.script.name.split("/"))[-1]
|
|
local_file_path = "/home/ubuntu/Conversion_Kitchen_Code/kitchen_counter/media/" + translated_script.script_link_id.script.name
|
|
s3_client = boto3.client('s3',
|
|
aws_access_key_id="AKIAQVLBBGCB45RMLKVW",
|
|
aws_secret_access_key="ZWc6KOc5LuBLuCEBDDfQTor+Q7rp3fFH74gVt+AA",
|
|
region_name="ap-south-1"
|
|
)
|
|
s3_client.download_file("conversion-kitchen", object_key, local_file_path)
|
|
|
|
audit_user = User.objects.get(id=1)
|
|
conversion_params = {
|
|
"audit_user": audit_user,
|
|
"user": translated_script.user_id,
|
|
"file_path": str(basePath) + "/media/" + translated_script.script_link_id.script.name,
|
|
"original_script_id": translated_script.script_link_id.script_id,
|
|
"translated_script_id": translated_script.translation_id,
|
|
"sample_id": body_dict.get('sample_id', None),
|
|
"existing_script": body_dict.get('existing_script', None),
|
|
"iteration_no": body_dict.get('iteration_no', None),
|
|
"juggernaut_pages_deduction": body_dict.get("juggernaut_pages_deduction", None),
|
|
"language_set": body_dict.get("language_set", None),
|
|
"amount_without_subscrption": body_dict.get("amount_without_subscrption", None)
|
|
}
|
|
print("reached here")
|
|
obj = Conversion(**conversion_params)
|
|
# obj.convert()
|
|
background_thread = threading.Thread(target=background_execution, args=(obj,))
|
|
background_thread.start()
|
|
background_thread.join()
|
|
# s3_client.delete_object(Bucket='conversion-kitchen', Key=object_key)
|
|
|
|
|
|
def run_audit(msg):
|
|
body_dict = json.loads(msg.body)
|
|
msg.delete()
|
|
user = body_dict.get("user")
|
|
s3_url = body_dict.get("s3-file-path")
|
|
screenplay_name = body_dict.get("screenplay_name")
|
|
author = body_dict.get("author")
|
|
language = body_dict.get("language")
|
|
script_ext = body_dict.get("script_ext")
|
|
script_file_name = body_dict.get("script_file_name")
|
|
language = "en"
|
|
print("112")
|
|
object_key = "INPUT/" + str(script_file_name)
|
|
local_file_path = "/home/ubuntu/Conversion_Kitchen_Code/kitchen_counter/media/audit_counter_files/" + script_file_name
|
|
s3_client = boto3.client('s3',
|
|
aws_access_key_id="AKIAQVLBBGCB45RMLKVW",
|
|
aws_secret_access_key="ZWc6KOc5LuBLuCEBDDfQTor+Q7rp3fFH74gVt+AA",
|
|
region_name="ap-south-1"
|
|
)
|
|
print(object_key, local_file_path)
|
|
s3_client.download_file("conversion-kitchen", object_key, local_file_path)
|
|
audit_parameters = {
|
|
"service_type" : "audit",
|
|
"user":user,
|
|
"s3-file-path": local_file_path,
|
|
"screenplay_name": screenplay_name,
|
|
"author": author,
|
|
"language": language,
|
|
"script_ext": script_ext,
|
|
"script_file_name": script_file_name
|
|
}
|
|
run_audit_in_counter(audit_parameters)
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Custom Command to start django server and then start dequeuing from SQS'
|
|
|
|
def handle(self, *args, **options):
|
|
# Call the original runserver command"
|
|
# call_command('runserver', '0.0.0.0:4549')
|
|
command = 'python manage.py runserver 0.0.0.0:4549 > /home/ubuntu/Conversion_Kitchen_Code/logfile.log 2>&1'
|
|
|
|
# # Execute the command using subprocess.Popen
|
|
subprocess.Popen(command, shell=True)
|
|
|
|
# Wait for the server to start
|
|
while True:
|
|
try:
|
|
# Try to connect to the server
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
s.connect(('0.0.0.0', 4549))
|
|
break
|
|
except ConnectionRefusedError:
|
|
# If connection is refused, wait and try again
|
|
time.sleep(1)
|
|
|
|
# Once the server is up, run your function
|
|
self.run_after_server_startup()
|
|
|
|
def run_after_server_startup(self):
|
|
# Your function to run after the server starts
|
|
print("Development server is fully up and running! Run your function here.")
|
|
|
|
session = boto3.Session(
|
|
aws_access_key_id='AKIAQVLBBGCB45RMLKVW', # replace with your key
|
|
aws_secret_access_key='ZWc6KOc5LuBLuCEBDDfQTor+Q7rp3fFH74gVt+AA', # replace with your key
|
|
)
|
|
sqs = session.resource('sqs', region_name='ap-south-1')
|
|
|
|
print("Started Executin this from conversion")
|
|
queue = sqs.get_queue_by_name(QueueName="mnfqueue")
|
|
# try:
|
|
while True:
|
|
|
|
messages = queue.receive_messages(
|
|
MessageAttributeNames=["All"],
|
|
MaxNumberOfMessages=1,
|
|
WaitTimeSeconds=5,
|
|
)
|
|
if len(messages) > 0:
|
|
|
|
for msg in messages:
|
|
try:
|
|
print("Received message: %s: %s", msg.message_id, msg.body)
|
|
print(type(msg.body))
|
|
body_dict = json.loads(msg.body)
|
|
if body_dict['service_type'] == "conversion":
|
|
|
|
run_conversion(msg)
|
|
|
|
elif body_dict['service_type'] == "audit":
|
|
|
|
run_audit(msg)
|
|
|
|
except Exception as error:
|
|
print("error execution from queue: %s", error)
|
|
else:
|
|
break
|
|
|
|
print("Completed All Execution")
|