You are here

hosting_task_gc.module in Hosting 7.4

Same filename and directory in other branches
  1. 7.3 task_gc/hosting_task_gc.module

Module code for Hosting task garbage collection

File

task_gc/hosting_task_gc.module
View source
<?php

/**
 * @file
 * Module code for Hosting task garbage collection
 */

/**
 * Implements hook_hosting_queues().
 *
 * Return a list of queues that this module needs to manage.
 */
function hosting_task_gc_hosting_queues() {
  $count = hosting_task_gc_count_sites();
  $queue['task_gc'] = array(
    'name' => t('Task GC'),
    'description' => t('Process the garbage collection of task logs.'),
    // Run queue sequentially. always with the same parameters.
    'type' => HOSTING_QUEUE_TYPE_BATCH,
    'frequency' => strtotime("1 hour", 0),
    'items' => 20,
    'total_items' => $count,
    'min_threads' => 6,
    'max_threads' => 12,
    'threshold' => 100,
    'singular' => t('site'),
    'plural' => t('sites'),
  );
  return $queue;
}

/**
 * The main queue callback task_gc.
 */
function hosting_task_gc_queue() {
  global $user;
  $old_user = $user;
  $user = user_load(1);
  $result = hosting_task_gc_list_sites();
  while ($site = $result
    ->fetchObject()) {
    $query = "SELECT nid FROM {hosting_task} WHERE rid = :nid";
    $tasks_to_remove = db_query($query, array(
      ':nid' => $site->nid,
    ));
    while ($row = $tasks_to_remove
      ->fetchObject()) {
      node_delete($row->nid);
      watchdog('hosting_task_gc', 'Deleted task node with nid @nid.', array(
        '@nid' => $row->nid,
      ));
    }
  }
  $user = $old_user;

  // Look for orphaned task log entries.
  $query = "SELECT DISTINCT h.vid\n              FROM {hosting_task_log} h\n                LEFT OUTER JOIN {node_revision} n ON h.vid = n.vid\n              WHERE n.vid IS NULL LIMIT 100";
  $result = db_query($query);
  while ($revision = $result
    ->fetchObject()) {
    $num = db_delete('hosting_task_log')
      ->condition('vid', $revision->vid)
      ->execute();
    watchdog('hosting_task_gc', 'Deleted @num orphaned task log entries with vid @vid.', array(
      '@num' => $num,
      '@vid' => $revision->vid,
    ));
  }
}

/**
 * Prepare a list of sites with garbage to collect.
 *
 * @return DatabaseStatementInterface
 *   A result object.
 */
function hosting_task_gc_list_sites() {
  $query = "SELECT DISTINCT s.nid " . "FROM {hosting_site} s INNER JOIN {hosting_task} t ON s.nid = t.rid " . "WHERE s.status = :status LIMIT 5";
  return db_query($query, array(
    ':status' => HOSTING_SITE_DELETED,
  ));
}

/**
 * Get the number of sites with garbage to collect.
 *
 * @return int
 *   The number of sites.
 */
function hosting_task_gc_count_sites() {
  $query = "SELECT COUNT(DISTINCT s.nid) AS num_sites " . "FROM {hosting_site} s INNER JOIN {hosting_task} t ON s.nid = t.rid " . "WHERE s.status = :status";
  return db_query($query, array(
    ':status' => HOSTING_SITE_DELETED,
  ))
    ->fetchField();
}

Functions

Namesort descending Description
hosting_task_gc_count_sites Get the number of sites with garbage to collect.
hosting_task_gc_hosting_queues Implements hook_hosting_queues().
hosting_task_gc_list_sites Prepare a list of sites with garbage to collect.
hosting_task_gc_queue The main queue callback task_gc.