-
-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add queue task to purge inactive users
- Loading branch information
Showing
5 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
return new class() extends UpgradeScript { | ||
public function run(): void | ||
{ | ||
$this->runMigrations(); | ||
|
||
PurgeInactiveUsers::schedule(new Language('core', 'en_UK')); | ||
|
||
$this->setVersion('2.2.0'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
class PurgeInactiveUsers extends Task { | ||
|
||
public function run(): string { | ||
$language = $this->_container->get(Language::class); | ||
|
||
$errors = []; | ||
|
||
$cutoff = (int) Settings::get('purge_inactive_users_cutoff', '0'); | ||
if (!$cutoff) { | ||
$result = ['cutoff' => $cutoff, 'total' => 'Task disabled.']; | ||
} else { | ||
$cutoff_timestamp = strtotime("$cutoff days ago"); | ||
$total = DB::getInstance()->query( | ||
'SELECT COUNT(*) c FROM nl2_users WHERE active = 0 AND joined < ?', | ||
[$cutoff_timestamp] | ||
)->first()->c; | ||
|
||
if ($total) { | ||
$users = DB::getInstance()->query( | ||
'SELECT id FROM nl2_users WHERE active = 0 AND joined < ?', | ||
[$cutoff_timestamp] | ||
)->results(); | ||
|
||
foreach ($users as $user) { | ||
EventHandler::executeEvent(new UserDeletedEvent(new User($user->id))); | ||
} | ||
} | ||
|
||
$result = [ | ||
'cutoff' => $cutoff, | ||
'cutoff_timestamp' => $cutoff_timestamp, | ||
'total' => $total, | ||
'users' => $users ?? [], | ||
]; | ||
} | ||
|
||
$this->setOutput(['result' => $result, 'errors' => $errors]); | ||
$this->reschedule($language); | ||
|
||
return Task::STATUS_COMPLETED; | ||
} | ||
|
||
private function reschedule(Language $language) { | ||
Queue::schedule((new PurgeInactiveUsers())->fromNew( | ||
Module::getIdFromName('Core'), | ||
$language->get('admin', 'purge_inactive_users'), | ||
[], | ||
Date::next()->getTimestamp() | ||
)); | ||
} | ||
|
||
public static function schedule(Language $language) { | ||
(new self())->reschedule($language); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters