Commit a6078c49 authored by Cecilia Vela Gurovic's avatar Cecilia Vela Gurovic

Bug 1840195: add function to check if a cron jobs needs to be executed

this adds the possibility to define a check to see if a
cron callfunction needs to be executed before droping
elasticsearch triggers or calling the cron function

if the check function is not define then the cron
call function will be executed as before

to define the check function it has to have the name
of the cron callfunction and add a 'needs_to_run'
at the end

+ also making the successful login reset the logintries
to 0 for the user on the DB

behatnotneeded

Change-Id: I0abe28c1172136da043c9e9310c315b75c7d4fbb
parent c388a220
......@@ -2599,6 +2599,14 @@ function reset_password($user, $resetpasswordchange=true, $quickhash=false) {
}
}
/*
* Check if we need to set any user login tries to 0
*/
function user_login_tries_to_zero_needs_to_run() {
return (bool)count_records_sql("SELECT COUNT(*) FROM {usr} WHERE logintries >= ?", array(MAXLOGINTRIES));
}
function user_login_tries_to_zero() {
execute_sql('UPDATE {usr} SET logintries = 0 WHERE logintries > 0');
}
......
......@@ -1939,6 +1939,8 @@ class LiveUser extends User {
$this->commit();
update_record('usr', (object) array('id' => $user->id, 'logintries' => 0));
// finally, after all is done, call the (maybe non existant) hook on their auth plugin
$authobj = AuthFactory::create($authinstance);
$authobj->login();
......
......@@ -582,6 +582,12 @@ EOF;
return $forums;
}
/*
* Check if we need to process new forum posts
*/
public static function interaction_forum_new_post_needs_to_run() {
return (bool)count_records_sql('SELECT COUNT(*) FROM {interaction_forum_post} WHERE sent = 0 AND deleted = 0 AND approved = 1');
}
/**
* Process new forum posts.
......
......@@ -102,27 +102,35 @@ foreach (plugin_types() as $plugintype) {
$classname = generate_class_name($plugintype, $job->plugin);
log_info("Running $classname::" . $job->callfunction);
safe_require($plugintype, $job->plugin, 'lib.php', 'require_once');
$droptriggers = in_array($job->callfunction, $jobsneeddroptriggers);
if ($droptriggers) {
drop_elasticsearch_triggers();
}
// check if the cron function needs to run on the DB
if (!method_exists($classname, $job->callfunction . '_needs_to_run') ||
call_static_method($classname, $job->callfunction . '_needs_to_run')) {
try {
call_static_method($classname, $job->callfunction);
}
catch (Exception $e) {
log_message($e->getMessage(), LOG_LEVEL_WARN, true, true, $e->getFile(), $e->getLine(), $e->getTrace());
$output = $e instanceof MaharaException ? $e->render_exception() : $e->getMessage();
echo "$output\n";
// Don't call handle_exception; try to update next run time and free the lock
}
log_info("Running $classname::" . $job->callfunction);
if ($droptriggers) {
create_elasticsearch_triggers();
$droptriggers = in_array($job->callfunction, $jobsneeddroptriggers);
if ($droptriggers) {
drop_elasticsearch_triggers();
}
try {
call_static_method($classname, $job->callfunction);
}
catch (Exception $e) {
log_message($e->getMessage(), LOG_LEVEL_WARN, true, true, $e->getFile(), $e->getLine(), $e->getTrace());
$output = $e instanceof MaharaException ? $e->render_exception() : $e->getMessage();
echo "$output\n";
// Don't call handle_exception; try to update next run time and free the lock
}
if ($droptriggers) {
create_elasticsearch_triggers();
}
}
else {
log_info("Skipping: No need to run $classname::" . $job->callfunction);
}
$nextrun = cron_next_run_time($start, (array)$job);
......@@ -173,27 +181,35 @@ if ($jobs) {
continue;
}
log_info("Running core cron " . $job->callfunction);
$function = $job->callfunction;
$droptriggers = in_array($job->callfunction, $jobsneeddroptriggers);
if ($droptriggers) {
drop_elasticsearch_triggers();
}
// check if the cron function needs to run on the DB
$checkfunction = $job->callfunction . '_needs_to_run';
try {
$function();
}
catch (Exception $e) {
log_message($e->getMessage(), LOG_LEVEL_WARN, true, true, $e->getFile(), $e->getLine(), $e->getTrace());
$output = $e instanceof MaharaException ? $e->render_exception() : $e->getMessage();
echo "$output\n";
// Don't call handle_exception; try to update next run time and free the lock
}
if (!function_exists($checkfunction) || $checkfunction()) {
log_info("Running core cron " . $job->callfunction);
$droptriggers = in_array($job->callfunction, $jobsneeddroptriggers);
if ($droptriggers) {
drop_elasticsearch_triggers();
}
if ($droptriggers) {
create_elasticsearch_triggers();
try {
$function();
}
catch (Exception $e) {
log_message($e->getMessage(), LOG_LEVEL_WARN, true, true, $e->getFile(), $e->getLine(), $e->getTrace());
$output = $e instanceof MaharaException ? $e->render_exception() : $e->getMessage();
echo "$output\n";
// Don't call handle_exception; try to update next run time and free the lock
}
if ($droptriggers) {
create_elasticsearch_triggers();
}
}
else {
log_info("Skipping: No need to run " . $job->callfunction);
}
$nextrun = cron_next_run_time($start, (array)$job);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment