Changeset 690 for tea4cups

Show
Ignore:
Timestamp:
11/07/06 23:47:25 (18 years ago)
Author:
jerome
Message:

Backported PyKota's locking mechanism.

Location:
tea4cups/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • tea4cups/trunk/NEWS

    r688 r690  
    2323Tea4CUPS News : 
    2424 
     25  * 3.13alpha (2006-11-07) : 
     26   
     27    - Backported locking mechanism from PyKota. 
     28     
    2529  * 3.12 (2006-08-11) : 
    2630   
  • tea4cups/trunk/tea4cups

    r688 r690  
    8787from struct import pack, unpack 
    8888 
    89 __version__ = "3.12_unofficial" 
     89__version__ = "3.13alpha_unofficial" 
    9090 
    9191class TeeError(Exception): 
     
    914914    def waitForLock(self) :     
    915915        """Waits until we can acquire the lock file.""" 
    916         self.logDebug("Waiting for lock...") 
    917916        lockfilename = self.DeviceURI.replace("/", ".") 
    918917        lockfilename = lockfilename.replace(":", ".") 
     
    921920        lockfilename = lockfilename.replace("@", ".") 
    922921        lockfilename = os.path.join(self.Directory, "%s-%s..LCK" % (self.myname, lockfilename)) 
    923         stillrunning = True 
    924         while 1 : 
    925             self.LockFile = open(lockfilename, "a+") 
    926             fcntl.lockf(self.LockFile, fcntl.LOCK_EX) # Will wait until lock available 
    927             self.LockFile.seek(0, 0)       
    928             try :  
    929                 oldpid = int(self.LockFile.readlines()[-1].strip()) 
    930             except :     
    931                 stillrunning = False 
    932             else :     
    933                 try : 
    934                     os.kill(oldpid, 0) 
    935                 except OSError :     
    936                     stillrunning = False 
    937             if not stillrunning :         
     922        self.logDebug("Waiting for lock %s to become available..." % lockfilename) 
     923        haslock = False 
     924        while not haslock : 
     925            try : 
     926                # open the lock file, optionally creating it if needed. 
     927                self.LockFile = open(lockfilename, "a+") 
     928                 
     929                # we wait indefinitely for the lock to become available. 
     930                # works over NFS too. 
     931                fcntl.lockf(self.LockFile, fcntl.LOCK_EX) 
     932                haslock = True 
     933                 
     934                self.logDebug("Lock %s acquired." % lockfilename) 
     935                 
     936                # Here we save the PID in the lock file, but we don't use 
     937                # it, because the lock file may be in a directory shared 
     938                # over NFS between two (or more) print servers, so the PID 
     939                # has no meaning in this case. 
    938940                self.LockFile.truncate(0) 
    939941                self.LockFile.seek(0, 0) 
    940                 self.LockFile.write("%s\n" % self.pid) 
     942                self.LockFile.write(str(self.pid)) 
    941943                self.LockFile.flush() 
    942                 break 
    943             else :     
    944                 time.sleep(0.1) 
    945         self.logDebug("Lock acquired.") 
     944            except IOError :             
     945                self.logDebug("I/O Error while waiting for lock %s" % lockfilename) 
     946                time.sleep(0.25) 
    946947         
    947948    def readConfig(self) : 
     
    12681269            except OSError, msg :     
    12691270                self.logInfo("Problem when removing %s : %s" % (self.DataFile, msg), "error") 
    1270         self.logDebug("Removing lock...") 
    1271         try : 
    1272             fcntl.flock(self.LockFile, fcntl.LOCK_UN) 
    1273             self.LockFile.close() 
    1274             # NB : we don't remove the lock file, since it might already be opened by another process. 
    1275         except :     
    1276             self.logInfo("Problem while removing lock.", "error") 
    1277         else :     
    1278             self.logDebug("Lock removed.") 
     1271                 
     1272        if self.LockFile is not None : 
     1273            self.logDebug("Removing lock...") 
     1274            try : 
     1275                fcntl.lockf(self.LockFile, fcntl.LOCK_UN) 
     1276                self.LockFile.close() 
     1277            except :     
     1278                self.logInfo("Problem while unlocking.", "error") 
     1279            else :     
     1280                self.logDebug("Lock removed.") 
    12791281        self.logDebug("Clean.") 
    12801282