Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scheduler Improvements #1017

Merged
merged 3 commits into from
Apr 7, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 48 additions & 1 deletion application/controllers/scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,51 @@ class Scheduler_Controller extends Controller {
public function __construct()
{
parent::__construct();
// Increase max execution time
if (ini_get('max_execution_time') < 180)
{
ini_set('max_execution_time', 180);
}
// Set time limit regardless of ini settings
set_time_limit(180);
}

public function index()
{
// Debug
$debug = "";


// @todo abstract most of this into a library, especially locking

// Ensure settings entry for `scheduler_lock` exists
try {
Database::instance()->query(
"INSERT INTO `".Kohana::config('database.default.table_prefix')."settings`
(`key`, `value`) VALUES ('scheduler_lock', 0)");
}
catch (Kohana_Database_Exception $e)
{
// ignore database error from already existing scheduler_lock row
}

// Now try and update the scheduler_lock
$result = Database::instance()->query(
"UPDATE `".Kohana::config('database.default.table_prefix')."settings`
SET `value` = UNIX_TIMESTAMP() + 180
WHERE `value` < UNIX_TIMESTAMP() AND `key` = 'scheduler_lock';");

// If no entries were changed, scheduler is already running
if ($result->count() <= 0)
{
Kohana::log('info', 'Could not acquire scheduler lock');
if (isset($_GET['debug']) AND $_GET['debug'] == 1)
{
echo 'Could not acquire scheduler lock';
}
return;
}

// Get all active scheduled items
foreach (ORM::factory('scheduler')
->where('scheduler_active','1')
Expand Down Expand Up @@ -106,6 +144,8 @@ public function index()
// Nada.
}

// @todo allow tasks to save state between runs.

if ($run !== FALSE)
{
// Set last time of last execution
Expand Down Expand Up @@ -160,7 +200,7 @@ public function index()
if (isset($this->profiler))
{
echo $this->profiler->render(TRUE);
}
}
}
else
{
Expand All @@ -172,6 +212,13 @@ public function index()
Header("Content-Length: 49");
echo pack('H*', '47494638396101000100910000000000ffffffff' . 'ffff00000021f90405140002002c000000000100' . '01000002025401003b');
}

// Release lock
$result = Database::instance()->query(
"UPDATE `".Kohana::config('database.default.table_prefix')."settings`
SET `value` = 0
WHERE `key` = 'scheduler_lock';");

}

}
2 changes: 1 addition & 1 deletion application/controllers/scheduler/s_cleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function remove_orphan_images()
{
// Get all the media files from the database so we can check if the file isn't orphaned

$images = ORM::factory("media")->find_all();
$images = ORM::factory("media")->where('media_type', 1)->find_all();

// Turn this into an array that we can easily check against

Expand Down