Changeset 2883

Show
Ignore:
Timestamp:
05/11/06 16:08:45 (18 years ago)
Author:
jerome
Message:

Added locking to prevent two or more cupspykota processes to handle different
jobs for the same print queue at the very same time.

Location:
pykota/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/cupspykota

    r2882 r2883  
    2727import sys 
    2828import os 
     29import fcntl 
    2930import time 
    3031import errno 
     
    113114        self.logdebug("SIGINT enabled.") 
    114115         
     116    def waitForLock(self) :     
     117        """Waits until we can acquire the lock file.""" 
     118        self.logdebug("Waiting for lock...") 
     119        stillrunning = True 
     120        while 1 : 
     121            self.lockfile = open(self.lockfilename, "a+") 
     122            fcntl.lockf(self.lockfile, fcntl.LOCK_EX) # Will wait until lock available 
     123            self.lockfile.seek(0, 0)       
     124            try :  
     125                oldpid = int(self.lockfile.read().strip()) 
     126            except :     
     127                stillrunning = False 
     128            else :     
     129                try : 
     130                    os.kill(oldpid, 0) 
     131                except OSError :     
     132                    stillrunning = False 
     133            if not stillrunning :         
     134                self.lockfile.seek(0, 0) 
     135                self.lockfile.write("%i" % self.pid) 
     136                self.lockfile.flush() 
     137                break 
     138            else :     
     139                time.sleep(0.1) 
     140        self.logdebug("Lock acquired.") 
     141                     
    115142    def discoverOtherBackends(self) :     
    116143        """Discovers the other CUPS backends. 
     
    257284        (jbtype, self.JobBillingCode) = jbing 
    258285         
     286        self.lockfilename = os.path.join(self.Directory, "%s-%s..LCK" % (self.myname, self.PrinterName)) 
     287         
    259288        self.logdebug("Backend : %s" % self.RealBackend) 
    260289        self.logdebug("DeviceURI : %s" % self.DeviceURI) 
     
    404433            self.logdebug("Work file %s will be kept." % self.DataFile) 
    405434        PyKotaTool.clean(self)     
     435        self.logdebug("Removing lock...") 
     436        try : 
     437            fcntl.flock(self.lockfile, fcntl.LOCK_UN) 
     438            self.lockfile.close() 
     439            # NB : we don't remove the lock file, since it might already be opened by another process. 
     440        except :     
     441            pass 
     442        else :     
     443            self.logdebug("Lock removed.") 
    406444        self.logdebug("Clean.") 
    407445             
     
    12511289            wrapper.deferredInit() 
    12521290            wrapper.initBackendParameters() 
     1291            wrapper.waitForLock() 
    12531292            wrapper.saveDatasAndCheckSum() 
    12541293            wrapper.preaccounter = openAccounter(wrapper, ispreaccounter=1) 
  • pykota/trunk/NEWS

    r2882 r2883  
    2424    - 1.25alpha3 (2006-05-11) : 
    2525     
     26        - Added a lock to ensure that a single cupspykota backend 
     27          is processing datas for a particular print queue at any 
     28          given time. 
     29           
    2630        - Now exports more environment variables for the subprocesses. 
    2731