Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-70382

Improving adhoc tasks management

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Duplicate
    • Affects Version/s: 3.9.3, 3.10
    • Fix Version/s: None
    • Component/s: Tasks
    • Affected Branches:
      MOODLE_310_STABLE, MOODLE_39_STABLE

      Description

      Hello everyone,

      Unfortunately, some adhoc tasks generate some unavoidable slow queries, for example: process_data_request_task which generates huge slow queries on large DBs.

      I had to make a quickfix to launch the adhoc tasks only in the evening or at night.

      I doubt it would be a great idea for the community as some adhoc tasks need to be executed quite quickly.
      However, I still share the idea and the quickfix:

      • adding a parameter to run adhoc tasks only during a schedule (at night)
      • added a parameter to say when this schedule starts
      • added a parameter to say when this schedule ends
      • use of these parameters in the adhoc job function

      Quickfix :

      diff --git a/admin/settings/server.php b/admin/settings/server.php
      index c1b20560e0..a0b478b21e 100644
      --- a/admin/settings/server.php
      +++ b/admin/settings/server.php
      @@ -253,6 +253,34 @@ $temp->add(
               30 * MINSECS
           )
       );
      +    $temp->add(
      +        new admin_setting_configcheckbox(
      +            'task_adhoc_only_evening',
      +            new lang_string('task_adhoc_only_evening', 'admin'),
      +            new lang_string('task_adhoc_only_evening_desc', 'admin'),
      +            true
      +        )
      +    );
      +    $temp->add(
      +        new admin_setting_configselect(
      +            'task_adhoc_only_evening_start',
      +            new lang_string('task_adhoc_only_evening_start', 'admin'),
      +            new lang_string('task_adhoc_only_evening_start_desc', 'admin'),
      +            20,
      +            range(0, 23)
      +        )
      +    );
      +    $temp->add(
      +        new admin_setting_configselect(
      +            'task_adhoc_only_evening_end',
      +            new lang_string('task_adhoc_only_evening_end', 'admin'),
      +            new lang_string('task_adhoc_only_evening_end_desc', 'admin'),
      +            7,
      +            range(0, 23)
      +        )
      +    );
       $ADMIN->add('taskconfig', $temp);
       
       $temp = new admin_settingpage('tasklogging', new lang_string('tasklogging','admin'));
      diff --git a/lang/en/admin.php b/lang/en/admin.php
      index 408071297f..77b6b89cbc 100644
      --- a/lang/en/admin.php
      +++ b/lang/en/admin.php
      @@ -1476,3 +1476,12 @@ $string['requestcategoryselection'] = 'Enable category selection';
       // Deprecated since Moodle 3.9.
       $string['availablelicenses'] = 'Available licences';
       $string['managelicenses'] = 'Manage licences';
      +
      +$string['task_adhoc_only_evening'] = 'Execute adhoc tasks only in the evening';
      +$string['task_adhoc_only_evening_desc'] = 'Execute adhoc tasks only in the evening to avoid slow queries and server load during the day';
      +$string['task_adhoc_only_evening_start'] = 'From (hour)';
      +$string['task_adhoc_only_evening_start_desc'] = 'Choose an hour from which adhoc tasks can be executed (if task_adhoc_only_evening is set)';
      +$string['task_adhoc_only_evening_end'] = 'To (hour)';
      +$string['task_adhoc_only_evening_end_desc'] = 'Choose an hour up to which adhoc tasks can be executed (if task_adhoc_only_evening is set)';
      diff --git a/lib/cronlib.php b/lib/cronlib.php
      index 88c7463a9c..3caba69d39 100644
      --- a/lib/cronlib.php
      +++ b/lib/cronlib.php
      @@ -144,6 +144,19 @@ function cron_run_adhoc_tasks(int $timenow, $keepalive = 0, $checklimits = true)
           $cronlockfactory = \core\lock\lock_config::get_lock_factory('cron');
           $maxruns = get_config('core', 'task_adhoc_concurrency_limit');
           $maxruntime = get_config('core', 'task_adhoc_max_runtime');
      +    $only_evening = get_config('core', 'task_adhoc_only_evening');
      +    if (!empty($only_evening)) {
      +        $only_evening_start = get_config('core', 'task_adhoc_only_evening_start');
      +        $only_evening_end = get_config('core', 'task_adhoc_only_evening_end');
      +        $hour = date('H');
      +        if ($hour >= $only_evening_start || $hour < $only_evening_end) {
      +            // OK
      +        } else {
      +            return;
      +        }
      +    }
       
           if ($checklimits) {
               $adhoclock = null;
      

      Olivier

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              Trolli Olivier Piton
              Participants:
              Component watchers:
              Amaia Anabitarte, Carlos Escobedo, Ferran Recio, Ilya Tregubov, Sara Arjona (@sarjona)
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: