You are here

progress.module in Background Process 8

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/progress.module
View 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 Default Value for Progress Age.
 */
const PROGRESS_AGE = 86400;

/**
 * Initialize a progress.
 */
function progress_initialize_progress($name, $message) {
  progress_remove_progress($name);
  $current = microtime(TRUE);
  return db_insert('progress')
    ->fields([
    'name' => $name,
    'progress' => 0,
    'message' => $message,
    'start_stamp' => $current,
    'current_stamp' => $current,
  ])
    ->execute();
}

/**
 * Set progress.
 */
function progress_set_progress($name, $message, $progress) {
  $current = microtime(TRUE);
  $result = db_update('progress')
    ->fields([
    'progress' => $progress,
    'message' => $message,
    'current_stamp' => $current,
  ])
    ->condition('name', $name)
    ->execute();
  if (\Drupal::moduleHandler()
    ->moduleExists('nodejs')) {
    $start_stamp = db_query("SELECT start_stamp FROM {progress} WHERE name = :name", [
      ':name' => $name,
    ])
      ->fetchField();
    $object = (object) [
      'name' => $name,
      'progress' => $progress,
      'message' => $message,
      'current_stamp' => $current,
      'start_stamp' => $start_stamp,
    ];
    $message = (object) [
      'channel' => 'progress',
      'data' => (object) [
        'action' => 'setProgress',
        'progress' => $object,
        'timestamp' => microtime(TRUE),
      ],
      'callback' => 'nodejsProgress',
    ];
    \Drupal::moduleHandler()
      ->alter('progress_message', $message);
    nodejs_send_content_channel_message($message);
  }
  return $result;
}

/**
 * Set progress start time.
 */
function progress_set_progress_start($name, $start) {
  return db_update('progress')
    ->fields([
    'start_stamp' => $start,
  ])
    ->condition('name', $name)
    ->execute();
}

/**
 * Set intervalled progress.
 */
function progress_set_intervalled_progress($name, $message, $progress, $interval = NULL) {
  static $set = [];
  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.
 */
function progress_get_progress($name) {
  $result = db_query("SELECT name, progress, message, start_stamp, end_stamp, current_stamp FROM {progress} WHERE name = :name", [
    ':name' => $name,
  ])
    ->fetchObject();
  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).
 */
function progress_end_progress($name, $message) {
  $current = microtime(TRUE);
  return db_update('progress')
    ->fields([
    'progress' => 1,
    'message' => $message,
    'end_stamp' => $current,
  ])
    ->condition('name', $name)
    ->execute();
}

/**
 * Remove a progress.
 */
function progress_remove_progress($name) {
  return db_delete('progress')
    ->condition('name', $name)
    ->execute();
}

/**
 * Get all progresses.
 */
function progress_get_progresses() {
  $result = db_select('progress', 'p')
    ->fields('p', [
    'name',
    'progress',
    'message',
    'start_stamp',
    'end_stamp',
    'current_stamp',
  ])
    ->execute();
  $progresses = [];
  foreach ($result as $progress) {
    $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.
 */
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;
}

/**
 * Implements hook cron.
 */
function progress_cron() {
  return db_delete('progress')
    ->condition('end_stamp', 0, '>')
    ->condition('end_stamp', time() - \Drupal::config('progress.settings')
    ->get('progress_age'), '<')
    ->execute();
}

/**
 * Implements hook_cronapi().
 */
function progress_cronapi($op, $job = NULL) {
  switch ($op) {
    case 'list':
      return [
        'progress_cron' => t('Cleanup old progresses'),
      ];
  }
}

Functions

Namesort descending Description
progress_cron Implements hook cron.
progress_cronapi Implements 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.
progress_set_progress Set progress.
progress_set_progress_start Set progress start time.

Constants

Namesort descending Description
PROGRESS_AGE Define Default Value for Progress Age.