Преглед на файлове

Make use of LockHandler instead of dumb real lock file

Jordi Boggiano преди 9 години
родител
ревизия
34088fb4aa
променени са 2 файла, в които са добавени 10 реда и са изтрити 13 реда
  1. 5 6
      src/Packagist/WebBundle/Command/DumpPackagesCommand.php
  2. 5 7
      src/Packagist/WebBundle/Command/IndexPackagesCommand.php

+ 5 - 6
src/Packagist/WebBundle/Command/DumpPackagesCommand.php

@@ -17,6 +17,7 @@ use Symfony\Component\Console\Input\InputArgument;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Filesystem\LockHandler;
 
 /**
  * @author Jordi Boggiano <j.boggiano@seld.be>
@@ -66,23 +67,21 @@ class DumpPackagesCommand extends ContainerAwareCommand
             $ids[] = $package['id'];
         }
 
-        $lock = $this->getContainer()->getParameter('kernel.cache_dir').'/composer-dumper.lock';
-        $timeout = 30*60;
+        $lock = new LockHandler('packagist_package_dumper');
 
         ini_set('memory_limit', -1);
         gc_enable();
 
         // another dumper is still active
-        if (file_exists($lock) && filemtime($lock) > time() - $timeout) {
+        if (!$lock->lock()) {
             if ($verbose) {
-                $output->writeln('Aborting, '.$lock.' file present');
+                $output->writeln('Aborting, another dumper is still active');
             }
             return;
         }
 
-        touch($lock);
         $result = $this->getContainer()->get('packagist.package_dumper')->dump($ids, $force, $verbose);
-        unlink($lock);
+        $lock->release();
 
         return $result ? 0 : 1;
     }

+ 5 - 7
src/Packagist/WebBundle/Command/IndexPackagesCommand.php

@@ -21,6 +21,7 @@ use Symfony\Component\Console\Input\InputArgument;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Filesystem\LockHandler;
 
 /**
  * @author Igor Wiedler <igor@wiedler.ch>
@@ -67,19 +68,16 @@ class IndexPackagesCommand extends ContainerAwareCommand
         $downloadManager = $this->getContainer()->get('packagist.download_manager');
         $favoriteManager = $this->getContainer()->get('packagist.favorite_manager');
 
-        $lock = $this->getContainer()->getParameter('kernel.cache_dir').'/composer-indexer.lock';
-        $timeout = 600;
+        $lock = new LockHandler('packagist_package_indexer');
 
         // another dumper is still active
-        if (file_exists($lock) && filemtime($lock) > time() - $timeout) {
+        if (!$lock->lock()) {
             if ($verbose) {
-                $output->writeln('Aborting, '.$lock.' file present');
+                $output->writeln('Aborting, another indexer is still active');
             }
             return;
         }
 
-        touch($lock);
-
         if ($package) {
             $packages = array(array('id' => $doctrine->getRepository('PackagistWebBundle:Package')->findOneByName($package)->getId()));
         } elseif ($force || $indexAll) {
@@ -165,7 +163,7 @@ class IndexPackagesCommand extends ContainerAwareCommand
             $solarium->update($update);
         }
 
-        unlink($lock);
+        $lock->release();
     }
 
     private function updateDocumentFromPackage(