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

Improve users_build_cache_tree scheduling. #4449

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
32 changes: 20 additions & 12 deletions scripts/background_tasks___user_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,37 @@
// log start
$logID = doLog('start', 'user_task', (isset($SETTINGS['enable_tasks_log']) === true ? (int) $SETTINGS['enable_tasks_log'] : 0));

// Number of users on each scheduler run
$number_users_build_cache_tree = $SETTINGS['number_users_build_cache_tree'] ?? 0;
// Never less than 10
$number_users_build_cache_tree = max((int) $SETTINGS['number_users_build_cache_tree'] ?? 0, 10);

DB::debugmode(false);
$rows = DB::query(
'SELECT *
FROM ' . prefixTable('background_tasks') . '
WHERE is_in_progress = %i AND process_type = %s
ORDER BY increment_id ASC LIMIT 0,' . $number_users_build_cache_tree,
0,
'user_build_cache_tree'
);
foreach ($rows as $record) {

for ($i = 0; $i < $number_users_build_cache_tree; $i++) {

// Get one task at a time so we don't duplicate them if the scheduler is
// launched several times.
$record = DB::queryFirstRow(
'SELECT *
FROM ' . prefixTable('background_tasks') . '
WHERE is_in_progress = %i AND process_type = %s
ORDER BY increment_id ASC LIMIT 1',
0,
'user_build_cache_tree'
);

// No more pending user_build_cache_tree tasks
if (DB::count() === 0)
exit;

// get email properties
$arguments = json_decode($record['arguments'], true);

// update DB - started_at
// Update started_at time and is_in_progress state
DB::update(
prefixTable('background_tasks'),
array(
'started_at' => time(),
'is_in_progress' => 1,
),
'increment_id = %i',
$record['increment_id']
Expand Down
25 changes: 22 additions & 3 deletions sources/folders.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,14 +418,33 @@ private function refreshCacheForUsersWithSimilarRoles($user_roles)
{
$usersWithSimilarRoles = getUsersWithRoles(explode(";", $user_roles));
foreach ($usersWithSimilarRoles as $user) {

// Arguments field
$arguments = json_encode([
'user_id' => (int) $user,
], JSON_HEX_QUOT | JSON_HEX_TAG);

// Search for existing job
$count = DB::queryFirstRow(
'SELECT COUNT(*) AS count
FROM ' . prefixTable('background_tasks') . '
WHERE is_in_progress = %i AND process_type = %s AND arguments = %s',
0,
'user_build_cache_tree',
$arguments
)['count'];

// Don't insert duplicates
if ($count > 0)
continue;

// Insert new background task
DB::insert(
prefixTable('background_tasks'),
array(
'created_at' => time(),
'process_type' => 'user_build_cache_tree',
'arguments' => json_encode([
'user_id' => (int) $user,
], JSON_HEX_QUOT | JSON_HEX_TAG),
'arguments' => $arguments,
'updated_at' => '',
'finished_at' => '',
'output' => '',
Expand Down