Skip to content

Commit

Permalink
Option to disable mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
timkelty committed Nov 21, 2023
1 parent 84af44f commit 0c409b6
Showing 3 changed files with 21 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ class Config extends BaseConfig
public ?string $cdnSigningKey = null;
public bool $useAssetBundleCdn = true;
public ?string $previewDomain = null;
public bool $useMutex = true;
protected ?string $region = null;
protected bool $useAssetCdn = true;
protected bool $useArtifactCdn = true;
@@ -34,6 +35,7 @@ public function init(): void
$this->useAssetCdn = false;
$this->useArtifactCdn = false;
$this->useAssetBundleCdn = false;
$this->useMutex = false;
}
}

17 changes: 12 additions & 5 deletions src/Helper.php
Original file line number Diff line number Diff line change
@@ -12,6 +12,8 @@
use craft\helpers\App;
use craft\helpers\ConfigHelper;
use craft\queue\Queue as CraftQueue;
use yii\di\Instance;
use yii\mutex\Mutex as YiiMutex;
use yii\web\DbSession;

class Helper
@@ -85,11 +87,16 @@ public static function modifyConfig(array &$config, string $appType): void
return Craft::createObject($config);
};

$config['components']['mutex'] = function() {
return Craft::createObject([
'class' => Mutex::class,
'namePrefix' => Module::getInstance()->getConfig()->environmentId,
]);

$config['components']['mutex'] = function() use ($config) {
$mutex = Module::getInstance()->getConfig()->useMutex
? Craft::createObject([
'class' => Mutex::class,
'namePrefix' => Module::getInstance()->getConfig()->environmentId,
])
: $config['components']['mutex'];

return Instance::ensure($mutex, YiiMutex::class);
};

$config['components']['queue'] = function() {
13 changes: 7 additions & 6 deletions src/Module.php
Original file line number Diff line number Diff line change
@@ -197,12 +197,13 @@ protected function bootstrapCloud(ConsoleApplication|WebApplication $app): void
],
);

Event::on(
WebResponse::class,
YiiWebResponse::EVENT_BEFORE_SEND,
[$this->get('mutex'), 'handleBeforeSend'],
);

if ($this->getConfig()->useMutex) {
Event::on(
WebResponse::class,
YiiWebResponse::EVENT_BEFORE_SEND,
[$this->get('mutex'), 'handleBeforeSend'],
);
}

$this->setComponents([
'staticCaching' => StaticCaching::class,

0 comments on commit 0c409b6

Please sign in to comment.