Skip to content

Commit dbd0870

Browse files
committed
feat(kernel): add system requirements validation and service criticality definitions
1 parent 5752ec7 commit dbd0870

3 files changed

Lines changed: 82 additions & 0 deletions

File tree

packages/core/src/kernel.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Plugin, PluginContext } from './types.js';
22
import { createLogger, ObjectLogger } from './logger.js';
33
import type { LoggerConfig } from '@objectstack/spec/system';
4+
import { ServiceRequirementDef } from '@objectstack/spec/system';
45
import { PluginLoader, PluginMetadata, ServiceLifecycle, ServiceFactory, PluginStartupResult } from './plugin-loader.js';
56

67
/**
@@ -185,6 +186,44 @@ export class ObjectKernel {
185186
return this;
186187
}
187188

189+
/**
190+
* Validate Critical System Requirements
191+
*/
192+
private validateSystemRequirements() {
193+
this.logger.debug('Validating system service requirements...');
194+
const missingServices: string[] = [];
195+
const missingCoreServices: string[] = [];
196+
197+
// Iterate through all defined requirements
198+
for (const [serviceName, criticality] of Object.entries(ServiceRequirementDef)) {
199+
const hasService = this.services.has(serviceName) || this.pluginLoader.hasService(serviceName);
200+
201+
if (!hasService) {
202+
if (criticality === 'required') {
203+
this.logger.error(`CRITICAL: Required service missing: ${serviceName}`);
204+
missingServices.push(serviceName);
205+
} else if (criticality === 'core') {
206+
this.logger.warn(`CORE: Core service missing, functionality may be degraded: ${serviceName}`);
207+
missingCoreServices.push(serviceName);
208+
} else {
209+
this.logger.info(`Info: Optional service not present: ${serviceName}`);
210+
}
211+
}
212+
}
213+
214+
if (missingServices.length > 0) {
215+
const errorMsg = `System failed to start. Missing critical services: ${missingServices.join(', ')}`;
216+
this.logger.error(errorMsg);
217+
throw new Error(errorMsg);
218+
}
219+
220+
if (missingCoreServices.length > 0) {
221+
this.logger.warn(`System started with degraded capabilities. Missing core services: ${missingCoreServices.join(', ')}`);
222+
}
223+
224+
this.logger.info('System requirement check passed');
225+
}
226+
188227
/**
189228
* Bootstrap the kernel with enhanced features
190229
*/
@@ -231,6 +270,7 @@ export class ObjectKernel {
231270
}
232271

233272
// Phase 3: Trigger kernel:ready hook
273+
this.validateSystemRequirements(); // Final check before ready
234274
this.logger.debug('Triggering kernel:ready hook');
235275
await this.context.trigger('kernel:ready');
236276

packages/core/src/plugin-loader.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,13 @@ export class PluginLoader {
246246
this.serviceInstances.set(name, service);
247247
}
248248

249+
/**
250+
* Check if a service is registered (either as instance or factory)
251+
*/
252+
hasService(name: string): boolean {
253+
return this.serviceInstances.has(name) || this.serviceFactories.has(name);
254+
}
255+
249256
/**
250257
* Detect circular dependencies in service factories
251258
* Note: This only detects cycles in service dependencies, not plugin dependencies.

packages/spec/src/system/service-registry.zod.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,41 @@ export const CoreServiceName = z.enum([
3838

3939
export type CoreServiceName = z.infer<typeof CoreServiceName>;
4040

41+
/**
42+
* Service Criticality Level
43+
* Defines the startup behavior when a service is missing.
44+
*/
45+
export const ServiceCriticalitySchema = z.enum([
46+
'required', // System fails to start if missing (Exit Code 1)
47+
'core', // System warns if missing, functionality degraded (Warn)
48+
'optional', // System ignores if missing, feature disabled (Info)
49+
]);
50+
51+
/**
52+
* Service Requirement Definition
53+
*/
54+
export const ServiceRequirementDef = {
55+
// Required: The kernel cannot function without these
56+
metadata: 'required',
57+
data: 'required',
58+
auth: 'required',
59+
60+
// Core: Highly recommended, defaults to in-memory if missing
61+
cache: 'core',
62+
queue: 'core',
63+
job: 'core',
64+
65+
// Optional: Add-on capabilities
66+
'file-storage': 'optional',
67+
search: 'optional',
68+
automation: 'optional',
69+
graphql: 'optional',
70+
analytics: 'optional',
71+
hub: 'optional',
72+
realtime: 'optional',
73+
notification: 'optional',
74+
} as const;
75+
4176
// ==========================================
4277
// Service Capabilities
4378
// ==========================================

0 commit comments

Comments
 (0)