Skip to content

Commit b000649

Browse files
committed
feat: Enhance LifecycleService with detailed logging and lifecycle source management
1 parent ef92b6f commit b000649

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

apps/api/src/management/lifecycle/lifecycle.service.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,31 @@ export class LifecycleService extends AbstractServiceSchema implements OnApplica
108108
* It also logs the lifecycle sources for debugging purposes.
109109
*/
110110
public async onApplicationBootstrap(): Promise<void> {
111-
if (isConsoleEntrypoint) {
112-
this.logger.debug('Skipping LifecycleService bootstrap in console mode.');
113-
return;
114-
}
115-
116111
this.logger.verbose('Bootstrap LifecycleService application...');
117112

118113
const lifecycleRules = await this.loadLifecycleRules();
119114
await this.loadCustomStates();
120115

116+
for (const lfr of lifecycleRules) {
117+
for (const idRule of lfr.identities) {
118+
for (const source of idRule.sources) {
119+
if (!this.lifecycleSources[source]) {
120+
this.lifecycleSources[source] = [];
121+
}
122+
this.lifecycleSources[source].push(idRule);
123+
}
124+
}
125+
}
126+
127+
this.logger.debug('Lifecycle sources loaded:', JSON.stringify(this.lifecycleSources, null, 2));
128+
129+
console.log('lifecycleRules:', lifecycleRules);
130+
131+
if (isConsoleEntrypoint) {
132+
this.logger.debug('Skipping LifecycleService bootstrap in console mode.');
133+
return;
134+
}
135+
121136
const cronExpression = this.configService.get<string>('lifecycle.triggerCronExpression') || '*/5 * * * *';
122137
const job = new CronJob(cronExpression, this.handleCron.bind(this, { lifecycleRules }));
123138
this.schedulerRegistry.addCronJob(`lifecycle-trigger`, job);
@@ -135,10 +150,16 @@ export class LifecycleService extends AbstractServiceSchema implements OnApplica
135150
this.logger.log('LifecycleService bootstraped');
136151
}
137152

153+
public async listLifecycles(): Promise<any> {
154+
const lifecycles = this.lifecycleSources ? Object.keys(this.lifecycleSources) : [];
155+
return this.lifecycleSources;
156+
}
157+
138158
private async handleCron({ lifecycleRules }: { lifecycleRules: ConfigRulesObjectSchemaDTO[] }): Promise<void> {
139159
this.logger.debug(`Running lifecycle trigger cron job...`);
140160

141161
for (const lfr of lifecycleRules) {
162+
console.log('Processing lifecycle rule:', JSON.stringify(lfr, null, 2));
142163
for (const idRule of lfr.identities) {
143164
if (idRule.trigger) {
144165
const dateKey = idRule.dateKey || 'lastLifecycleUpdate';
@@ -509,6 +530,8 @@ export class LifecycleService extends AbstractServiceSchema implements OnApplica
509530
// If the lifecycle has changed, we need to process the new lifecycle
510531
}
511532

533+
console.log('AH', after.lifecycle, this.lifecycleSources);
534+
512535
if (this.lifecycleSources[after.lifecycle]) {
513536
this.logger.debug(`Processing lifecycle sources for identity <${after._id}> with lifecycle <${after.lifecycle}>`);
514537

@@ -520,6 +543,13 @@ export class LifecycleService extends AbstractServiceSchema implements OnApplica
520543
continue; // Skip processing if it's a trigger-based rule
521544
}
522545

546+
console.log('LCS',
547+
{
548+
...lcs.rules,
549+
_id: after._id,
550+
ignoreLifecycle: { $ne: true },
551+
},);
552+
523553
const res = await this.identitiesService.model.findOneAndUpdate(
524554
{
525555
...lcs.rules,
@@ -537,6 +567,7 @@ export class LifecycleService extends AbstractServiceSchema implements OnApplica
537567
upsert: false, // Do not create a new document if no match is found
538568
}
539569
);
570+
console.log('RES', res);
540571

541572
if (!res) {
542573
this.logger.debug(`No identity found matching rules for lifecycle <${after.lifecycle}>`);

0 commit comments

Comments
 (0)