-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTenantPurgeJob.php
More file actions
138 lines (120 loc) · 4.19 KB
/
TenantPurgeJob.php
File metadata and controls
138 lines (120 loc) · 4.19 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* Tenant Purge Background Job
*
* Permanently deletes archived organisations and their data after the
* configured retention period (default: 90 days).
*
* @category BackgroundJob
* @package OCA\OpenRegister\BackgroundJob
*
* @author Conduction Development Team <dev@conduction.nl>
* @copyright 2024 Conduction B.V.
* @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
*
* @link https://OpenRegister.app
*/
declare(strict_types=1);
namespace OCA\OpenRegister\BackgroundJob;
use DateTime;
use DateInterval;
use OCA\OpenRegister\Db\OrganisationMapper;
use OCA\OpenRegister\Db\TenantUsageMapper;
use OCA\OpenRegister\Service\TenantLifecycleService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
use OCP\IAppConfig;
use Psr\Log\LoggerInterface;
/**
* Purges archived organisations after retention period.
*
* @package OCA\OpenRegister\BackgroundJob
*/
class TenantPurgeJob extends TimedJob
{
/**
* Default retention period in days.
*/
private const DEFAULT_RETENTION_DAYS = 90;
/**
* Constructor
*
* @param ITimeFactory $time Time factory
* @param OrganisationMapper $organisationMapper Organisation mapper
* @param TenantUsageMapper $tenantUsageMapper Usage mapper
* @param IAppConfig $appConfig App config
* @param LoggerInterface $logger Logger
*/
public function __construct(
ITimeFactory $time,
private readonly OrganisationMapper $organisationMapper,
private readonly TenantUsageMapper $tenantUsageMapper,
private readonly IAppConfig $appConfig,
private readonly LoggerInterface $logger
) {
parent::__construct(time: $time);
// Run daily.
$this->setInterval(seconds: 86400);
}//end __construct()
/**
* Execute the background job.
*
* @param mixed $argument Job argument (unused)
*
* @return void
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
protected function run(mixed $argument): void
{
$this->logger->info('[TenantPurgeJob] Starting purge check');
$retentionDays = (int) $this->appConfig->getValueString(
'openregister',
'tenantRetentionDays',
(string) self::DEFAULT_RETENTION_DAYS
);
$cutoffDate = new DateTime();
$cutoffDate->sub(new DateInterval("P{$retentionDays}D"));
try {
$organisations = $this->organisationMapper->findAll(
filters: ['status' => TenantLifecycleService::STATUS_ARCHIVED]
);
} catch (\Exception $e) {
$this->logger->error(
'[TenantPurgeJob] Failed to query archived organisations',
['error' => $e->getMessage()]
);
return;
}
$purgedCount = 0;
foreach ($organisations as $organisation) {
$deprovisionedAt = $organisation->getDeprovisionedAt();
if ($deprovisionedAt === null) {
continue;
}
if ($deprovisionedAt > $cutoffDate) {
continue;
}
try {
$orgUuid = $organisation->getUuid();
// Delete usage records for this organisation.
$this->tenantUsageMapper->deleteOlderThan(new DateTime('2099-12-31'));
// Delete the organisation entity.
$this->organisationMapper->delete($organisation);
$this->logger->info(
'[TenantPurgeJob] Permanently deleted archived organisation',
['uuid' => $orgUuid, 'deprovisionedAt' => $deprovisionedAt->format('c')]
);
$purgedCount++;
} catch (\Exception $e) {
$this->logger->error(
'[TenantPurgeJob] Failed to purge organisation',
['uuid' => $organisation->getUuid(), 'error' => $e->getMessage()]
);
}//end try
}//end foreach
$this->logger->info(
'[TenantPurgeJob] Completed, purged '.$purgedCount.' organisations'
);
}//end run()
}//end class