Skip to content

Commit c82d120

Browse files
committed
issue #108: add global setting to disable realtime processing
1 parent 8609e69 commit c82d120

File tree

6 files changed

+54
-4
lines changed

6 files changed

+54
-4
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ Rules can be processed by two mechanisms:
8383
1. By cron: When a rule is created or updated, there may be many users that need to be added or removed from a cohort. This process is handled by cron, and depending on how many users are matched by a rule, this process can take some time. For rules matching large sets of users, some [configuration options](#rule-processing-options) are provided which may be useful to server administrators.
8484
2. By event: rules may also listen to certain events. When one of these events triggers, appropriate rules will be checked and the user will be added to the appropriate cohort immediately. For example, the User standard profile field rule listens to the "User created" and "User updated" events.
8585

86+
### Disabling realtime processing (processing on event)
87+
88+
There is a global admin setting that allows administrator to enable or disable realtime rule processing. This may be useful if processing taking too long and blocking the user interface.
89+
8690
# Configuration
8791

8892
## Prerequisites

classes/observer.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ class observer {
3333
* @param base $event The event.
3434
*/
3535
public static function process_event(base $event): void {
36-
foreach (condition_manager::get_conditions_with_event($event) as $condition) {
37-
foreach (rule_manager::get_rules_with_condition($condition) as $rule) {
38-
rule_manager::process_rule($rule, self::get_userid_from_event($event));
36+
// Check if realtime processing enabled globally.
37+
if (get_config('tool_dynamic_cohorts', 'realtime')) {
38+
foreach (condition_manager::get_conditions_with_event($event) as $condition) {
39+
foreach (rule_manager::get_rules_with_condition($condition) as $rule) {
40+
rule_manager::process_rule($rule, self::get_userid_from_event($event));
41+
}
3942
}
4043
}
4144
}

lang/en/tool_dynamic_cohorts.php

+2
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@
147147
$string['rule_entity.description'] = 'Description';
148148
$string['rule_entity.bulkprocessing'] = 'Bulk processing';
149149
$string['rule_entity.status'] = 'Status';
150+
$string['settings:realtime'] = 'Real time processing';
151+
$string['settings:realtime_desc'] = 'When enabled, rules with conditions that support triggering on the event will be processed synchronously as part of the event. Use caution when enabling as long running rule processing will block the user interface.';
150152
$string['settings:releasemembers'] = 'Release members';
151153
$string['settings:releasemembers_desc'] = 'If enabled all members will be removed from a cohort once it\'s not managed by the plugin (e.g a rule is deleted or cohort for a rule is changed). <br/> Please note: no cohort_member_removed events will be triggered when members are released from a cohort.';
152154
$string['usercreated'] = 'User was created';

settings.php

+6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434
new lang_string('settings:releasemembers_desc', 'tool_dynamic_cohorts'),
3535
0
3636
));
37+
$settings->add(new admin_setting_configcheckbox(
38+
'tool_dynamic_cohorts/realtime',
39+
new lang_string('settings:realtime', 'tool_dynamic_cohorts'),
40+
new lang_string('settings:realtime_desc', 'tool_dynamic_cohorts'),
41+
1
42+
));
3743
$ADMIN->add('tool_dynamic_cohorts', $settings);
3844
}
3945

tests/observer_test.php

+35
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,39 @@ public function test_user_updating_triggers_rule_processing() {
114114
user_update_user($user2, false);
115115
$this->assertEquals(2, $DB->count_records('cohort_members', ['cohortid' => $this->cohort->id]));
116116
}
117+
118+
/**
119+
* Test that user creation event doesn't trigger rule processing for that user if realtime processing is disabled globally.
120+
*/
121+
public function test_realtime_rule_processing_when_disabled_globally() {
122+
global $DB;
123+
124+
set_config('realtime', 0, 'tool_dynamic_cohorts');
125+
126+
$rule = new rule(0, (object)['name' => 'Test rule 1', 'enabled' => 1, 'cohortid' => $this->cohort->id]);
127+
$rule->save();
128+
129+
$condition = condition_base::get_instance(0, (object)[
130+
'classname' => 'tool_dynamic_cohorts\local\tool_dynamic_cohorts\condition\user_profile',
131+
]);
132+
133+
// Condition username starts with user to catch both users.
134+
$condition->set_config_data([
135+
'profilefield' => 'username',
136+
'username_operator' => user_profile::TEXT_STARTS_WITH,
137+
'username_value' => 'user',
138+
]);
139+
140+
$record = $condition->get_record();
141+
$record->set('ruleid', $rule->get('id'));
142+
$record->set('sortorder', 0);
143+
$record->save();
144+
145+
$this->assertEquals(0, $DB->count_records('cohort_members', ['cohortid' => $this->cohort->id]));
146+
147+
$this->getDataGenerator()->create_user(['username' => 'user1']);
148+
$this->getDataGenerator()->create_user(['username' => 'user2']);
149+
150+
$this->assertEquals(0, $DB->count_records('cohort_members', ['cohortid' => $this->cohort->id]));
151+
}
117152
}

version.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
$plugin->component = 'tool_dynamic_cohorts';
2828
$plugin->release = 2024051001;
29-
$plugin->version = 2024051001;
29+
$plugin->version = 2024051003;
3030
$plugin->requires = 2022112800;
3131
$plugin->supported = [401, 403];
3232
$plugin->maturity = MATURITY_STABLE;

0 commit comments

Comments
 (0)