132 | | while 1 : |
133 | | self.lockfile = open(self.lockfilename, "a+") |
134 | | fcntl.lockf(self.lockfile, fcntl.LOCK_EX) # Will wait until lock available |
135 | | self.lockfile.seek(0, 0) |
136 | | try : |
137 | | oldpid = int(self.lockfile.readlines()[-1].strip()) |
138 | | except : |
139 | | stillrunning = False |
| 134 | while not haslock : |
| 135 | self.logdebug("Trying to acquire lock %s" % self.lockfilename) |
| 136 | try : |
| 137 | self.lockfile = os.open(self.lockfilename, |
| 138 | os.O_CREAT | os.O_EXCL | os.O_WRONLY, |
| 139 | 0600) |
| 140 | except OSError, error : |
| 141 | if error.errno == errno.EEXIST : |
| 142 | self.logdebug("Lock not available.") |
| 143 | else : |
| 144 | self.logdebug("Lock not available : %s" % error) |
| 145 | try : |
| 146 | try : |
| 147 | lockfobj = open(self.lockfilename, "r") |
| 148 | oldpid = int(lockfobj.readline().strip()) |
| 149 | except IOError : |
| 150 | self.logdebug("Lock file has probably been deleted in the meantime.") |
| 151 | except ValueError : |
| 152 | self.logdebug("Invalid content in existing lock file.") |
| 153 | else : |
| 154 | try : |
| 155 | # Is the old process still alive ? |
| 156 | os.kill(oldpid, 0) |
| 157 | except OSError : |
| 158 | # old process is dead, we delete the lock file |
| 159 | self.logdebug("Old process (PID %i) is dead." % oldpid) |
| 160 | os.unlink(self.lockfilename) |
| 161 | self.logdebug("Lock file %s removed." % self.lockfilename) |
| 162 | else : |
| 163 | self.logdebug("Old process (PID %i) still alive, waiting a bit..." % oldpid) |
| 164 | time.sleep(0.25) # No hurry :) |
| 165 | finally : |
| 166 | try : |
| 167 | lockfobj.close() |
| 168 | except NameError : |
| 169 | pass |
512 | | if not self.config.getPrinterKeepFiles(self.PrinterName) : |
513 | | self.logdebug("Work file %s will be deleted." % self.DataFile) |
| 531 | if (self.DataFile is not None) and os.path.exists(self.DataFile) : |
| 532 | if not self.config.getPrinterKeepFiles(self.PrinterName) : |
| 533 | self.logdebug("Work file %s will be deleted." % self.DataFile) |
| 534 | try : |
| 535 | os.remove(self.DataFile) |
| 536 | except OSError, msg : |
| 537 | self.logdebug("Problem while deleting work file %s : %s" % (self.DataFile, msg)) |
| 538 | else : |
| 539 | self.logdebug("Work file %s has been deleted." % self.DataFile) |
| 540 | else : |
| 541 | self.logdebug("Work file %s will be kept." % self.DataFile) |
| 542 | PyKotaTool.clean(self) |
| 543 | if (self.lockfile is not None) and os.path.exists(self.lockfilename) : |
| 544 | self.logdebug("Removing lock...") |
515 | | if os.path.exists(self.DataFile) : |
516 | | os.remove(self.DataFile) |
517 | | except OSError, msg : |
518 | | self.logdebug("Problem while deleting work file %s : %s" % (self.DataFile, msg)) |
519 | | else : |
520 | | self.logdebug("Work file %s has been deleted." % self.DataFile) |
521 | | else : |
522 | | self.logdebug("Work file %s will be kept." % self.DataFile) |
523 | | PyKotaTool.clean(self) |
524 | | self.logdebug("Removing lock...") |
525 | | try : |
526 | | fcntl.flock(self.lockfile, fcntl.LOCK_UN) |
527 | | self.lockfile.close() |
528 | | # NB : we don't remove the lock file, since it might already be opened by another process. |
529 | | except : |
530 | | pass |
531 | | else : |
532 | | self.logdebug("Lock removed.") |
| 546 | os.close(self.lockfile) |
| 547 | os.unlink(self.lockfilename) |
| 548 | except : |
| 549 | self.logdebug("Problem while removing lock file %s" % self.lockfilename) |
| 550 | else : |
| 551 | self.logdebug("Lock file %s removed." % self.lockfilename) |
1389 | | wrapper.deferredInit() |
1390 | | wrapper.initBackendParameters() |
1391 | | wrapper.waitForLock() |
1392 | | if os.environ.get("PYKOTASTATUS") == "CANCELLED" : |
1393 | | raise KeyboardInterrupt |
1394 | | wrapper.saveDatasAndCheckSum() |
1395 | | wrapper.preaccounter = openAccounter(wrapper, ispreaccounter=1) |
1396 | | wrapper.accounter = openAccounter(wrapper) |
1397 | | wrapper.precomputeJobSize() |
1398 | | wrapper.exportJobInfo() # exports a first time to give hints to external scripts |
1399 | | wrapper.overwriteJobAttributes() |
1400 | | wrapper.exportJobInfo() # re-exports in case it was overwritten |
1401 | | retcode = wrapper.mainWork() |
1402 | | except KeyboardInterrupt : |
1403 | | wrapper.printInfo(_("Job %s interrupted by the administrator !") % wrapper.JobId, "warn") |
1404 | | retcode = 0 |
1405 | | except SystemExit, err : |
1406 | | retcode = err.code |
1407 | | except : |
1409 | | wrapper.crashed("cupspykota backend failed") |
| 1409 | wrapper.deferredInit() |
| 1410 | wrapper.initBackendParameters() |
| 1411 | wrapper.waitForLock() |
| 1412 | if os.environ.get("PYKOTASTATUS") == "CANCELLED" : |
| 1413 | raise KeyboardInterrupt |
| 1414 | wrapper.saveDatasAndCheckSum() |
| 1415 | wrapper.preaccounter = openAccounter(wrapper, ispreaccounter=1) |
| 1416 | wrapper.accounter = openAccounter(wrapper) |
| 1417 | wrapper.precomputeJobSize() |
| 1418 | wrapper.exportJobInfo() # exports a first time to give hints to external scripts |
| 1419 | wrapper.overwriteJobAttributes() |
| 1420 | wrapper.exportJobInfo() # re-exports in case it was overwritten |
| 1421 | retcode = wrapper.mainWork() |
| 1422 | except KeyboardInterrupt : |
| 1423 | wrapper.printInfo(_("Job %s interrupted by the administrator !") % wrapper.JobId, "warn") |
| 1424 | retcode = 0 |
| 1425 | except SystemExit, err : |
| 1426 | retcode = err.code |