Skip to content
Browse files

Change state operations to be atomic (via file rename)

  • Loading branch information...
1 parent 31da84b commit e8a4886b93117230ae5d856996cd1ee2433bc8de @dcramer committed May 15, 2012
Showing with 13 additions and 16 deletions.
  1. +1 −1 setup.py
  2. +12 −15 src/taskmaster/server.py
View
2 setup.py
@@ -5,7 +5,7 @@
setup(
name="taskmaster",
license='Apache License 2.0',
- version="0.5.1",
+ version="0.5.2",
description="",
author="David Cramer",
author_email="dcramer@gmail.com",
View
27 src/taskmaster/server.py
@@ -11,7 +11,7 @@
import sys
from gevent_zeromq import zmq
from gevent.queue import Queue, Empty
-from os import path, unlink
+from os import path, unlink, rename
from taskmaster.util import import_target
@@ -168,25 +168,22 @@ def update_state(self, job_id, job, fp=None):
'job': job,
'job_id': job_id,
}
- if not fp:
- with open(self.state_file, 'w') as fp:
- pickle.dump(data, fp)
- else:
- fp.seek(0)
+
+ with open(self.state_file + '.tmp', 'w') as fp:
pickle.dump(data, fp)
+ rename(self.state_file + '.tmp', self.state_file)
def state_writer(self):
- with open(self.state_file, 'w') as fp:
- while self.server.is_alive():
- gevent.sleep(0)
+ while self.server.is_alive():
+ gevent.sleep(0)
- try:
- job_id, job = self.server.first_job()
- except IndexError:
- self.update_state(None, None, fp)
- continue
+ try:
+ job_id, job = self.server.first_job()
+ except IndexError:
+ self.update_state(None, None)
+ continue
- self.update_state(job_id, job, fp)
+ self.update_state(job_id, job)
def reset(self):
if path.exists(self.state_file):

0 comments on commit e8a4886

Please sign in to comment.
Something went wrong with that request. Please try again.