/** * Task that rebuilds all dirty images. * @param Task_Model the task */ static function rebuild_dirty_images($task) { $errors = array(); try { $result = graphics::find_dirty_images_query()->select("id")->execute(); $total_count = $task->get("total_count", $result->count()); $mode = $task->get("mode", "init"); if ($mode == "init") { $task->set("total_count", $total_count); $task->set("mode", "process"); batch::start(); } $completed = $task->get("completed", 0); $ignored = $task->get("ignored", array()); $i = 0; foreach ($result as $row) { if (array_key_exists($row->id, $ignored)) { continue; } $item = ORM::factory("item", $row->id); if ($item->loaded()) { try { graphics::generate($item); $completed++; $errors[] = t("Successfully rebuilt images for '%title'", array("title" => html::purify($item->title))); } catch (Exception $e) { $errors[] = t("Unable to rebuild images for '%title'", array("title" => html::purify($item->title))); $errors[] = (string) $e; $ignored[$item->id] = 1; } } if (++$i == 2) { break; } } $task->status = t2("Updated: 1 image. Total: %total_count.", "Updated: %count images. Total: %total_count.", $completed, array("total_count" => $total_count)); if ($completed < $total_count) { $task->percent_complete = (int) (100 * ($completed + count($ignored)) / $total_count); } else { $task->percent_complete = 100; } $task->set("completed", $completed); $task->set("ignored", $ignored); if ($task->percent_complete == 100) { $task->done = true; $task->state = "success"; batch::stop(); site_status::clear("graphics_dirty"); } } catch (Exception $e) { Kohana_Log::add("error", (string) $e); $task->done = true; $task->state = "error"; $task->status = $e->getMessage(); $errors[] = (string) $e; } if ($errors) { $task->log($errors); } }
/** * Task that rebuilds all dirty images. * @param Task_Model the task */ static function rebuild_dirty_images($task) { $result = graphics::find_dirty_images_query(); $remaining = $result->count(); $completed = $task->get("completed", 0); $i = 0; foreach ($result as $row) { $item = ORM::factory("item", $row->id); if ($item->loaded) { graphics::generate($item); } $completed++; $remaining--; if (++$i == 2) { break; } } $task->status = t2("Updated: 1 image. Total: %total_count.", "Updated: %count images. Total: %total_count.", $completed, array("total_count" => $remaining + $completed)); if ($completed + $remaining > 0) { $task->percent_complete = (int) (100 * $completed / ($completed + $remaining)); } else { $task->percent_complete = 100; } $task->set("completed", $completed); if ($remaining == 0) { $task->done = true; $task->state = "success"; site_status::clear("graphics_dirty"); } }
/** * Task that rebuilds all dirty images. * @param Task_Model the task */ static function rebuild_dirty_images($task) { $errors = array(); try { $result = graphics::find_dirty_images_query(); $completed = $task->get("completed", 0); $ignored = $task->get("ignored", array()); $remaining = $result->count() - count($ignored); $i = 0; foreach ($result as $row) { if (array_key_exists($row->id, $ignored)) { continue; } $item = ORM::factory("item", $row->id); if ($item->loaded) { try { graphics::generate($item); $ignored[$item->id] = 1; $errors[] = t("Successfully rebuilt images for '%title'", array("title" => html::purify($item->title))); } catch (Exception $e) { $errors[] = t("Unable to rebuild images for '%title'", array("title" => html::purify($item->title))); $errors[] = $e->__toString(); } } $completed++; $remaining--; if (++$i == 2) { break; } } $task->status = t2("Updated: 1 image. Total: %total_count.", "Updated: %count images. Total: %total_count.", $completed, array("total_count" => $remaining + $completed)); if ($completed + $remaining > 0) { $task->percent_complete = (int) (100 * $completed / ($completed + $remaining)); } else { $task->percent_complete = 100; } $task->set("completed", $completed); $task->set("ignored", $ignored); if ($remaining == 0) { $task->done = true; $task->state = "success"; site_status::clear("graphics_dirty"); } } catch (Exception $e) { $task->done = true; $task->state = "error"; $task->status = $e->getMessage(); $errors[] = $e->__toString(); } if ($errors) { $task->log($errors); } }
/** * Mark thumbnails and resizes as dirty. They will have to be rebuilt. */ static function mark_dirty($thumbs, $resizes) { if ($thumbs || $resizes) { $db = db::build()->update("items"); if ($thumbs) { $db->set("thumb_dirty", 1); } if ($resizes) { $db->set("resize_dirty", 1); } $db->execute(); } $count = graphics::find_dirty_images_query()->count_records(); if ($count) { site_status::warning(t2("One of your photos is out of date. <a %attrs>Click here to fix it</a>", "%count of your photos are out of date. <a %attrs>Click here to fix them</a>", $count, array("attrs" => html::mark_clean(sprintf('href="%s" class="g-dialog-link"', url::site("admin/maintenance/start/gallery_task::rebuild_dirty_images?csrf=__CSRF__"))))), "graphics_dirty"); } }
/** * Task that rebuilds all dirty images. * @param Task_Model the task */ static function rebuild_dirty_images($task) { $errors = array(); try { // Choose the dirty images in a random order so that if we run this task multiple times // concurrently each task is rebuilding different images simultaneously. $result = graphics::find_dirty_images_query()->select("id")->select(db::expr("RAND() as r"))->order_by("r", "ASC")->execute(); $total_count = $task->get("total_count", $result->count()); $mode = $task->get("mode", "init"); if ($mode == "init") { $task->set("total_count", $total_count); $task->set("mode", "process"); batch::start(); } $completed = $task->get("completed", 0); $ignored = $task->get("ignored", array()); $i = 0; // If there's no work left to do, skip to the end. This can happen if we resume a task long // after the work got done in some other task. if (!$result->count()) { $completed = $total_count; } foreach ($result as $row) { if (array_key_exists($row->id, $ignored)) { continue; } $item = ORM::factory("item", $row->id); if ($item->loaded()) { try { graphics::generate($item); $completed++; $errors[] = t("Successfully rebuilt images for '%title'", array("title" => html::purify($item->title))); } catch (Exception $e) { $errors[] = t("Unable to rebuild images for '%title'", array("title" => html::purify($item->title))); $errors[] = (string) $e; $ignored[$item->id] = 1; } } if (++$i == 2) { break; } } $task->status = t2("Updated: 1 image. Total: %total_count.", "Updated: %count images. Total: %total_count.", $completed, array("total_count" => $total_count)); if ($completed < $total_count) { $task->percent_complete = (int) (100 * ($completed + count($ignored)) / $total_count); } else { $task->percent_complete = 100; } $task->set("completed", $completed); $task->set("ignored", $ignored); if ($task->percent_complete == 100) { $task->done = true; $task->state = "success"; batch::stop(); site_status::clear("graphics_dirty"); } } catch (Exception $e) { Kohana_Log::add("error", (string) $e); $task->done = true; $task->state = "error"; $task->status = $e->getMessage(); $errors[] = (string) $e; } if ($errors) { $task->log($errors); } }