chore: import codes
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
/**
|
||||
* ZEN Initialization
|
||||
* Initialize all ZEN services and modules using dynamic module discovery
|
||||
*/
|
||||
|
||||
import { discoverModules, startModuleCronJobs, stopModuleCronJobs } from '../../core/modules/index.js';
|
||||
|
||||
// Use globalThis to persist initialization flag across module reloads
|
||||
const ZEN_INIT_KEY = Symbol.for('__ZEN_INITIALIZED__');
|
||||
|
||||
/**
|
||||
* Initialize ZEN system
|
||||
* Discovers modules dynamically and starts cron jobs
|
||||
*
|
||||
* Recommended: Use instrumentation.js for automatic initialization
|
||||
* Alternative: Call this function manually in your root layout
|
||||
*
|
||||
* @example
|
||||
* // instrumentation.js (Recommended)
|
||||
* export async function register() {
|
||||
* if (process.env.NEXT_RUNTIME === 'nodejs') {
|
||||
* const { initializeZen } = await import('@hykocx/zen');
|
||||
* await initializeZen();
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* @example
|
||||
* // app/layout.js (Alternative)
|
||||
* import { initializeZen } from '@hykocx/zen';
|
||||
* initializeZen();
|
||||
*
|
||||
* @param {Object} options - Initialization options
|
||||
* @param {boolean} options.skipCron - Skip cron job initialization
|
||||
* @param {boolean} options.skipDb - Skip database initialization
|
||||
* @returns {Promise<Object>} Initialization result
|
||||
*/
|
||||
export async function initializeZen(options = {}) {
|
||||
const { skipCron = false, skipDb = true } = options;
|
||||
|
||||
// Only run on server-side
|
||||
if (typeof window !== 'undefined') {
|
||||
return { skipped: true, reason: 'client-side' };
|
||||
}
|
||||
|
||||
// Prevent multiple initializations using globalThis
|
||||
if (globalThis[ZEN_INIT_KEY]) {
|
||||
console.log('⚠ ZEN: Already initialized, skipping...');
|
||||
return { skipped: true, reason: 'already-initialized' };
|
||||
}
|
||||
|
||||
globalThis[ZEN_INIT_KEY] = true;
|
||||
console.log('🚀 ZEN: Starting initialization...');
|
||||
|
||||
const result = {
|
||||
discovery: null,
|
||||
cron: { started: [], errors: [] }
|
||||
};
|
||||
|
||||
try {
|
||||
// Step 1: Discover and register all enabled modules
|
||||
// This reads from modules.registry.js and loads each module's config files
|
||||
result.discovery = await discoverModules();
|
||||
|
||||
const enabledCount = result.discovery.enabled?.length || 0;
|
||||
const skippedCount = result.discovery.skipped?.length || 0;
|
||||
|
||||
if (enabledCount > 0) {
|
||||
console.log(`✓ ZEN: Discovered ${enabledCount} enabled module(s): ${result.discovery.enabled.join(', ')}`);
|
||||
}
|
||||
if (skippedCount > 0) {
|
||||
console.log(`⚠ ZEN: Skipped ${skippedCount} disabled module(s): ${result.discovery.skipped.join(', ')}`);
|
||||
}
|
||||
|
||||
// Step 2: Start cron jobs for all enabled modules
|
||||
if (!skipCron) {
|
||||
result.cron = await startModuleCronJobs();
|
||||
|
||||
if (result.cron.started.length > 0) {
|
||||
console.log(`✓ ZEN: Started ${result.cron.started.length} cron job(s): ${result.cron.started.join(', ')}`);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('✓ ZEN: Initialization complete');
|
||||
|
||||
} catch (error) {
|
||||
console.error('✗ ZEN: Initialization failed:', error);
|
||||
result.error = error.message;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset initialization flag (useful for testing or manual reinitialization)
|
||||
* @returns {void}
|
||||
*/
|
||||
export function resetZenInitialization() {
|
||||
globalThis[ZEN_INIT_KEY] = false;
|
||||
|
||||
// Stop all cron jobs using the module system
|
||||
try {
|
||||
stopModuleCronJobs();
|
||||
} catch (e) {
|
||||
// Cron system not available
|
||||
}
|
||||
|
||||
console.log('⚠ ZEN: Initialization flag reset');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user