Commit 9d453f1e authored by David Mendez's avatar David Mendez
Browse files

Use non-naive utcnow instead of now

parent 71fba523
......@@ -6,16 +6,19 @@ from datetime import datetime, timedelta
from app.config import RUN_CONFIG
from app.job_statistics import statistics_saver
from app import utils
class TestStatisticsSaver(unittest.TestCase):
"""
Class to test the statistics saver
"""
def test_generates_the_correct_json_for_a_job_record(self):
"""
Tests that it generates the correct json for a job record to be sent to elasticsearch
"""
current_time = datetime.now()
current_time = utils.get_utc_now()
dict_must_be = {
'job_type': 'TEST',
......@@ -49,7 +52,7 @@ class TestStatisticsSaver(unittest.TestCase):
)
record_date_field_name = RUN_CONFIG.get('job_statistics').get('date_field_name')
del job_record_dict_got[record_date_field_name] # to avoid complications with the current time calculation
del job_record_dict_got[record_date_field_name] # to avoid complications with the current time calculation
self.assertEqual(job_record_dict_got, dict_must_be, 'The job record dict was not generated correctly')
def test_generates_the_correct_json_for_a_job_cache_record(self):
......@@ -61,7 +64,7 @@ class TestStatisticsSaver(unittest.TestCase):
'job_type': 'TEST',
'run_env_type': 'DEV',
'was_cached': True,
'request_date': datetime.now().timestamp() * 1000
'request_date': utils.get_utc_now().timestamp() * 1000
}
job_cache_record_dict_got = statistics_saver.get_job_cache_record_dict(
......
......@@ -18,6 +18,7 @@ from app.blueprints.job_submission.services import job_submission_service
from app.job_status_daemon import locks
from app.job_statistics import statistics_saver
from app.job_status_daemon.job_statistics import statistics_generator
from app import utils
AGENT_RUN_DIR = RUN_CONFIG.get('status_agent_run_dir', str(Path().absolute()) + '/status_agents_run')
if not os.path.isabs(AGENT_RUN_DIR):
......@@ -90,7 +91,7 @@ def get_check_job_status_script_path(job_id, lsf_job_id):
:return: the path to use for creating the job status script
"""
filename = f'{datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")}_job_id-{job_id}_lsf_job_id-{lsf_job_id}' \
filename = f'{utils.get_utc_now().strftime("%Y-%m-%d-%H-%M-%S")}_job_id-{job_id}_lsf_job_id-{lsf_job_id}' \
f'_check_lsf_job_status.sh'
job_status_check_script_path = Path(AGENT_RUN_DIR).joinpath(socket.gethostname(), filename)
......
......@@ -12,6 +12,7 @@ from app import create_app
from app.models import delayed_job_models
from app.job_status_daemon import daemon
from app.job_status_daemon.job_statistics import statistics_generator
from app import utils
class TestJobStatisticsGeneration(unittest.TestCase):
"""
......@@ -35,7 +36,7 @@ class TestJobStatisticsGeneration(unittest.TestCase):
test that it calculates the time from created to running
"""
current_time = datetime.now()
current_time = utils.get_utc_now()
created_at = current_time
seconds_must_be = 1.5
started_at = created_at + timedelta(seconds=seconds_must_be)
......@@ -56,7 +57,7 @@ class TestJobStatisticsGeneration(unittest.TestCase):
test that it calculates the time from running to finished
"""
current_time = datetime.now()
current_time = utils.get_utc_now()
started_at = current_time
seconds_must_be = 1.5
finished_at = started_at + timedelta(seconds=seconds_must_be)
......
......@@ -152,7 +152,7 @@ class TestJobStatusDaemon(unittest.TestCase):
job_id = 'job_1'
lsf_job_id = 1
filename = f'{datetime.now().strftime("%Y-%m-%d-%H-%M-%S")}_job_id-{job_id}_lsf_job_id-{lsf_job_id}' \
filename = f'{utils.get_utc_now().strftime("%Y-%m-%d-%H-%M-%S")}_job_id-{job_id}_lsf_job_id-{lsf_job_id}' \
f'_check_lsf_job_status.sh'
job_status_check_script_path_must_be = Path(daemon.AGENT_RUN_DIR).joinpath(socket.gethostname(), filename)
print('job_status_check_script_path_must_be: ', job_status_check_script_path_must_be)
......
......@@ -15,6 +15,7 @@ from app.db import DB
from app.models import utils
from app.config import RUN_CONFIG
import app.app_logging as app_logging
from app import utils as app_utils
DAYS_TO_LIVE = 7 # Days for which the results are kept
......@@ -474,7 +475,7 @@ def update_job_progress(job_id, progress, status_log, status_description):
if status_log is not None:
if job.status_log is None:
job.status_log = ''
job.status_log += f'{datetime.datetime.now().isoformat()}: {status_log}\n'
job.status_log += f'{app_utils.get_utc_now().isoformat()}: {status_log}\n'
if status_description is not None:
job.status_description = status_description
......@@ -494,7 +495,7 @@ def append_to_lsf_check_log(job_id, checker_name, check_log):
if job.lsf_check_log is None:
job.lsf_check_log = ''
job.lsf_check_log += f'{datetime.datetime.now().isoformat()}-{checker_name}: {check_log}\n'
job.lsf_check_log += f'{app_utils.get_utc_now().isoformat()}-{checker_name}: {check_log}\n'
save_job(job)
......
......@@ -40,4 +40,4 @@ def get_utc_now():
"""
:return: the UTC current time with the timezone info
"""
return datetime.utcnow().replace(tzinfo=datetime.timezone.utc)
return datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc)
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