瀏覽代碼

Use mysql locking mechanism where relevant to allow running workers everywhere

Jordi Boggiano 5 年之前
父節點
當前提交
080dc9fdbf

+ 12 - 0
src/Packagist/WebBundle/Command/CompileStatsCommand.php

@@ -43,6 +43,16 @@ class CompileStatsCommand extends ContainerAwareCommand
      */
     protected function execute(InputInterface $input, OutputInterface $output)
     {
+        $locker = $this->getContainer()->get('locker');
+
+        $lockAcquired = $locker->lockCommand($this->getName());
+        if (!$lockAcquired) {
+            if ($input->getOption('verbose')) {
+                $output->writeln('Aborting, another task is running already');
+            }
+            return;
+        }
+
         $verbose = $input->getOption('verbose');
 
         $doctrine = $this->getContainer()->get('doctrine');
@@ -76,6 +86,8 @@ class CompileStatsCommand extends ContainerAwareCommand
 
         $redis->rename('downloads:trending:new', 'downloads:trending');
         $redis->rename('downloads:absolute:new', 'downloads:absolute');
+
+        $locker->unlockCommand($this->getName());
     }
 
     protected function sumLastNDays($days, $id, \DateTime $yesterday, $conn)

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

@@ -16,7 +16,6 @@ use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
 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>
@@ -54,12 +53,12 @@ class DumpPackagesCommand extends ContainerAwareCommand
         }
 
         // another dumper is still active
-        $lock = new LockHandler('packagist_package_dumper');
-        if (!$lock->lock()) {
+        $locker = $this->getContainer()->get('locker');
+        if (!$locker->lockCommand($this->getName())) {
             if ($verbose) {
-                $output->writeln('Aborting, another dumper is still active');
+                $output->writeln('Aborting, another task is running already');
             }
-            return;
+            return 0;
         }
 
         $doctrine = $this->getContainer()->get('doctrine');
@@ -85,9 +84,9 @@ class DumpPackagesCommand extends ContainerAwareCommand
         gc_enable();
 
         try {
-             $result = $this->getContainer()->get('packagist.package_dumper')->dump($ids, $force, $verbose);
+            $result = $this->getContainer()->get('packagist.package_dumper')->dump($ids, $force, $verbose);
         } finally {
-             $lock->release();
+            $locker->unlockCommand($this->getName());
         }
 
         return $result ? 0 : 1;

+ 11 - 12
src/Packagist/WebBundle/Command/IndexPackagesCommand.php

@@ -20,7 +20,6 @@ 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;
 use Doctrine\DBAL\Connection;
 
 class IndexPackagesCommand extends ContainerAwareCommand
@@ -60,6 +59,16 @@ class IndexPackagesCommand extends ContainerAwareCommand
             return;
         }
 
+        $locker = $this->getContainer()->get('locker');
+
+        $lockAcquired = $locker->lockCommand($this->getName());
+        if (!$lockAcquired) {
+            if ($input->getOption('verbose')) {
+                $output->writeln('Aborting, another task is running already');
+            }
+            return;
+        }
+
         $doctrine = $this->getContainer()->get('doctrine');
         $algolia = $this->getContainer()->get('packagist.algolia.client');
         $index = $algolia->initIndex($indexName);
@@ -68,16 +77,6 @@ class IndexPackagesCommand extends ContainerAwareCommand
         $downloadManager = $this->getContainer()->get('packagist.download_manager');
         $favoriteManager = $this->getContainer()->get('packagist.favorite_manager');
 
-        $lock = new LockHandler('packagist_algolia_indexer');
-
-        // another dumper is still active
-        if (!$lock->lock()) {
-            if ($verbose) {
-                $output->writeln('Aborting, another indexer is still active');
-            }
-            return;
-        }
-
         if ($package) {
             $packages = array(array('id' => $doctrine->getRepository('PackagistWebBundle:Package')->findOneByName($package)->getId()));
         } elseif ($force || $indexAll) {
@@ -164,7 +163,7 @@ class IndexPackagesCommand extends ContainerAwareCommand
             $this->updateIndexedAt($idsToUpdate, $doctrine, $indexTime->format('Y-m-d H:i:s'));
         }
 
-        $lock->release();
+        $locker->unlockCommand($this->getName());
     }
 
     private function packageToSearchableArray(

+ 0 - 1
src/Packagist/WebBundle/Command/MigrateDownloadCountsCommand.php

@@ -5,7 +5,6 @@ namespace Packagist\WebBundle\Command;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
-use Symfony\Component\Filesystem\LockHandler;
 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
 use Seld\Signal\SignalHandler;
 use Packagist\WebBundle\Entity\Package;