progress.module in Progress 6
Same filename and directory in other branches
Progress framework for keeping track of progresses
@todo Force MyISAM for table {progress} to avoid transaction/isolation level problems, and use INSERT DELAYED for performance?
File
progress.moduleView source
<?php
/**
* @file
*
* Progress framework for keeping track of progresses
*
* @todo Force MyISAM for table {progress} to avoid transaction/isolation level problems, and use INSERT DELAYED for performance?
*/
define('PROGRESS_AGE', 86400);
/**
* Initialize a progress
*
* @param string $name
* Name of progress
* @param string $message
* Message for progress
* @return mixed
* Result from db_query()
*/
function progress_initialize_progress($name, $message) {
progress_remove_progress($name);
$current = microtime(TRUE);
return db_query("INSERT INTO {progress} (name, progress, message, start_stamp, current_stamp) VALUES('%s', %f, '%s', %f, %f)", $name, 0, $message, $current, $current);
}
/**
* Set progress
*
* @param string $name
* Name of progress
* @param string $message
* Message for progress
* @param double $progress
* Current progress (0 - 1)
* @return mixed
* Result from db_query()
*/
function progress_set_progress($name, $message, $progress) {
$current = microtime(TRUE);
return db_query("UPDATE {progress} SET progress = %f, message = '%s', current_stamp = %f WHERE name = '%s'", $progress, $message, $current, $name);
}
/**
* Set progress start time
*
* @param string $name
* Name of progress
* @param float $start
* Start time of progress
*/
function progress_set_progress_start($name, $start) {
return db_query("UPDATE {progress} SET start_stamp = %d WHERE name = '%s'", $start, $name);
}
/**
* Set intervalled progress
* Use when you don't need to spam the progress table every time.
*
* @param string $name
* Name of progress
* @param string $message
* Message for progress
* @param double $progress
* Current progress (0 - 1)
* @param double $interval
* Interval in seconds
* @return mixed
* Result from db_query()
*/
function progress_set_intervalled_progress($name, $message, $progress, $interval = NULL) {
static $set = array();
if (isset($interval, $set[$name])) {
if ($set[$name] + $interval > microtime(TRUE)) {
return TRUE;
}
}
$result = progress_set_progress($name, $message, $progress);
$set[$name] = microtime(TRUE);
return $result;
}
/**
* Get progress
*
* @param string $name
* Name of progress
* @return object
* Object containing all the progress data
*/
function progress_get_progress($name) {
$result = db_fetch_object(db_query("SELECT name, progress, message, start_stamp, end_stamp, current_stamp FROM {progress} WHERE name = '%s'", $name));
if ($result) {
$result->start = $result->start_stamp;
$result->end = $result->end_stamp;
$result->current = $result->current_stamp;
}
return $result;
}
/**
* End a progress. Sets to 100% (=1)
*
* @param string $name
* Name of progress
* @param string $message
* Message for progress
* @return boolean
* TRUE on success, FALSE on fail
*/
function progress_end_progress($name, $message) {
$current = microtime(TRUE);
return db_query("UPDATE {progress} SET progress = %f, message = '%s', end_stamp = %f WHERE name = '%s'", 1, $message, $current, $name);
}
/**
* Remove a progress
*
* @param string $name
* Name of progress
* @return boolean
* TRUE on success, FALSE on fail
*/
function progress_remove_progress($name) {
return db_query("DELETE FROM {progress} WHERE name = '%s'", $name);
}
/**
* Get all progresses
*
* @return array
* Array of objects containing all progress data
*/
function progress_get_progresses() {
$progresses = array();
$result = db_query("SELECT name, progress, message, start_stamp, end_stamp, current_stamp FROM {progress}");
while ($progress = db_fetch_object($result)) {
$progress->start = $progress->start_stamp;
$progress->end = $progress->end_stamp;
$progress->current = $progress->current_stamp;
$progresses[$progress->name] = $progress;
}
return $progresses;
}
/**
* Estimate completion time of a progress
*
* @param object $progress
* Progress object
* @return double
* Estimated unix timestamp of completion in microseconds
*/
function progress_estimate_completion($progress) {
if ($progress->progress) {
$progress->estimate = $progress->start + 1 / $progress->progress * ($progress->current - $progress->start);
}
else {
$progress->estimate = NULL;
}
return $progress->estimate;
}
/**
* Implementation of hook_cron().
*
* Clean up finished progresses
*/
function progress_cron() {
db_query("DELETE FROM {progress} WHERE end_stamp > 0 AND end_stamp < %d", time() - variable_get('progress_age', PROGRESS_AGE));
}
/**
* Implementation of hook_cronapi().
*/
function progress_cronapi($op, $job = NULL) {
switch ($op) {
case 'list':
return array(
'progress_cron' => t('Cleanup old progresses'),
);
}
}
Functions
Name | Description |
---|---|
progress_cron | Implementation of hook_cron(). |
progress_cronapi | Implementation of hook_cronapi(). |
progress_end_progress | End a progress. Sets to 100% (=1) |
progress_estimate_completion | Estimate completion time of a progress |
progress_get_progress | Get progress |
progress_get_progresses | Get all progresses |
progress_initialize_progress | Initialize a progress |
progress_remove_progress | Remove a progress |
progress_set_intervalled_progress | Set intervalled progress Use when you don't need to spam the progress table every time. |
progress_set_progress | Set progress |
progress_set_progress_start | Set progress start time |
Constants
Name | Description |
---|---|
PROGRESS_AGE | @file |