Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #108: add global setting to disable realtime processing #109

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ Rules can be processed by two mechanisms:
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.
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.

### Disabling realtime processing (processing on event)

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.

# Configuration

## Prerequisites
Expand Down
9 changes: 6 additions & 3 deletions classes/observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ class observer {
* @param base $event The event.
*/
public static function process_event(base $event): void {
foreach (condition_manager::get_conditions_with_event($event) as $condition) {
foreach (rule_manager::get_rules_with_condition($condition) as $rule) {
rule_manager::process_rule($rule, self::get_userid_from_event($event));
// Check if realtime processing enabled globally.
if (get_config('tool_dynamic_cohorts', 'realtime')) {
foreach (condition_manager::get_conditions_with_event($event) as $condition) {
foreach (rule_manager::get_rules_with_condition($condition) as $rule) {
rule_manager::process_rule($rule, self::get_userid_from_event($event));
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions lang/en/tool_dynamic_cohorts.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@
$string['rule_entity.description'] = 'Description';
$string['rule_entity.bulkprocessing'] = 'Bulk processing';
$string['rule_entity.status'] = 'Status';
$string['settings:realtime'] = 'Real time processing';
$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.';
$string['settings:releasemembers'] = 'Release members';
$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.';
$string['usercreated'] = 'User was created';
Expand Down
6 changes: 6 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
new lang_string('settings:releasemembers_desc', 'tool_dynamic_cohorts'),
0
));
$settings->add(new admin_setting_configcheckbox(
'tool_dynamic_cohorts/realtime',
new lang_string('settings:realtime', 'tool_dynamic_cohorts'),
new lang_string('settings:realtime_desc', 'tool_dynamic_cohorts'),
1
));
$ADMIN->add('tool_dynamic_cohorts', $settings);
}

Expand Down
35 changes: 35 additions & 0 deletions tests/observer_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,39 @@ public function test_user_updating_triggers_rule_processing() {
user_update_user($user2, false);
$this->assertEquals(2, $DB->count_records('cohort_members', ['cohortid' => $this->cohort->id]));
}

/**
* Test that user creation event doesn't trigger rule processing for that user if realtime processing is disabled globally.
*/
public function test_realtime_rule_processing_when_disabled_globally() {
global $DB;

set_config('realtime', 0, 'tool_dynamic_cohorts');

$rule = new rule(0, (object)['name' => 'Test rule 1', 'enabled' => 1, 'cohortid' => $this->cohort->id]);
$rule->save();

$condition = condition_base::get_instance(0, (object)[
'classname' => 'tool_dynamic_cohorts\local\tool_dynamic_cohorts\condition\user_profile',
]);

// Condition username starts with user to catch both users.
$condition->set_config_data([
'profilefield' => 'username',
'username_operator' => user_profile::TEXT_STARTS_WITH,
'username_value' => 'user',
]);

$record = $condition->get_record();
$record->set('ruleid', $rule->get('id'));
$record->set('sortorder', 0);
$record->save();

$this->assertEquals(0, $DB->count_records('cohort_members', ['cohortid' => $this->cohort->id]));

$this->getDataGenerator()->create_user(['username' => 'user1']);
$this->getDataGenerator()->create_user(['username' => 'user2']);

$this->assertEquals(0, $DB->count_records('cohort_members', ['cohortid' => $this->cohort->id]));
}
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

$plugin->component = 'tool_dynamic_cohorts';
$plugin->release = 2024051001;
$plugin->version = 2024051001;
$plugin->version = 2024051003;
$plugin->requires = 2022112800;
$plugin->supported = [401, 403];
$plugin->maturity = MATURITY_STABLE;
Loading