Skip to content

Commit

Permalink
Merge pull request #51126 from msteed/utc-jid
Browse files Browse the repository at this point in the history
Add `utc_jid` option for generating jids based on utc
  • Loading branch information
garethgreenaway committed Jan 10, 2019
2 parents 87835dc + ff15986 commit f1d0963
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
3 changes: 3 additions & 0 deletions salt/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,9 @@ def _gather_buffer_space():
# Tell the client to display the jid when a job is published
'show_jid': bool,

# Generate jids based on UTC time instead of local time
'utc_jid': bool,

# Ensure that a generated jid is always unique. If this is set, the jid
# format is different due to an underscore and process id being appended
# to the jid. WARNING: A change to the jid format may break external
Expand Down
7 changes: 5 additions & 2 deletions salt/utils/jid.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ def gen_jid(opts=None):
opts = {}
global LAST_JID_DATETIME # pylint: disable=global-statement

if opts.get('utc_jid', False):
jid_dt = datetime.datetime.utcnow()
else:
jid_dt = datetime.datetime.now()
if not opts.get('unique_jid', False):
return '{0:%Y%m%d%H%M%S%f}'.format(datetime.datetime.now())
jid_dt = datetime.datetime.now()
return '{0:%Y%m%d%H%M%S%f}'.format(jid_dt)
if LAST_JID_DATETIME and LAST_JID_DATETIME >= jid_dt:
jid_dt = LAST_JID_DATETIME + datetime.timedelta(microseconds=1)
LAST_JID_DATETIME = jid_dt
Expand Down
27 changes: 26 additions & 1 deletion tests/unit/utils/test_jid.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,38 @@ def test_is_jid(self):

@skipIf(NO_MOCK, NO_MOCK_REASON)
def test_gen_jid(self):
now = datetime.datetime(2002, 12, 25, 12, 00, 00, 00)
now = datetime.datetime(2002, 12, 25, 12, 0, 0, 0)
with patch('datetime.datetime'):
datetime.datetime.now.return_value = now
ret = salt.utils.jid.gen_jid({})
self.assertEqual(ret, '20021225120000000000')

@skipIf(NO_MOCK, NO_MOCK_REASON)
def test_gen_jid_unique(self):
now = datetime.datetime(2002, 12, 25, 12, 0, 0, 0)
with patch('datetime.datetime'):
datetime.datetime.now.return_value = now
salt.utils.jid.LAST_JID_DATETIME = None
ret = salt.utils.jid.gen_jid({'unique_jid': True})
self.assertEqual(ret, '20021225120000000000_{0}'.format(os.getpid()))
ret = salt.utils.jid.gen_jid({'unique_jid': True})
self.assertEqual(ret, '20021225120000000001_{0}'.format(os.getpid()))

@skipIf(NO_MOCK, NO_MOCK_REASON)
def test_gen_jid_utc(self):
utcnow = datetime.datetime(2002, 12, 25, 12, 7, 0, 0)
with patch('datetime.datetime'):
datetime.datetime.utcnow.return_value = utcnow
ret = salt.utils.jid.gen_jid({'utc_jid': True})
self.assertEqual(ret, '20021225120700000000')

@skipIf(NO_MOCK, NO_MOCK_REASON)
def test_gen_jid_utc_unique(self):
utcnow = datetime.datetime(2002, 12, 25, 12, 7, 0, 0)
with patch('datetime.datetime'):
datetime.datetime.utcnow.return_value = utcnow
salt.utils.jid.LAST_JID_DATETIME = None
ret = salt.utils.jid.gen_jid({'utc_jid': True, 'unique_jid': True})
self.assertEqual(ret, '20021225120700000000_{0}'.format(os.getpid()))
ret = salt.utils.jid.gen_jid({'utc_jid': True, 'unique_jid': True})
self.assertEqual(ret, '20021225120700000001_{0}'.format(os.getpid()))

0 comments on commit f1d0963

Please sign in to comment.