Enhances cleanup with async lifecycle handling
Updates plugin cleanup to utilize async handlers during onunload, ensuring proper resource management and cleanup of temporary files. Removes redundant process termination listeners and integrates lifecycle methods for better maintainability and efficiency. Improves robustness of plugin cleanup on unload.
This commit is contained in:
3 files changed
+29
-66
No files matched your search
@@ -236,11 +236,14 @@ export class obsidianTypstPDFExport extends Plugin {
|
|||||||
throw error; // Re-throw to let callers handle appropriately
|
throw error; // Re-throw to let callers handle appropriately
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
onunload() {
|
// Obsidian supports async onunload and will await it properly, even though
|
||||||
|
// the Plugin type definition shows void return. This ensures cleanup completes.
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
||||||
|
async onunload(): Promise<void> {
|
||||||
// Use lifecycle manager for cleanup
|
// Use lifecycle manager for cleanup
|
||||||
this.lifecycle.cleanup();
|
await this.lifecycle.cleanup();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
* - Error handling
|
* - Error handling
|
||||||
*
|
*
|
||||||
* The converter supports both file-based and content-based conversions, automatically
|
* The converter supports both file-based and content-based conversions, automatically
|
||||||
* managing temporary directories and cleanup on process termination.
|
* managing temporary directories with cleanup via the plugin lifecycle.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { PandocOptions, TypstSettings, ConversionResult, ProgressCallback } from './converterTypes';
|
import { PandocOptions, TypstSettings, ConversionResult, ProgressCallback } from './converterTypes';
|
||||||
@@ -33,7 +33,7 @@ import { PandocExecutor } from './pandoc/PandocExecutor';
|
|||||||
* Key features:
|
* Key features:
|
||||||
* - File and content-based conversion support
|
* - File and content-based conversion support
|
||||||
* - Automatic temporary directory management
|
* - Automatic temporary directory management
|
||||||
* - Process cleanup on termination (exit, SIGINT, SIGTERM, exceptions)
|
* - Cleanup via plugin lifecycle (onunload)
|
||||||
* - Progress callback support for UI updates
|
* - Progress callback support for UI updates
|
||||||
* - Configurable Pandoc and Typst settings
|
* - Configurable Pandoc and Typst settings
|
||||||
*
|
*
|
||||||
@@ -41,8 +41,8 @@ import { PandocExecutor } from './pandoc/PandocExecutor';
|
|||||||
* 1. Constructor initializes command builder and executor
|
* 1. Constructor initializes command builder and executor
|
||||||
* 2. Conversion methods create temp directories and build commands
|
* 2. Conversion methods create temp directories and build commands
|
||||||
* 3. Executor runs Pandoc with Typst engine
|
* 3. Executor runs Pandoc with Typst engine
|
||||||
* 4. Cleanup automatically removes temporary files
|
* 4. Plugin unload calls dispose() to remove temporary files
|
||||||
* 5. dispose() for manual cleanup when done
|
* 5. dispose() can also be called manually for cleanup when done
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* ```typescript
|
* ```typescript
|
||||||
@@ -75,15 +75,14 @@ export class PandocTypstConverter {
|
|||||||
private plugin: obsidianTypstPDFExport;
|
private plugin: obsidianTypstPDFExport;
|
||||||
private commandBuilder: PandocCommandBuilder;
|
private commandBuilder: PandocCommandBuilder;
|
||||||
private pandocExecutor: PandocExecutor;
|
private pandocExecutor: PandocExecutor;
|
||||||
private processCleanupHandler: (() => void) | null = null;
|
|
||||||
private isDisposed = false;
|
private isDisposed = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new PandocTypstConverter instance with configuration.
|
* Creates a new PandocTypstConverter instance with configuration.
|
||||||
*
|
*
|
||||||
* Initializes the converter with the plugin instance and configuration options
|
* Initializes the converter with the plugin instance and configuration options
|
||||||
* for both Pandoc and Typst. Sets up cleanup handlers to ensure temporary
|
* for both Pandoc and Typst. Temporary files are cleaned up via dispose()
|
||||||
* files are removed on process termination.
|
* which is called during plugin unload.
|
||||||
*
|
*
|
||||||
* @param plugin - The main plugin instance for accessing app and settings
|
* @param plugin - The main plugin instance for accessing app and settings
|
||||||
* @param pandocOptions - Configuration options for Pandoc conversion (template, variables, etc.)
|
* @param pandocOptions - Configuration options for Pandoc conversion (template, variables, etc.)
|
||||||
@@ -116,8 +115,6 @@ export class PandocTypstConverter {
|
|||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
this.commandBuilder = new PandocCommandBuilder(plugin);
|
this.commandBuilder = new PandocCommandBuilder(plugin);
|
||||||
this.pandocExecutor = new PandocExecutor(plugin);
|
this.pandocExecutor = new PandocExecutor(plugin);
|
||||||
// Set up cleanup handlers for process termination
|
|
||||||
this.setupCleanup();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -282,38 +279,6 @@ export class PandocTypstConverter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets up automatic cleanup handlers for process termination events.
|
|
||||||
*
|
|
||||||
* Registers the cleanup method to run on various process termination events
|
|
||||||
* to ensure temporary files are removed even if the process exits unexpectedly.
|
|
||||||
* Handlers are registered for:
|
|
||||||
* - Normal exit
|
|
||||||
* - SIGINT (Ctrl+C)
|
|
||||||
* - SIGTERM (kill command)
|
|
||||||
*
|
|
||||||
* Note: Handlers are stored and removed on dispose() to prevent listener leaks.
|
|
||||||
* The uncaughtException handler is NOT registered to avoid swallowing errors.
|
|
||||||
*
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
private setupCleanup(): void {
|
|
||||||
// Store handler reference so we can remove it later
|
|
||||||
this.processCleanupHandler = () => {
|
|
||||||
// Only cleanup if not already disposed to avoid double-cleanup
|
|
||||||
if (!this.isDisposed) {
|
|
||||||
void this.cleanup();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Register cleanup handlers - these will be removed on dispose()
|
|
||||||
process.on('exit', this.processCleanupHandler);
|
|
||||||
process.on('SIGINT', this.processCleanupHandler);
|
|
||||||
process.on('SIGTERM', this.processCleanupHandler);
|
|
||||||
// Note: uncaughtException handler removed to avoid swallowing errors
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cleans up temporary files and directories.
|
* Cleans up temporary files and directories.
|
||||||
*
|
*
|
||||||
@@ -321,7 +286,7 @@ export class PandocTypstConverter {
|
|||||||
* directory created by the converter. Cleanup failures are logged but don't throw
|
* directory created by the converter. Cleanup failures are logged but don't throw
|
||||||
* to avoid interfering with normal operation or other cleanup operations.
|
* to avoid interfering with normal operation or other cleanup operations.
|
||||||
*
|
*
|
||||||
* Called automatically on process termination or manually via dispose().
|
* Called via dispose() during plugin unload.
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @returns Promise that resolves when cleanup is complete
|
* @returns Promise that resolves when cleanup is complete
|
||||||
@@ -413,14 +378,6 @@ export class PandocTypstConverter {
|
|||||||
|
|
||||||
this.isDisposed = true;
|
this.isDisposed = true;
|
||||||
|
|
||||||
// Remove process event listeners to prevent memory leaks
|
|
||||||
if (this.processCleanupHandler) {
|
|
||||||
process.off('exit', this.processCleanupHandler);
|
|
||||||
process.off('SIGINT', this.processCleanupHandler);
|
|
||||||
process.off('SIGTERM', this.processCleanupHandler);
|
|
||||||
this.processCleanupHandler = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cleanup temporary files and resources
|
// Cleanup temporary files and resources
|
||||||
await this.cleanup();
|
await this.cleanup();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,22 +62,25 @@ export class PluginLifecycle {
|
|||||||
/**
|
/**
|
||||||
* Clean up resources during onunload
|
* Clean up resources during onunload
|
||||||
*/
|
*/
|
||||||
cleanup(): void {
|
async cleanup(): Promise<void> {
|
||||||
// Cancel any ongoing exports
|
// Cancel any ongoing exports
|
||||||
if (this.plugin.currentExportController) {
|
if (this.plugin.currentExportController) {
|
||||||
this.plugin.currentExportController.abort();
|
this.plugin.currentExportController.abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up temp directories on plugin unload (fire-and-forget async)
|
// Dispose converter to clean up its temp directories
|
||||||
void (async () => {
|
if (this.plugin.converter) {
|
||||||
try {
|
await this.plugin.converter.dispose();
|
||||||
const vaultPath = this.pathUtils.getVaultPath();
|
}
|
||||||
const cleanupManager = TempDirectoryManager.create(vaultPath, this.plugin.app.vault.configDir, undefined, this.plugin.app);
|
|
||||||
await cleanupManager.cleanupAllTempDirs();
|
// Clean up temp directories on plugin unload
|
||||||
} catch (error) {
|
try {
|
||||||
console.warn('Export: Failed to clean up temp directories during unload:', error);
|
const vaultPath = this.pathUtils.getVaultPath();
|
||||||
}
|
const cleanupManager = TempDirectoryManager.create(vaultPath, this.plugin.app.vault.configDir, undefined, this.plugin.app);
|
||||||
})();
|
await cleanupManager.cleanupAllTempDirs();
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('Export: Failed to clean up temp directories during unload:', error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in new issue
Block a user