Skip to content

Commit

Permalink
add support for update stopper on app side
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicGD committed Jul 14, 2014
1 parent edb8756 commit fb7da5a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 17 deletions.
5 changes: 5 additions & 0 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ class Module extends \yii\base\Module
public $clearCache = true;

public $defaultRoute = "release/index";

/**
* @var array Classes with static method check(), that can stop or pause update process
*/
public $appUpdateStoppers = [];
}
17 changes: 17 additions & 0 deletions components/UpdateStopper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Created by PhpStorm.
* User: Георгий
* Date: 14.07.2014
* Time: 16:55
*/

namespace sitkoru\updater\components;


interface UpdateStopper {
/**
* @return mixed
*/
public function check();
}
79 changes: 62 additions & 17 deletions controllers/ReleaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace sitkoru\updater\controllers;

use sitkoru\updater\components\Console;
use sitkoru\updater\components\UpdateStopper;
use yii\base\Exception;
use yii\console\Controller;

Expand All @@ -17,6 +18,9 @@ class ReleaseController extends Controller
const MODE_UPGRADE = 1;
const MODE_DOWNGRADE = 2;

const PREVENT_WAIT = 1;
const PREVENT_CANCEL = 2;

/**
* @var \sitkoru\updater\Module
*/
Expand Down Expand Up @@ -76,27 +80,29 @@ protected function process($mode = null)
return false;
}
$this->createLock();
$this->runBefore();
$version = false;
switch ($mode) {
case self::MODE_UPGRADE:
$version = $this->upgrade();
break;
case self::MODE_DOWNGRADE:
$version = $this->downgrade();
break;
}
if ($version) {
$this->runComposer();
$this->saveVersion($version);
$this->runAssets();
$this->clearCaches();
if ($this->checkAppPreventUpdate()) {
$this->runBefore();
$version = false;
switch ($mode) {
case self::MODE_UPGRADE:
$version = $this->upgrade();
break;
case self::MODE_DOWNGRADE:
$version = $this->downgrade();
break;
}
if ($version) {
$this->runComposer();
$this->saveVersion($version);
$this->runAssets();
$this->clearCaches();
}
$this->finalize();
}
$this->runAfter();
$this->deleteLock();
return true;
}


/**
* @return bool
*/
Expand Down Expand Up @@ -377,4 +383,43 @@ private function runBefore()
$this->execCommand($command);
}
}

private function checkAppPreventUpdate()
{
if (count($this->module->appUpdateStoppers)) {
foreach ($this->module->appUpdateStoppers as $stopperClass) {
$stopper = new $stopperClass();
if ($stopper instanceof UpdateStopper) {
$canProcess = $stopper->check();
if ($canProcess !== true) {
Console::output(
$stopperClass . " prevent update process with message '" . $canProcess['message'] . "'"
);
$answer = Console::select(
"What should we do?",
[
self::PREVENT_WAIT => 'Wait. Updater would ask for permission every 5 sec and proceed after positive answer',
self::PREVENT_CANCEL => 'Cancel update',
]
);
if ($answer == self::PREVENT_CANCEL) {
$this->finalize();
return false;
} else {
while ($stopper->check() !== true) {
sleep(5);
}
}
}
}
}
}
return true;
}

private function finalize()
{
$this->runAfter();
$this->deleteLock();
}
}

0 comments on commit fb7da5a

Please sign in to comment.