Skip to content

Commit

Permalink
Onetime task tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bethrezen committed Oct 12, 2015
1 parent 6d9de98 commit 8485672
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 66 deletions.
26 changes: 0 additions & 26 deletions phpunit.xml

This file was deleted.

19 changes: 19 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit bootstrap="./tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false">
<testsuites>
<testsuite>
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>

13 changes: 9 additions & 4 deletions src/commands/DeferredController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function actionIndex($currentTime = null, $forceNoParallel = 0)
// group queue
$grouppedQueue = [];
$ids = [];
foreach ($queue as $index => $item) {
foreach ($queue as $item) {
$ids[] = $item->id;

$itemCanBeAdded = true;
Expand Down Expand Up @@ -87,11 +87,10 @@ public function actionIndex($currentTime = null, $forceNoParallel = 0)

// ok, now we can process groups
if ($this->canRunInParallel() && $this->forceNoParallel === false) {

$fork = new Fork;

foreach ($grouppedQueue as $groupId => $items) {
$fork->call(function() use($groupId, $items){
$fork->call(function () use ($groupId, $items) {
$this->processGroup($groupId, $items);
}, [$groupId, $items]);
}
Expand Down Expand Up @@ -274,8 +273,14 @@ private function runQueueItem($item)
if (empty($item->cli_command) === false) {
$command->setPrefix($item->cli_command);
} else {
if (stristr(PHP_OS, 'WIN')) {
$command
->setPrefix('yii.bat');
} else {
$command
->setPrefix('./yii');
}
$command
->setPrefix('./yii')
->add($item->console_route);
}
if (empty($item->command_arguments) === false) {
Expand Down
16 changes: 12 additions & 4 deletions src/helpers/DeferredTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,28 @@ public function registerTask()
return $this->model->save();
}

public function __construct($attributes=[])
public function __construct($attributes = [])
{
$this->model = new DeferredQueue();
$this->model->initiated_date = date("Y-m-d H:i:s");
$this->model->setAttributes($attributes);
}

public function consoleRoute($route, $arguments=[])
public function consoleRoute($route, $arguments = [])
{
$this->model->console_route = $route;
$this->setArguments($arguments);
return $this;
}

public function cliCommand($command, $arguments=[])
public function cliCommand($command, $arguments = [])
{
$this->model->cli_command = $command;
$this->setArguments($arguments);
return $this;
}

private function setArguments($arguments=[])
private function setArguments($arguments = [])
{
if (is_array($arguments) === true) {
$this->model->command_arguments = implode("\n", $arguments);
Expand All @@ -47,4 +47,12 @@ private function setArguments($arguments=[])
}
return $this;
}

/**
* @return \DevGroup\DeferredTasks\models\DeferredQueue|null
*/
public function model()
{
return $this->model;
}
}
10 changes: 7 additions & 3 deletions src/helpers/OnetimeTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

namespace DevGroup\DeferredTasks\helpers;

use DevGroup\DeferredTasks\helpers\DeferredTask;
use Cron\CronExpression;
use DevGroup\DeferredTasks\models\DeferredQueue;

class OnetimeTask extends DeferredTask
{
public function __construct($attributes=[])
public function __construct($attributes = [])
{
parent::__construct($attributes);
$this->model->is_repeating_task = false;
$this->model->cron_expression = '* * * * *';
$this->model->status = DeferredQueue::STATUS_SCHEDULED;
$cron = CronExpression::factory($this->model->cron_expression);
$this->model->next_start = date('Y-m-d H:i:s', $cron->getNextRunDate()->getTimestamp());
}
}
}
46 changes: 23 additions & 23 deletions src/migrations/m160514_135802_initial.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,32 @@ public function up()
}

$this->createTable('{{%deferred_queue}}', [
'id' => Schema::TYPE_PK,
'deferred_group_id' => Schema::TYPE_INTEGER . ' NOT NULL DEFAULT 0',
'user_id' => Schema::TYPE_INTEGER . ' NULL',
'initiated_date' => Schema::TYPE_TIMESTAMP . ' NOT NULL DEFAULT CURRENT_TIMESTAMP',
'is_repeating_task' => Schema::TYPE_BOOLEAN . ' NOT NULL DEFAULT 0',
'cron_expression' => Schema::TYPE_STRING . ' NULL',
'next_start' => Schema::TYPE_TIMESTAMP . ' NULL',
'status' => Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 0',
'last_run_date' => Schema::TYPE_TIMESTAMP . ' NULL',
'console_route' => Schema::TYPE_STRING . ' NULL',
'cli_command' => Schema::TYPE_STRING . ' NULL',
'command_arguments' => Schema::TYPE_TEXT . ' NULL',
'notify_initiator' => Schema::TYPE_BOOLEAN . ' NOT NULL DEFAULT 1',
'notify_roles' => Schema::TYPE_STRING . ' NULL',
'email_notification' => Schema::TYPE_BOOLEAN . ' NOT NULL DEFAULT 1',
'id' => $this->primaryKey(),
'deferred_group_id' => $this->integer()->notNull()->defaultValue(0),
'user_id' => $this->integer(),
'initiated_date' => $this->dateTime(),
'is_repeating_task' => $this->boolean()->defaultValue(0)->notNull(),
'cron_expression' => $this->string(),
'next_start' => $this->string(),
'status' => $this->smallInteger()->defaultValue(0)->notNull(),
'last_run_date' => $this->timestamp(),
'console_route' => $this->string(),
'cli_command' => $this->string(),
'command_arguments' => $this->text(),
'notify_initiator' => $this->boolean()->defaultValue(1)->notNull(),
'notify_roles' => $this->string(),
'email_notification' => $this->boolean()->defaultValue(1)->notNull(),
], $tableOptions);

$this->createTable('{{%deferred_group}}', [
'id' => Schema::TYPE_PK,
'name' => Schema::TYPE_STRING . ' NULL',
'allow_parallel_run' => Schema::TYPE_BOOLEAN . ' NOT NULL DEFAULT 0',
'run_last_command_only' => Schema::TYPE_BOOLEAN . ' NOT NULL DEFAULT 0',
'notify_initiator' => Schema::TYPE_BOOLEAN . ' NOT NULL DEFAULT 1',
'notify_roles' => Schema::TYPE_STRING . ' NULL',
'email_notification' => Schema::TYPE_BOOLEAN . ' NOT NULL DEFAULT 1',
'group_notifications' => Schema::TYPE_BOOLEAN . ' NOT NULL DEFAULT 1',
'id' => $this->primaryKey(),
'name' => $this->string(),
'allow_parallel_run' => $this->boolean()->defaultValue(0)->notNull(),
'run_last_command_only' => $this->boolean()->defaultValue(0)->notNull(),
'notify_initiator' => $this->boolean()->defaultValue(1)->notNull(),
'notify_roles' => $this->string(),
'email_notification' => $this->boolean()->defaultValue(1)->notNull(),
'group_notifications' => $this->boolean()->defaultValue(1)->notNull(),
], $tableOptions);

$this->createIndex('by_status', '{{%deferred_queue}}', [
Expand Down
4 changes: 2 additions & 2 deletions src/models/DeferredGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public static function tableName()
public function rules()
{
return [
[['allow_parallel_run', 'run_last_command_only', 'notify_initiator', 'email_notification', 'group_notifications'], 'integer'],
[['name', 'notify_roles'], 'string', 'max' => 255]
[['name', 'notify_roles'], 'string', 'max' => 255],
[['allow_parallel_run','notify_initiator','email_notification','group_notifications','run_last_command_only'], 'filter', 'filter'=>'boolval']
];
}

Expand Down
5 changes: 3 additions & 2 deletions src/models/DeferredQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @property integer $deferred_group_id
* @property integer $user_id
* @property string $initiated_date
* @property integer $is_repeating_task
* @property boolean $is_repeating_task
* @property string $cron_expression
* @property string $next_start
* @property integer $status
Expand Down Expand Up @@ -49,10 +49,11 @@ public static function tableName()
public function rules()
{
return [
[['deferred_group_id', 'user_id', 'is_repeating_task', 'status', 'notify_initiator', 'email_notification'], 'integer'],
[['deferred_group_id', 'user_id', 'status'], 'integer'],
[['initiated_date', 'next_start', 'last_run_date'], 'safe'],
[['cron_expression', 'console_route', 'cli_command', 'notify_roles'], 'string', 'max' => 255],
[['command_arguments'], 'string'],
[['is_repeating_task','notify_initiator','email_notification'], 'filter', 'filter'=>'boolval']
];
}

Expand Down
30 changes: 28 additions & 2 deletions tests/DeferredControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace DevGroup\DeferredTasks\Tests;

use DevGroup\DeferredTasks\commands\DeferredController;
use DevGroup\DeferredTasks\helpers\OnetimeTask;
use DevGroup\DeferredTasks\models\DeferredQueue;
use Yii;
use yii\db\Connection;
Expand Down Expand Up @@ -56,6 +57,11 @@ protected function mockApplication($config = [], $appClass = '\yii\console\Appli
'id' => 'testapp',
'basePath' => __DIR__,
'vendorPath' => '../../vendor',
'controllerMap' => [
'deferred' => [
'class' => DeferredController::className(),
],
],
'components' => [
'mutex' => [
'class' => 'yii\mutex\MysqlMutex',
Expand Down Expand Up @@ -124,8 +130,6 @@ public function testGetNextTasks()

public function testRunProcesses()
{


$files = [
'task3',
'task4',
Expand Down Expand Up @@ -155,4 +159,26 @@ public function testRunProcesses()
$this->assertTrue(file_exists('/tmp/task11'));
}

public function testRegister()
{
$files = [
'task91',
];
foreach ($files as $f) {
if (file_exists("/tmp/$f")) {
unlink("/tmp/$f");
}
}
$task = new OnetimeTask();
$task->cliCommand('touch', ['/tmp/task91']);

$this->assertTrue($task->registerTask());
$time = time()+120;
echo "Running $time = " . date("Y-m-d H:i:s", $time) . "\n";

Yii::$app->runAction('deferred/index', [$time, 1]);

echo "Checking\n";
$this->assertTrue(file_exists('/tmp/task91'));
}
}

0 comments on commit 8485672

Please sign in to comment.