Skip to content

Commit

Permalink
PIOXD-276 - Fixed cronjobs for multishop setups
Browse files Browse the repository at this point in the history
Fixes #19
  • Loading branch information
FatchipRobert committed Oct 8, 2024
1 parent 3923c39 commit d133009
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
27 changes: 16 additions & 11 deletions Application/Model/Cronjob.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ public static function getTableCreateQuery()
{
return "CREATE TABLE `".self::$sTableName."` (
`OXID` CHAR(32) NOT NULL COLLATE 'latin1_general_ci',
`OXSHOPID` int(11) NOT NULL DEFAULT 1,
`MINUTE_INTERVAL` INT(11) NOT NULL,
`LAST_RUN` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`OXID`) USING BTREE
PRIMARY KEY (`OXID`,`OXSHOPID`) USING BTREE
) COLLATE='utf8_general_ci' ENGINE=InnoDB";
}

Expand All @@ -52,14 +53,16 @@ public static function getTableCreateQuery()
*
* @param string $sCronjobId
* @param int $iDefaultMinuteInterval
* @param int $iShopId
* @return void
*/
public function addNewCronjob($sCronjobId, $iDefaultMinuteInterval)
public function addNewCronjob($sCronjobId, $iDefaultMinuteInterval, $iShopId)
{
$sQuery = "INSERT INTO `".self::$sTableName."` (OXID, MINUTE_INTERVAL, LAST_RUN) VALUES(:oxid, :minuteinterval, '0000-00-00 00:00:00');";
$sQuery = "INSERT INTO `".self::$sTableName."` (OXID, OXSHOPID, MINUTE_INTERVAL, LAST_RUN) VALUES(:oxid, :shopid, :minuteinterval, '0000-00-00 00:00:00');";

DatabaseProvider::getDb()->Execute($sQuery, [
':oxid' => $sCronjobId,
':shopid' => $iShopId,
':minuteinterval' => $iDefaultMinuteInterval,
]);
}
Expand All @@ -68,12 +71,13 @@ public function addNewCronjob($sCronjobId, $iDefaultMinuteInterval)
* Check if cronjob already exists
*
* @param string $sCronjobId
* @param int $iShopId
* @return bool
*/
public function isCronjobAlreadyExisting($sCronjobId)
public function isCronjobAlreadyExisting($sCronjobId, $iShopId)
{
$sQuery = "SELECT OXID FROM `".self::$sTableName."` WHERE OXID = ?;";
if (!DatabaseProvider::getDb()->getOne($sQuery, array($sCronjobId))) {
$sQuery = "SELECT OXID FROM `".self::$sTableName."` WHERE OXID = ? AND OXSHOPID = ?;";
if (!DatabaseProvider::getDb()->getOne($sQuery, array($sCronjobId, $iShopId))) {
return false;
}
return true;
Expand All @@ -85,22 +89,23 @@ public function isCronjobAlreadyExisting($sCronjobId)
* @param string $sCronjobId
* @return void
*/
public function markCronjobAsFinished($sCronjobId)
public function markCronjobAsFinished($sCronjobId, $iShopId)
{
DatabaseProvider::getDb()->execute("UPDATE `".self::$sTableName."` SET LAST_RUN = NOW() WHERE OXID = ?;", array($sCronjobId));
DatabaseProvider::getDb()->execute("UPDATE `".self::$sTableName."` SET LAST_RUN = NOW() WHERE OXID = ? AND OXSHOPID = ?;", array($sCronjobId, $iShopId));
}

/**
* Return cronjob data for given cronjobId
*
* @param string $sCronjobId
* @param int $iShopId
* @return array
*/
public function getCronjobData($sCronjobId)
public function getCronjobData($sCronjobId, $iShopId)
{
$oDb = DatabaseProvider::getDb(true);
$oDb->setFetchMode(DatabaseInterface::FETCH_MODE_ASSOC);
$sQuery = "SELECT * FROM `".self::$sTableName."` WHERE OXID = ?;";
return $oDb->getRow($sQuery, array($sCronjobId));
$sQuery = "SELECT * FROM `".self::$sTableName."` WHERE OXID = ? AND OXSHOPID = ?;";
return $oDb->getRow($sQuery, array($sCronjobId, $iShopId));
}
}
8 changes: 4 additions & 4 deletions Application/Model/Cronjob/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public function __construct($iShopId = false)
$this->iShopId = $iShopId;

$oCronjob = Cronjob::getInstance();
if ($this->getCronjobId() !== null && $oCronjob->isCronjobAlreadyExisting($this->getCronjobId()) === false) {
$oCronjob->addNewCronjob($this->getCronjobId(), $this->getDefaultMinuteInterval());
if ($this->getCronjobId() !== null && $oCronjob->isCronjobAlreadyExisting($this->getCronjobId(), $this->getShopId()) === false) {
$oCronjob->addNewCronjob($this->getCronjobId(), $this->getDefaultMinuteInterval(), $this->getShopId());
}
$this->loadDbData();
}
Expand All @@ -67,7 +67,7 @@ public function __construct($iShopId = false)
*/
protected function loadDbData()
{
$this->aDbData = Cronjob::getInstance()->getCronjobData($this->getCronjobId());
$this->aDbData = Cronjob::getInstance()->getCronjobData($this->getCronjobId(), $this->getShopId());
}

/**
Expand Down Expand Up @@ -182,7 +182,7 @@ protected function handleCronjob()
*/
protected function finishCronjob($blResult, $sError = false)
{
Cronjob::getInstance()->markCronjobAsFinished($this->getCronjobId());
Cronjob::getInstance()->markCronjobAsFinished($this->getCronjobId(), $this->getShopId());
if ($blResult === false) {
Logger::logMessage('Cron "'.$this->getCronjobId().'" failed'.($sError !== false ? " (Error: ".$sError.")" : ""), getShopBasePath().'/log/'.$this->sLogFileName);
}
Expand Down
4 changes: 3 additions & 1 deletion Application/Model/Cronjob/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ public function start($iShopId = false)
{
Base::outputInfo("START MOLLIE CRONJOB EXECUTION");

$oConfig = Registry::getConfig();
if ($iShopId !== false) {
$oConfig = Registry::getConfig();
$oConfig->setShopId($iShopId);
Registry::set(\OxidEsales\Eshop\Core\Config::class, $oConfig);
} else {
$iShopId = $oConfig->getShopId();
}

foreach ($this->getCronjobs() as $sCronjobClass) {
Expand Down
3 changes: 3 additions & 0 deletions Core/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ protected static function addDatabaseStructure()
self::addColumnIfNotExists('oxorder', 'MOLLIESHIPMENTHASBEENMARKED', "ALTER TABLE `oxorder` ADD COLUMN `MOLLIESHIPMENTHASBEENMARKED` tinyint(1) UNSIGNED NOT NULL DEFAULT '0';", $aShipmentSentQuery);

self::addColumnIfNotExists('oxuser', 'MOLLIECUSTOMERID', "ALTER TABLE `oxuser` ADD COLUMN `MOLLIECUSTOMERID` VARCHAR(32) CHARSET utf8 COLLATE utf8_general_ci DEFAULT '' NOT NULL;");

$aCronjobShopIdFollowup = ["ALTER TABLE `molliecronjob` DROP PRIMARY KEY, ADD PRIMARY KEY (`OXID`, `OXSHOPID`) USING BTREE;"];
self::addColumnIfNotExists('molliecronjob', 'OXSHOPID', "ALTER TABLE `molliecronjob` ADD `OXSHOPID` INT(11) NOT NULL DEFAULT '1' AFTER `OXID`;", $aCronjobShopIdFollowup);
}

/**
Expand Down

0 comments on commit d133009

Please sign in to comment.