Replace deprecated datetime.utcnow()

The datetime.utcnow() is deprecated in Python 3.12.
Replace datetime.utcnow() with oslo_utils.timeutils.utcnow().
This bumps oslo.utils to 7.0.0.

Change-Id: I472068637f34af072662bf6d97fe370661df814e
Signed-off-by: Takashi Natsume <takanattie@gmail.com>
This commit is contained in:
Takashi Natsume 2024-10-03 20:51:06 +09:00
parent 183a63b725
commit 51d358436f
14 changed files with 49 additions and 47 deletions

View File

@ -19,6 +19,7 @@ from oslo_config import cfg
from oslo_log import log as logging
from oslo_service import periodic_task
from oslo_service import threadgroup
from oslo_utils import timeutils
from mistral import context as auth_ctx
from mistral.db.v2 import api as db_api
@ -118,8 +119,7 @@ def run_execution_expiration_policy(self, ctx):
LOG.debug("Starting expiration policy.")
older_than = CONF.execution_expiration_policy.older_than
exp_time = (datetime.datetime.utcnow()
- datetime.timedelta(minutes=older_than))
exp_time = timeutils.utcnow() - datetime.timedelta(minutes=older_than)
batch_size = CONF.execution_expiration_policy.batch_size
max_executions = CONF.execution_expiration_policy.max_finished_executions

View File

@ -12,13 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
import json
from oslo_config import cfg
from oslo_log import log as logging
from oslo_service import periodic_task
from oslo_service import threadgroup
from oslo_utils import timeutils
from mistral import context as auth_ctx
from mistral.db.v2 import api as db_api_v2
@ -125,7 +125,7 @@ def advance_cron_trigger(t):
# we use the max of the current time or the next scheduled time.
next_time = triggers.get_next_execution_time(
t.pattern,
max(datetime.datetime.utcnow(), t.next_execution_time)
max(timeutils.utcnow(), t.next_execution_time)
)
# Update the cron trigger with next execution details

View File

@ -17,6 +17,7 @@ import datetime
import json
from oslo_log import log as logging
from oslo_utils import timeutils
from mistral.db.v2 import api as db_api
from mistral.engine import utils as eng_utils
@ -39,8 +40,7 @@ def get_next_execution_time(pattern, start_time):
def get_next_cron_triggers():
return db_api.get_next_cron_triggers(
datetime.datetime.utcnow() + datetime.timedelta(0, 2)
)
timeutils.utcnow() + datetime.timedelta(0, 2))
def validate_cron_trigger_input(pattern, first_time, count):
@ -50,7 +50,7 @@ def validate_cron_trigger_input(pattern, first_time, count):
)
if first_time:
valid_min_time = datetime.datetime.utcnow() + datetime.timedelta(0, 60)
valid_min_time = timeutils.utcnow() + datetime.timedelta(0, 60)
if valid_min_time > first_time:
raise exc.InvalidModelException(
@ -74,7 +74,7 @@ def create_cron_trigger(name, workflow_name, workflow_input,
workflow_params=None, pattern=None, first_time=None,
count=None, start_time=None, workflow_id=None):
if not start_time:
start_time = datetime.datetime.utcnow()
start_time = timeutils.utcnow()
if isinstance(first_time, str):
try:

View File

@ -11,6 +11,7 @@
# limitations under the License.
import datetime
from oslo_utils import timeutils
from oslo_utils import uuidutils
import pecan
@ -36,7 +37,7 @@ PKI_TOKEN_VERIFIED = {
'roles': [{'id': uuidutils.generate_uuid(dashed=False),
'name': 'admin'}],
'expires_at': datetime.datetime.isoformat(
datetime.datetime.utcnow() + datetime.timedelta(seconds=60)
timeutils.utcnow() + datetime.timedelta(seconds=60)
),
'project': {
'domain': {'id': 'default', 'name': 'Default'},

View File

@ -17,6 +17,8 @@ import datetime
import json
from unittest import mock
from oslo_utils import timeutils
from oslo_utils import uuidutils
import sqlalchemy as sa
from mistral.api.controllers.v2 import resources
@ -24,7 +26,6 @@ from mistral.db.v2 import api as db_api
from mistral.db.v2.sqlalchemy import models as db
from mistral import exceptions as exc
from mistral.tests.unit.api import base
from oslo_utils import uuidutils
DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S.%f'
@ -67,8 +68,8 @@ ENVIRONMENT = {
'variables': VARIABLES,
'scope': 'private',
'project_id': '<default-project>',
'created_at': str(datetime.datetime.utcnow()),
'updated_at': str(datetime.datetime.utcnow())
'created_at': str(timeutils.utcnow()),
'updated_at': str(timeutils.utcnow())
}
ENVIRONMENT_WITH_ILLEGAL_FIELD = {

View File

@ -15,6 +15,7 @@ import datetime
from unittest import mock
from oslo_config import cfg
from oslo_utils import timeutils
from mistral import context as auth_ctx
from mistral.db.v2 import api as db_api
@ -123,7 +124,7 @@ class ProcessCronTriggerTest(base.EngineTestCase):
next_trigger = next_triggers[0]
next_execution_time_before = next_trigger.next_execution_time
ts_before = datetime.datetime.utcnow()
ts_before = timeutils.utcnow()
periodic.process_cron_triggers_v2(None, None)
self._await(
@ -157,7 +158,7 @@ class ProcessCronTriggerTest(base.EngineTestCase):
# Make the first_time 1 sec later than current time, in order to make
# it executed by next cron-trigger task.
first_time = datetime.datetime.utcnow() + datetime.timedelta(0, 1)
first_time = timeutils.utcnow() + datetime.timedelta(0, 1)
# Creates a cron-trigger with pattern and first time, ensure the
# cron-trigger can be executed more than once, and cron-trigger will
@ -200,7 +201,7 @@ class ProcessCronTriggerTest(base.EngineTestCase):
def test_validate_cron_trigger_input_first_time(self):
cfg.CONF.set_default('auth_enable', False, group='pecan')
first_time = datetime.datetime.utcnow() + datetime.timedelta(0, 1)
first_time = timeutils.utcnow() + datetime.timedelta(0, 1)
self.assertRaises(
exc.InvalidModelException,

View File

@ -18,6 +18,7 @@ from unittest import mock
from oslo_config import cfg
from oslo_messaging.rpc import client as rpc_client
from oslo_utils import timeutils
from oslo_utils import uuidutils
from mistral.db.v2 import api as db_api
@ -74,8 +75,8 @@ ENVIRONMENT = {
'key2': 123
},
'scope': 'private',
'created_at': str(datetime.datetime.utcnow()),
'updated_at': str(datetime.datetime.utcnow())
'created_at': str(timeutils.utcnow()),
'updated_at': str(timeutils.utcnow())
}
ENVIRONMENT_DB = models.Environment(

View File

@ -12,10 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime as dt
from unittest import mock
from oslo_config import cfg
from oslo_utils import timeutils
import requests
from mistral.db.v2 import api as db_api
@ -186,7 +186,7 @@ class TaskDefaultsReverseWorkflowEngineTest(base.EngineTestCase):
wf_service.create_workflows(wf_text)
time_before = dt.datetime.utcnow()
time_before = timeutils.utcnow()
# Start workflow.
wf_ex = self.engine.start_workflow('wf', task_name='task1')
@ -195,7 +195,7 @@ class TaskDefaultsReverseWorkflowEngineTest(base.EngineTestCase):
# Workflow must work at least 2 seconds (1+1).
self.assertGreater(
(dt.datetime.utcnow() - time_before).total_seconds(),
(timeutils.utcnow() - time_before).total_seconds(),
2
)

View File

@ -12,14 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
from unittest import mock
from eventlet import event
from eventlet import semaphore
from eventlet import timeout
from unittest import mock
import datetime
from oslo_config import cfg
from oslo_utils import timeutils
from mistral.db.v2 import api as db_api
from mistral.scheduler import base as scheduler_base
@ -124,9 +124,7 @@ class DefaultSchedulerTest(base.DbTestCase):
self.assertIsNotNone(captured_at)
self.assertTrue(
datetime.datetime.utcnow() - captured_at <
datetime.timedelta(seconds=3)
)
timeutils.utcnow() - captured_at < datetime.timedelta(seconds=3))
self._unlock_target_method()
self._wait_target_method_end()
@ -144,7 +142,7 @@ class DefaultSchedulerTest(base.DbTestCase):
self.override_config('pickup_job_after', 1, 'scheduler')
# 1. Create a scheduled job in Job Store.
execute_at = datetime.datetime.utcnow() + datetime.timedelta(seconds=1)
execute_at = timeutils.utcnow() + datetime.timedelta(seconds=1)
db_api.create_scheduled_job({
'run_after': 1,
@ -176,17 +174,17 @@ class DefaultSchedulerTest(base.DbTestCase):
# 1. Create a scheduled job in Job Store marked as captured in one
# second in the future. It can be captured again only after 3
# seconds after that according to the config option.
captured_at = datetime.datetime.utcnow() + datetime.timedelta(
captured_at = timeutils.utcnow() + datetime.timedelta(
seconds=1
)
before_ts = datetime.datetime.utcnow()
before_ts = timeutils.utcnow()
db_api.create_scheduled_job({
'run_after': 1,
'func_name': TARGET_METHOD_PATH,
'func_args': {'name': 'task', 'id': '321'},
'execute_at': datetime.datetime.utcnow(),
'execute_at': timeutils.utcnow(),
'captured_at': captured_at,
'auth_ctx': {}
})
@ -203,6 +201,4 @@ class DefaultSchedulerTest(base.DbTestCase):
# At least 3 seconds should have passed.
self.assertTrue(
datetime.datetime.utcnow() - before_ts >=
datetime.timedelta(seconds=3)
)
timeutils.utcnow() - before_ts >= datetime.timedelta(seconds=3))

View File

@ -16,6 +16,9 @@
import datetime
from oslo_config import cfg
from oslo_utils import timeutils
from mistral import context as ctx
from mistral.db.v2 import api as db_api
from mistral.services import expiration_policy
@ -23,7 +26,6 @@ from mistral.services.expiration_policy import ExecutionExpirationPolicy
from mistral.tests.unit import base
from mistral.tests.unit.base import get_context
from mistral_lib import utils
from oslo_config import cfg
def _create_workflow_executions():
@ -130,7 +132,7 @@ class ExpirationPolicyTest(base.DbTestCase):
_create_workflow_executions()
now = datetime.datetime.utcnow()
now = timeutils.utcnow()
# This execution has a parent wf and testing that we are
# querying only for parent wfs.
@ -171,7 +173,7 @@ class ExpirationPolicyTest(base.DbTestCase):
def test_expiration_policy_for_executions_with_ignored_states(self):
_create_workflow_executions()
now = datetime.datetime.utcnow()
now = timeutils.utcnow()
_set_expiration_policy_config(
evaluation_interval=1,
@ -215,7 +217,7 @@ class ExpirationPolicyTest(base.DbTestCase):
"""
_create_workflow_executions()
now = datetime.datetime.utcnow()
now = timeutils.utcnow()
_set_expiration_policy_config(
evaluation_interval=1,
@ -240,7 +242,7 @@ class ExpirationPolicyTest(base.DbTestCase):
"""
_create_workflow_executions()
now = datetime.datetime.utcnow()
now = timeutils.utcnow()
_set_expiration_policy_config(
evaluation_interval=1,

View File

@ -20,6 +20,7 @@ from unittest import mock
from eventlet import queue
from eventlet import timeout
from oslo_config import cfg
from oslo_utils import timeutils
from mistral import context as auth_context
from mistral.db.v2 import api as db_api
@ -39,7 +40,7 @@ DELAY = 1.5
def get_time_delay(delay=DELAY * 2):
return datetime.datetime.utcnow() + datetime.timedelta(seconds=delay)
return timeutils.utcnow() + datetime.timedelta(seconds=delay)
def target_method():

View File

@ -17,6 +17,7 @@ import eventlet
from unittest import mock
from oslo_config import cfg
from oslo_utils import timeutils
from mistral import exceptions as exc
from mistral.rpc import clients as rpc
@ -296,7 +297,7 @@ class TriggerServiceV2Test(base.DbTestCase):
self.wf.name,
{},
pattern='*/3 * * * *',
start_time=datetime.datetime.utcnow() + datetime.timedelta(0, 50)
start_time=timeutils.utcnow() + datetime.timedelta(0, 50)
)
trigger_names = [t.name for t in t_s.get_next_cron_triggers()]

View File

@ -18,6 +18,7 @@ import json
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import timeutils
import osprofiler.profiler
import osprofiler.web
@ -39,17 +40,14 @@ def log_to_file(info, context=None):
th_local_name = '_profiler_trace_%s_start_time_' % info['trace_id']
if info['name'].endswith('-start'):
utils.set_thread_local(
th_local_name,
datetime.datetime.utcnow()
)
utils.set_thread_local(th_local_name, timeutils.utcnow())
# Insert a blank sequence for a trace start.
attrs.insert(1, ' ' * 8)
if info['name'].endswith('-stop'):
delta = (
datetime.datetime.utcnow() - utils.get_thread_local(th_local_name)
timeutils.utcnow() - utils.get_thread_local(th_local_name)
).total_seconds()
utils.set_thread_local(th_local_name, None)

View File

@ -17,7 +17,7 @@ oslo.i18n>=3.15.3 # Apache-2.0
oslo.messaging>=14.1.0 # Apache-2.0
oslo.middleware>=3.31.0 # Apache-2.0
oslo.policy>=3.6.0 # Apache-2.0
oslo.utils>=4.0.0 # Apache-2.0
oslo.utils>=7.0.0 # Apache-2.0
oslo.log>=3.36.0 # Apache-2.0
oslo.serialization>=2.21.1 # Apache-2.0
oslo.service>=2.1.0 # Apache-2.0