Skip to content

Commit

Permalink
resolve : #4 #5 (#6)
Browse files Browse the repository at this point in the history
resolve : #5
fix : 버전 관리 수정
fix : 작업 목록 생성 병목 현상 수정

Signed-off-by: DongChuelKim <terrinens@gmail.com>
  • Loading branch information
terrinens authored Sep 14, 2024
1 parent 1d0c19f commit 4710d9e
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 65 deletions.
20 changes: 10 additions & 10 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from logger.log import create_logger
from manager.file_manager import file_manager
from manager.runner_manager import Manager, ready
from manager.runner_manager import Manager, is_ready, task_count
from manager.service_manager import registration

app = Flask(__name__)
Expand Down Expand Up @@ -41,11 +41,11 @@ def jar_upload():

if result:
response = jsonify({
'message': '업로드가 완료되었습니다. 작업이 진행중 입니다.',
'message': 'Upload has been completed. Work is in progress.',
'polling': f'/tasking?uuid={uuid}'
}), 202
else:
response = jsonify({'message': '업로드에 실패했습니다.'}), 400
response = jsonify({'message': 'Upload failed.'}), 400

return response

Expand All @@ -63,19 +63,19 @@ def tasking():

if is_tasking:
return jsonify({
'message': f'작업은 진행중입니다. 대기번호 : {waiting}',
'message': f'Work is in progress. waiting number : {waiting}',
'polling': f'{request.path}?uuid={uuid}'
}), 202
else:
return jsonify({'message': '해당 작업은 완료되었습니다.'}), 200
return jsonify({'message': 'That work has been completed.'}), 200


@app.route('/ready', methods=['GET'])
def ready():
if ready:
return jsonify({'message': '대기중인 작업이 없습니다.'}), 200
if is_ready():
return jsonify({'message': 'There are no pending tasks.'}), 200
else:
return jsonify({'message': f'대기중인 작업의 수 {len(manager.task_list)}'}), 202
return jsonify({'message': f'Number of pending tasks {task_count()}'}), 202


def add_parse(parse: argparse.ArgumentParser):
Expand Down Expand Up @@ -107,7 +107,7 @@ def add_parse(parse: argparse.ArgumentParser):
if debug:
log = create_logger('UEC_log', 'uec.log', console_level=debug)

manager = Manager(target_dir=save_dir, server_port=backend_port, debug=debug).start()
manager = Manager(target_dir=save_dir, server_port=backend_port, debug=debug, maintenance_count=10).start()

log.info(f"The server has started. Port : {port}")
app.run(port=port, debug=debug)
app.run(host='0.0.0.0', port=port, debug=debug)
80 changes: 54 additions & 26 deletions manager/file_manager.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import hashlib
import os
import re
import time
import uuid
from os.path import join as opj
from uuid import uuid4

from werkzeug.datastructures import FileStorage

Expand All @@ -10,56 +12,82 @@
log = create_logger('FM_Log', 'uec.log')


def custom_sort(string):
def version_sort(string):
match = re.search(r'v(\d+)', string)
return int(match.group(1)) if match else float('-inf')


def _version_manager(save_dir, file: FileStorage):
def __version_manager(save_dir, file: FileStorage):
save_path = opj(save_dir, file.filename)

true_name = file.filename[:file.filename.rfind('.')]

file_list = os.listdir(save_dir)
last_same_files = list(filter(lambda x: true_name in x, file_list))
last_same_files.sort(key=custom_sort, reverse=True)
last_same_files.sort(key=version_sort, reverse=True)

if os.path.exists(save_path) and os.path.isfile(save_path):
if last_same_files:
log.debug('An existing file exists. Change the file name.')
log.debug('The most recent version of the file was found. Change to the corresponding version or higher.')

if last_same_files:
log.debug('The most recent version of the file was found. Change to the corresponding version or higher.')

last_same_file = last_same_files[0]

if match := re.search(r'v(\d+)', last_same_file):
next_number = int(match[1]) + 1
new_name = true_name + f' v{next_number}' + '.' + file.filename.split('.')[-1]

else:
new_name = true_name + ' v1' + '.' + file.filename.split('.')[-1]

log.info(f'The version change has been completed. file name : {new_name}')
return opj(save_dir, new_name)

elif len(last_same_files) > 0 and os.path.isfile(save_path):
last_same_file = last_same_files[0]

if match := re.search(r'v(\d+)', last_same_file):

next_number = int(match[1]) + 1
return true_name + f' v{next_number}' + '.' + file.filename.split('.')[-1]
new_name = true_name + f' v{next_number}' + '.' + file.filename.split('.')[-1]

else:
return save_path
new_name = true_name + ' v2' + '.' + file.filename.split('.')[-1]

log.info(f'The version change has been completed. new version name : {new_name}')
return opj(save_dir, new_name)
else:
return save_path


def __gen_random_uuid():
timestamp = str(int(time.time()))
random_hase = hashlib.sha224(os.urandom(4)).hexdigest()
name = f"{timestamp}{random_hase}"
return uuid.uuid5(uuid.NAMESPACE_DNS, name)


def file_manager(save_dir, jar: FileStorage):
save_path = _version_manager(save_dir, jar)
save_path = __version_manager(save_dir, jar)
try:
jar.save(str(save_path))
return uuid4(), True
return __gen_random_uuid(), True
except Exception as e:
log.error(f'An error occurred while saving the file. : {e}')
return None, False


def matching_files(target_dir, extension, ignores=None):
files = sorted(os.listdir(target_dir), key=version_sort, reverse=True)
match_files = []

for file in files:
is_file = os.path.isfile(os.path.join(target_dir, file))
is_eq_extension = file.endswith(extension)
is_ignore = (ignores is not None) and file in ignores

if is_file and is_eq_extension and not is_ignore:
match_files.append(file)

return sorted(match_files, key=version_sort)


def old_file_remove(target_dir, extension, maintenance_count, ignores=None):
matches = matching_files(target_dir, extension, ignores)
if len(matches) > maintenance_count:
matches.sort(key=version_sort)

remove_list = matches[:len(matches) - maintenance_count]

for path in remove_list:
try:
os.remove(os.path.join(target_dir, path))
except PermissionError:
pass

log.info(f'The following files were deleted: {remove_list}')
Loading

0 comments on commit 4710d9e

Please sign in to comment.