mirror of
https://github.com/Mibew/mibew.git
synced 2025-02-25 07:44:33 +03:00
Extract CronWorker from CronController
This commit is contained in:
parent
a4ce7ffea7
commit
ff09ea09cc
@ -19,8 +19,7 @@
|
|||||||
|
|
||||||
namespace Mibew\Controller;
|
namespace Mibew\Controller;
|
||||||
|
|
||||||
use Mibew\EventDispatcher\EventDispatcher;
|
use Mibew\Maintenance\CronWorker;
|
||||||
use Mibew\EventDispatcher\Events;
|
|
||||||
use Mibew\Settings;
|
use Mibew\Settings;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
|
||||||
@ -47,29 +46,27 @@ class CronController extends AbstractController
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Do the job.
|
||||||
|
$worker = new CronWorker($this->getCache());
|
||||||
|
$success = $worker->run();
|
||||||
|
|
||||||
// Determine use or not quiet mode
|
// Determine use or not quiet mode
|
||||||
$quiet = $request->query->has('q');
|
$quiet = $request->query->has('q');
|
||||||
|
|
||||||
set_time_limit(0);
|
|
||||||
|
|
||||||
// Remove stale cached items
|
|
||||||
$this->getCache()->purge();
|
|
||||||
|
|
||||||
// Run cron jobs of the core
|
|
||||||
calculate_thread_statistics();
|
|
||||||
calculate_operator_statistics();
|
|
||||||
calculate_page_statistics();
|
|
||||||
|
|
||||||
// Trigger cron event
|
|
||||||
$dispatcher = EventDispatcher::getInstance();
|
|
||||||
$dispatcher->triggerEvent(Events::CRON_RUN);
|
|
||||||
|
|
||||||
// Update time of last cron run
|
|
||||||
Settings::set('_last_cron_run', time());
|
|
||||||
|
|
||||||
if (!$quiet) {
|
if (!$quiet) {
|
||||||
// TODO: May be localize it
|
if ($success) {
|
||||||
|
// Everything is fine.
|
||||||
return 'All cron jobs done.';
|
return 'All cron jobs done.';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prepare error message for system's error log.
|
||||||
|
$error_message = "Cron job failed. Here are the errors:\n";
|
||||||
|
foreach ($worker->getErrors() as $error) {
|
||||||
|
$error_message .= ' ' . $error . "\n";
|
||||||
|
}
|
||||||
|
trigger_error($error_message, E_USER_WARNING);
|
||||||
|
|
||||||
|
// Let the client know about the problem.
|
||||||
|
return 'Cron job failed.';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
117
src/mibew/libs/classes/Mibew/Maintenance/CronWorker.php
Normal file
117
src/mibew/libs/classes/Mibew/Maintenance/CronWorker.php
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is a part of Mibew Messenger.
|
||||||
|
*
|
||||||
|
* Copyright 2005-2015 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Mibew\Maintenance;
|
||||||
|
|
||||||
|
use Mibew\EventDispatcher\EventDispatcher;
|
||||||
|
use Mibew\EventDispatcher\Events;
|
||||||
|
use Mibew\Settings;
|
||||||
|
use Stash\Interfaces\PoolInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encapsulates periodical tasks runner.
|
||||||
|
*/
|
||||||
|
class CronWorker
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* An instance of cache pool.
|
||||||
|
*
|
||||||
|
* @var PoolInterface|null
|
||||||
|
*/
|
||||||
|
protected $cache = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List of errors.
|
||||||
|
*
|
||||||
|
* @var string[]
|
||||||
|
*/
|
||||||
|
protected $errors = array('asd', 'qwe');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List of log messages.
|
||||||
|
*
|
||||||
|
* @var string[]
|
||||||
|
*/
|
||||||
|
protected $log = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class constructor.
|
||||||
|
*
|
||||||
|
* @param PoolInterface $cache An instance of cache pool.
|
||||||
|
*/
|
||||||
|
public function __construct(PoolInterface $cache)
|
||||||
|
{
|
||||||
|
$this->cache = $cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs all periodical actions.
|
||||||
|
*
|
||||||
|
* @return boolean True if all periodical actions are done and false
|
||||||
|
* otherwise.
|
||||||
|
*/
|
||||||
|
public function run()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
set_time_limit(0);
|
||||||
|
|
||||||
|
// Remove stale cached items
|
||||||
|
$this->cache->purge();
|
||||||
|
|
||||||
|
// Run cron jobs of the core
|
||||||
|
calculate_thread_statistics();
|
||||||
|
calculate_operator_statistics();
|
||||||
|
calculate_page_statistics();
|
||||||
|
|
||||||
|
// Trigger cron event
|
||||||
|
$dispatcher = EventDispatcher::getInstance();
|
||||||
|
$dispatcher->triggerEvent(Events::CRON_RUN);
|
||||||
|
|
||||||
|
// Update time of last cron run
|
||||||
|
Settings::set('_last_cron_run', time());
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$this->log[] = $e->getMessage();
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retuns list of all errors that took place during running periodical
|
||||||
|
* actions.
|
||||||
|
*
|
||||||
|
* @return string[]
|
||||||
|
*/
|
||||||
|
public function getErrors()
|
||||||
|
{
|
||||||
|
return $this->errors;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns list of all information messages.
|
||||||
|
*
|
||||||
|
* @return string[]
|
||||||
|
*/
|
||||||
|
public function getLog()
|
||||||
|
{
|
||||||
|
return $this->log;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user