diff --git a/frappe/model/naming.py b/frappe/model/naming.py index 595cb28ef0..1eae1e2b45 100644 --- a/frappe/model/naming.py +++ b/frappe/model/naming.py @@ -14,6 +14,7 @@ import uuid_utils import frappe from frappe import _ from frappe.model import log_types +from frappe.monitor import get_trace_id from frappe.query_builder import DocType from frappe.utils import cint, cstr, now_datetime @@ -281,7 +282,7 @@ def make_autoname(key="", doctype="", doc="", *, ignore_validate=False): DE/09/01/00001 where 09 is the year, 01 is the month and 00001 is the series """ if key == "hash": - return _generate_random_string(10) + return (_get_timestamp_prefix() + _generate_random_string(7))[:10] series = NamingSeries(key) return series.generate_next_name(doc, ignore_validate=ignore_validate) @@ -291,7 +292,14 @@ def _get_timestamp_prefix(): ts = int(time.time() * 10) # time in deciseconds # we ~~don't need~~ can't get ordering over entire lifetime, so we wrap the time. ts = ts % (32**4) - return base64.b32hexencode(ts.to_bytes(length=5, byteorder="big")).decode()[-4:].lower() + ts_part = base64.b32hexencode(ts.to_bytes(length=5, byteorder="big")).decode()[-3:].lower() + + # First character is from request/job specific UUID, all documents created in this "session" will + # have same prefix. This avoids collision between parallel jobs with reasonable probabililistic + # guarantees. + request_part = (get_trace_id() or "")[-1:] + + return request_part + ts_part def _generate_random_string(length=10): diff --git a/frappe/monitor.py b/frappe/monitor.py index a02704f9e0..8b9179e6f9 100644 --- a/frappe/monitor.py +++ b/frappe/monitor.py @@ -83,7 +83,7 @@ class Monitor: self.data.job.scheduled = True if job := rq.get_current_job(): - self.data.uuid = job.id + self.data.job_id = job.id waitdiff = self.data.timestamp - job.enqueued_at.replace(tzinfo=datetime.timezone.utc) self.data.job.wait = int(waitdiff.total_seconds() * 1000000) diff --git a/frappe/tests/test_naming.py b/frappe/tests/test_naming.py index 21c24e34d0..b72aa2424f 100644 --- a/frappe/tests/test_naming.py +++ b/frappe/tests/test_naming.py @@ -2,7 +2,6 @@ # License: MIT. See LICENSE import time -import unittest from uuid import UUID import uuid_utils @@ -407,7 +406,6 @@ class TestNaming(IntegrationTestCase): expected_name = "TODO-" + nowdate().split("-")[1] + "-" + "0001" self.assertEqual(name, expected_name) - @unittest.skip("This is not supported anymore, see #28349.") @retry( retry=retry_if_exception_type(AssertionError), stop=stop_after_attempt(3),