Repository :
http://git.fedorahosted.org/cgit/copr.git
On branch : master
---------------------------------------------------------------
commit a0efd1068f13915cb464e84c74ded44f9f4d3c7d
Author: Miroslav Suchý <miroslav(a)suchy.cz>
Date: Tue Jan 21 01:07:41 2014 +0100
[backend] lock log file before writing
this prevent race condition when two workers appends data at exactly same time.
---------------------------------------------------------------
backend/dispatcher.py | 6 +++++-
backend/mockremote.py | 21 ++++++++++-----------
2 files changed, 15 insertions(+), 12 deletions(-)
diff --git a/backend/dispatcher.py b/backend/dispatcher.py
index d251075..589c196 100644
--- a/backend/dispatcher.py
+++ b/backend/dispatcher.py
@@ -1,3 +1,4 @@
+import fcntl
import re
import os
import sys
@@ -79,7 +80,10 @@ class WorkerCallback(object):
if self.logfile:
now = time.strftime('%F %T')
try:
- open(self.logfile, 'a').write(str(now) + ': ' + msg +
'\n')
+ with open(self.logfile, 'a') as lf:
+ fcntl.flock(lf, fcntl.LOCK_EX)
+ lf.write(str(now) + ': ' + msg + '\n')
+ fcntl.flock(lf, fcntl.LOCK_UN)
except (IOError, OSError), e:
print >>sys.stderr, 'Could not write to logfile %s - %s' %
(self.logfile, str(e))
diff --git a/backend/mockremote.py b/backend/mockremote.py
index 2d15d62..59cff55 100755
--- a/backend/mockremote.py
+++ b/backend/mockremote.py
@@ -81,14 +81,18 @@ def read_list_from_file(fn):
return lst
-def log(lf, msg):
+def log(lf, msg, quiet=None):
if lf:
now = time.time()
try:
- open(lf, 'a').write(str(now) + ':' + msg + '\n')
+ with open(lf, 'a') as lfh:
+ fcntl.flock(lfh, fcntl.LOCK_EX)
+ lfh.write(str(now) + ':' + msg + '\n')
+ fcntl.flock(lfh, fcntl.LOCK_UN)
except (IOError, OSError), e:
sys.stderr.write('Could not write to logfile %s - %s\n' % (lf,
str(e)))
- print msg
+ if not quiet:
+ print msg
def get_ans_results(results, hostname):
if hostname in results['dark']:
@@ -204,14 +208,7 @@ class CliLogCallBack(DefaultCallBack):
self.log("Error: %s" % msg)
def log(self, msg):
- if self.logfn:
- now = time.time()
- try:
- open(self.logfn, 'a').write(str(now) + ':' + msg +
'\n')
- except (IOError, OSError), e:
- print >> sys.stderr, 'Could not write to logfile %s - %s' %
(self.logfn, str(e))
- if not self.quiet:
- print msg
+ log(self.logfn, msg, self.quiet)
class Builder(object):
def __init__(self, hostname, username, timeout, mockremote, buildroot_pkgs):
@@ -531,11 +528,13 @@ class MockRemote(object):
if not os.path.exists(chroot_dir):
os.makedirs(self.destdir + '/' + self.chroot)
r_log = open(chroot_dir + '/mockchain.log', 'a')
+ fcntl.flock(r_log, fcntl.LOCK_EX)
r_log.write('\n\n%s\n\n' % pkg)
r_log.write(b_out)
if b_err:
r_log.write('\nstderr\n')
r_log.write(b_err)
+ fcntl.flock(r_log, fcntl.LOCK_UN)
r_log.close()