Skip to content

Commit a3ac1b8

Browse files
committed
feat(files_reminders): add background jobs
Signed-off-by: Christopher Ng <[email protected]>
1 parent b75fac3 commit a3ac1b8

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright 2023 Christopher Ng <[email protected]>
7+
*
8+
* @author Christopher Ng <[email protected]>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCA\FilesReminders\BackgroundJob;
28+
29+
use OCA\FilesReminders\Service\ReminderService;
30+
use OCP\AppFramework\Utility\ITimeFactory;
31+
use OCP\BackgroundJob\IJob;
32+
use OCP\BackgroundJob\TimedJob;
33+
34+
class CleanUpReminders extends TimedJob {
35+
public function __construct(
36+
private ITimeFactory $time,
37+
private ReminderService $reminderService,
38+
) {
39+
parent::__construct($time);
40+
$this->setInterval(60 * 60 * 24);
41+
$this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
42+
}
43+
44+
/**
45+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
46+
*/
47+
protected function run($argument) {
48+
$this->reminderService->cleanUp(500);
49+
}
50+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright 2023 Christopher Ng <[email protected]>
7+
*
8+
* @author Christopher Ng <[email protected]>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCA\FilesReminders\BackgroundJob;
28+
29+
use OCA\FilesReminders\Db\ReminderMapper;
30+
use OCA\FilesReminders\Service\ReminderService;
31+
use OCP\AppFramework\Db\DoesNotExistException;
32+
use OCP\AppFramework\Utility\ITimeFactory;
33+
use OCP\BackgroundJob\Job;
34+
use Psr\Log\LoggerInterface;
35+
use Throwable;
36+
37+
class ScheduledNotifications extends Job {
38+
public function __construct(
39+
protected ITimeFactory $time,
40+
protected ReminderMapper $reminderMapper,
41+
protected ReminderService $reminderService,
42+
protected LoggerInterface $logger,
43+
) {
44+
parent::__construct($time);
45+
}
46+
47+
/**
48+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
49+
*/
50+
public function run($argument) {
51+
$reminders = $this->reminderMapper->findToRemind();
52+
foreach ($reminders as $reminder) {
53+
try {
54+
$this->reminderService->send($reminder);
55+
} catch (DoesNotExistException $e) {
56+
$this->logger->debug('Could not send notification for reminder with id ' . $reminder->getId());
57+
} catch (Throwable $th) {
58+
$this->logger->error($th->getMessage(), $th->getTrace());
59+
}
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)