Skip to content

Commit 472595f

Browse files
author
Nathan Lee
committed
Removed unused constants and updated ctllog/ctx file writes to first write to temp file and switch to minimize file corruption during server issues.
1 parent 8e502b8 commit 472595f

3 files changed

Lines changed: 14 additions & 5 deletions

File tree

pyrunner/core/constants.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,4 @@
3434
HEADER_SHELL = '#{}\n#ID|PARENT_IDS|MAX_ATTEMPTS|RETRY_WAIT_TIME|PROCESS_NAME|SHELL_COMMAND|LOGFILE'.format(MODE_SHELL)
3535
HEADER_PYTHON = '#{}\n#ID|PARENT_IDS|MAX_ATTEMPTS|RETRY_WAIT_TIME|PROCESS_NAME|MODULE_NAME|WORKER_NAME|ARGUMENTS|LOGFILE'.format(MODE_PYTHON)
3636

37-
REPOMGR_HOME = os.path.dirname(os.path.realpath(__file__))
38-
LOCAL_REPO = '{}/repo'.format(REPOMGR_HOME)
39-
4037
ROOT_NODE_NAME = 'PyRunnerRootNode'

pyrunner/core/pyrunner.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,12 @@ def save_state(self, suppress_output=False):
194194

195195
if not suppress_output:
196196
print('Saving Context Object to File: {}'.format(self.config.ctx_file))
197-
pickle.dump(state_obj, open(self.config.ctx_file, 'wb'))
197+
tmp = self.config.ctx_file+'.tmp'
198+
perm = self.config.ctx_file
199+
pickle.dump(state_obj, open(tmp, 'wb'))
200+
if os.path.isfile(perm):
201+
os.unlink(perm)
202+
os.rename(tmp, perm)
198203

199204
except Exception:
200205
print("Failure in save_context()")

pyrunner/serde/abstract.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#
1515
# SPDX-License-Identifier: Apache-2.0
1616

17+
import os
1718
from abc import ABCMeta, abstractmethod
1819

1920
class SerDe:
@@ -42,9 +43,15 @@ def deserialize(self, proc_file, restart=False):
4243
pass
4344

4445
def save_to_file(self, filepath, node_register):
46+
tmp = filepath+'.tmp'
47+
perm = filepath
48+
4549
try:
46-
with open(filepath, 'w') as file:
50+
with open(tmp, 'w') as file:
4751
file.write(self.serialize(node_register))
52+
if os.path.isfile(perm):
53+
os.unlink(perm)
54+
os.rename(tmp, perm)
4855
except Exception:
4956
print('Failure in save_to_file()')
5057
raise

0 commit comments

Comments
 (0)