-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTenantDeprovisionJob.php
More file actions
102 lines (92 loc) · 3.25 KB
/
TenantDeprovisionJob.php
File metadata and controls
102 lines (92 loc) · 3.25 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
<?php
/**
* Tenant Deprovisioning Background Job
*
* Processes organisations in 'deprovisioning' state by soft-deleting their
* objects and transitioning them to 'archived' state.
*
* @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 OCA\OpenRegister\Db\OrganisationMapper;
use OCA\OpenRegister\Service\TenantLifecycleService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
use Psr\Log\LoggerInterface;
/**
* Processes deprovisioning organisations.
*
* @package OCA\OpenRegister\BackgroundJob
*/
class TenantDeprovisionJob extends TimedJob
{
/**
* Constructor
*
* @param ITimeFactory $time Time factory
* @param OrganisationMapper $organisationMapper Organisation mapper
* @param TenantLifecycleService $tenantLifecycleService Lifecycle service
* @param LoggerInterface $logger Logger
*/
public function __construct(
ITimeFactory $time,
private readonly OrganisationMapper $organisationMapper,
private readonly TenantLifecycleService $tenantLifecycleService,
private readonly LoggerInterface $logger
) {
parent::__construct(time: $time);
// Run every hour.
$this->setInterval(seconds: 3600);
}//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('[TenantDeprovisionJob] Starting deprovisioning check');
try {
$organisations = $this->organisationMapper->findAll(
filters: ['status' => TenantLifecycleService::STATUS_DEPROVISIONING]
);
} catch (\Exception $e) {
$this->logger->error(
'[TenantDeprovisionJob] Failed to query deprovisioning organisations',
['error' => $e->getMessage()]
);
return;
}
foreach ($organisations as $organisation) {
try {
// Transition to archived — actual data cleanup is handled by
// the purge job after the retention period expires.
$this->tenantLifecycleService->archive($organisation);
$this->logger->info(
'[TenantDeprovisionJob] Organisation archived',
['uuid' => $organisation->getUuid()]
);
} catch (\Exception $e) {
$this->logger->error(
'[TenantDeprovisionJob] Failed to archive organisation',
['uuid' => $organisation->getUuid(), 'error' => $e->getMessage()]
);
}
}
$this->logger->info(
'[TenantDeprovisionJob] Completed, processed '.count($organisations).' organisations'
);
}//end run()
}//end class