This repository has been archived by the owner on Apr 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace Craft; | ||
|
||
/** | ||
* Task Manager. | ||
* | ||
* @author Bob Olde Hampsink <[email protected]> | ||
* @copyright Copyright (c) 2015, Bob Olde Hampsink | ||
* @license MIT | ||
* | ||
* @link http://github.com/boboldehampsink | ||
*/ | ||
class TaskManagerCommand extends BaseCommand | ||
{ | ||
/** | ||
* Runs pending tasks. | ||
* | ||
* @return int | ||
*/ | ||
public function actionRun() | ||
{ | ||
Craft::log(Craft::t('Running new tasks.')); | ||
|
||
// Make sure tasks aren't already running | ||
if (!craft()->tasks->isTaskRunning()) { | ||
|
||
// Is there a pending task? | ||
if (craft()->tasks->getNextPendingTask()) { | ||
|
||
// Start running tasks | ||
craft()->tasks->runPendingTasks(); | ||
|
||
return 1; | ||
} else { | ||
Craft::log(Craft::t('No pending tasks found.')); | ||
} | ||
} else { | ||
Craft::log(Craft::t('Tasks are already running.')); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
* Watch for tasks and run them. | ||
*/ | ||
public function actionWatch() | ||
{ | ||
Craft::log(Craft::t('Watching for new tasks.')); | ||
|
||
// Keep on checking for pending tasks | ||
while (true) { | ||
|
||
// Make sure tasks aren't already running | ||
if (!craft()->tasks->isTaskRunning()) { | ||
|
||
// Reset next pending tasks cache | ||
$this->resetCraftNextPendingTasksCache(); | ||
|
||
// Is there a pending task? | ||
if (craft()->tasks->getNextPendingTask()) { | ||
|
||
// Start running tasks | ||
craft()->tasks->runPendingTasks(); | ||
} else { | ||
Craft::log(Craft::t('No pending tasks found.')); | ||
} | ||
} else { | ||
Craft::log(Craft::t('Tasks are already running, skipping iteration.')); | ||
} | ||
|
||
// Sleep a little | ||
sleep(10); | ||
} | ||
} | ||
|
||
/** | ||
* Reset craft next pending task cache using reflection. | ||
*/ | ||
private function resetCraftNextPendingTasksCache() | ||
{ | ||
$obj = craft()->tasks; | ||
$refObject = new \ReflectionObject($obj); | ||
$refProperty = $refObject->getProperty('_nextPendingTask'); | ||
$refProperty->setAccessible(true); | ||
$refProperty->setValue($obj, null); | ||
} | ||
} |