指摘修正・prepostexec追加

This commit is contained in:
nik.n 2024-02-21 16:48:12 +09:00
parent 22d8ee4465
commit 9400a7f5ff
4 changed files with 37 additions and 10 deletions

View File

@ -3,4 +3,5 @@ DB_PORT=3306
DB_USERNAME=************
DB_PASSWORD=************
DB_SCHEMA=src05
DUMP_FILE_S3_PATH=*******************
DUMP_FILE_S3_PATH=*******************
LOG_LEVEL=INFO

View File

@ -2,7 +2,7 @@
## 概要
ダンプ復元スクリプト
当処理は特定の機能で利用するものではなく、共通処理として要件に応じて実行することを想定している。
## 環境情報

View File

@ -4,16 +4,20 @@ import os
import subprocess
import textwrap
### from src.logging.get_logger import get_logger
from src.logging.get_logger import get_logger
from src.system_var import constants, environment
### logger = get_logger('ダンプ復元スクリプト')
logger = get_logger('ダンプ復元スクリプト')
def exec():
try:
### logger.info('ダンプ復元スクリプト:開始')
logger.info('ダンプ復元スクリプト:開始')
# 事前処理(共通処理としては空振りする)
_pre_exec()
#メイン処理
# MySQL接続情報を作成する
my_cnf_file_content = f"""
[client]
@ -37,7 +41,7 @@ def exec():
s3_cp_process = subprocess.Popen(['aws', 's3', 'cp', s3_file_path, './dump.gz'], stderr=subprocess.PIPE)
_, error = s3_cp_process.communicate()
if s3_cp_process.returncode != 0:
### logger.error(f'`aws s3 cp`実行時にエラーが発生しました。{"" if error is None else error.decode("utf-8")}')
logger.error(f'`aws s3 cp`実行時にエラーが発生しました。{"" if error is None else error.decode("utf-8")}')
return constants.BATCH_EXIT_CODE_SUCCESS
# S3コマンドの標準エラーはクローズしておく
s3_cp_process.stderr.close()
@ -54,11 +58,33 @@ def exec():
_, error = mysql_process.communicate()
if mysql_process.returncode != 0:
### logger.error(f'コマンド実行時にエラーが発生しました。{"" if error is None else error.decode("utf-8")}')
logger.error(f'コマンド実行時にエラーが発生しました。{"" if error is None else error.decode("utf-8")}')
return constants.BATCH_EXIT_CODE_SUCCESS
### logger.info('[NOTICE]ダンプ復元スクリプト:終了(正常終了)')
# 事後処理(共通処理としては空振りする)
_post_exec()
logger.info('[NOTICE]ダンプ復元スクリプト:終了(正常終了)')
return constants.BATCH_EXIT_CODE_SUCCESS
except Exception as e:
### logger.exception(f'ダンプ復元スクリプト中に想定外のエラーが発生しました :{e}')
logger.exception(f'ダンプ復元スクリプト中に想定外のエラーが発生しました :{e}')
return constants.BATCH_EXIT_CODE_SUCCESS
def _pre_exec():
"""
ダンプ復元 事前処理
共通機能としては事前処理を実装しない
事前処理が必要なダンプ復元処理を実装する場合当ロジックをコピーする
"""
print('pre')
pass
def _post_exec():
"""
ダンプ復元 事後処理
共通機能としては事後処理を実装しない
事後処理が必要なダンプ復元処理を実装する場合当ロジックをコピーする
"""
print('post')
pass

View File

@ -9,4 +9,4 @@ DB_SCHEMA = os.environ['DB_SCHEMA']
# AWS
DUMP_FILE_S3_PATH = os.environ['DUMP_FILE_S3_PATH']
LOG_LEVEL = os.environ['LOG_LEVEL']