-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgroup_lms_user_sync.module
More file actions
61 lines (53 loc) · 1.79 KB
/
Copy pathgroup_lms_user_sync.module
File metadata and controls
61 lines (53 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
/**
* @file
* Allows you to group users, content and other entities.
*/
use Drupal\Component\Utility\Html;
use Drupal\Core\Session\AccountInterface;
use Drupal\group\Entity\GroupRelationshipInterface;
use Drupal\group\Entity\GroupInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
/**
* Implements hook_cron().
*
* Queues group sync tasks in batches to prevent memory exhaustion.
*/
function group_lms_user_sync_cron() {
// Get the GroupLMSUserSyncAPI service.
$sync_service = \Drupal::service('group_lms_user_sync.api');
// Get total groups and batch size.
$total_groups = $sync_service->getGroupCount();
$batch_size = $sync_service->getBatchSize();
if ($total_groups === 0) {
\Drupal::logger('group_lms_user_sync')->info('No groups to sync.');
return;
}
// Queue batches for processing.
$queue = \Drupal::queue('group_lms_user_sync_batch');
// Only queue if the queue is empty (prevent duplicate queuing).
if ($queue->numberOfItems() === 0) {
for ($offset = 0; $offset < $total_groups; $offset += $batch_size) {
$queue->createItem([
'offset' => $offset,
'limit' => $batch_size,
]);
}
\Drupal::logger('group_lms_user_sync')->info('Queued @count batches for @total groups (batch size: @size).', [
'@count' => ceil($total_groups / $batch_size),
'@total' => $total_groups,
'@size' => $batch_size,
]);
} else {
\Drupal::logger('group_lms_user_sync')->info('Sync queue already has @count items pending.', [
'@count' => $queue->numberOfItems(),
]);
}
}
/**
* Worker callback for the daily task.
*/
function group_lms_user_sync_daily_task_worker($data) {
}