fix (templates): tal-to-data OK.

This commit is contained in:
Rodrigo Rodriguez 2024-09-11 21:02:19 -03:00
parent 393c73c217
commit 7d8d4e4d6b
3 changed files with 66 additions and 91 deletions

View file

@ -607,7 +607,7 @@ export class GBDeployer implements IGBDeployer {
if (!GBConfigService.get('STORAGE_NAME')) { if (!GBConfigService.get('STORAGE_NAME')) {
const filePath = path.join(GBConfigService.get('STORAGE_LIBRARY'), gbai, packageName); const filePath = path.join(GBConfigService.get('STORAGE_LIBRARY'), gbai, packageName);
GBUtil.copyIfNewerRecursive(filePath, packageWorkFolder); await GBUtil.copyIfNewerRecursive(filePath, packageWorkFolder);
} else { } else {
await this.downloadFolder(min, path.join('work', `${gbai}`), packageName); await this.downloadFolder(min, path.join('work', `${gbai}`), packageName);
} }

View file

@ -36,9 +36,8 @@
import { createRpcServer } from '@push-rpc/core'; import { createRpcServer } from '@push-rpc/core';
import AuthenticationContext from 'adal-node'; import AuthenticationContext from 'adal-node';
import arrayBufferToBuffer from 'arraybuffer-to-buffer'; import arrayBufferToBuffer from 'arraybuffer-to-buffer';
import { watch } from 'fs'; import { Semaphore } from 'async-mutex';
import debounce from 'lodash.debounce'; import { Mutex } from 'async-mutex';
import chokidar from 'chokidar'; import chokidar from 'chokidar';
import { import {
AutoSaveStateMiddleware, AutoSaveStateMiddleware,
@ -1744,63 +1743,41 @@ await fs.writeFile(localFileName, buffer);
} }
// Map to track recent changes with timestamps // Map to track recent changes with timestamps
private recentChanges: Map<string, number> = new Map();
private async watchPackages(min: GBMinInstance, packageType) { private recentChanges: Set<string> = new Set();
private mutex: Mutex = new Mutex();
public async watchPackages(min: GBMinInstance, packageType: string): Promise<void> {
if (!GBConfigService.get('STORAGE_NAME')) { if (!GBConfigService.get('STORAGE_NAME')) {
const packagePath = GBUtil.getGBAIPath(min.botId, packageType); const packagePath = GBUtil.getGBAIPath(min.botId, packageType);
const libraryPath = path.join(GBConfigService.get('STORAGE_LIBRARY'), packagePath); const libraryPath = path.join(GBConfigService.get('STORAGE_LIBRARY'), packagePath);
const watcher = chokidar.watch(libraryPath, { const watcher = chokidar.watch(libraryPath, {
persistent: true, depth: 99 // Watch subdirectories
ignoreInitial: true, // Ignore initial add events
depth: 99, // Watch subdirectories
awaitWriteFinish: {
stabilityThreshold: 500, // Wait for 500ms to ensure file is written completely
}
}); });
const WRITE_INTERVAL = 5000; // 5 seconds interval const handleFileChange = async (filePath: string) => {
this.recentChanges.add(filePath);
// Function to handle file changes and prevent multiple calls within the 5-second window // Use mutex to ensure only one deployment runs at a time
const handleFileChange = async (filePath) => { await this.mutex.runExclusive(async () => {
const now = Date.now();
// Add or update the file in the recent changes list
this.recentChanges.set(filePath, now);
// Clean up entries older than 5 seconds
for (const [key, timestamp] of this.recentChanges.entries()) {
if (now - timestamp > WRITE_INTERVAL) {
this.recentChanges.delete(key);
}
}
// If we have recent changes, deploy the package only once
if (this.recentChanges.size > 0) { if (this.recentChanges.size > 0) {
try { try {
const workFolder = path.join('work', packagePath); const workFolder = path.join('work', packagePath);
await this.deployer.deployPackage2(min, null, workFolder, true); await this.deployer.deployPackage2(min, null, workFolder, true);
} catch (error) { } catch (error) {
GBLogEx.error(min, `Error deploying package: ${GBUtil.toYAML(error)}`); GBLogEx.error(min, `Error deploying package: ${GBUtil.toYAML(error)}`);
} } finally {
// Delay further processing to prevent multiple deployments within 5 seconds
await new Promise(resolve => setTimeout(resolve, WRITE_INTERVAL));
// After the delay, clear only entries that were processed
this.recentChanges.clear(); this.recentChanges.clear();
} }
}
});
}; };
// Watch for file changes // Watch for file changes
watcher.on('change', (filePath) => { watcher.on('change', filePath => {
handleFileChange(filePath).catch(error => console.error('Error processing file change:', error)); handleFileChange(filePath).catch(error => console.error('Error processing file change:', error));
}); });
} }
} }
} }

View file

@ -161,8 +161,8 @@ export class GBUtil {
} }
public static async copyIfNewerRecursive(src, dest) { public static async copyIfNewerRecursive(src, dest) {
// Check if the source exists
if (!(await GBUtil.exists(src))) { if (!(await GBUtil.exists(src))) {
console.error(`Source path "${src}" does not exist.`);
return; return;
} }
@ -181,12 +181,10 @@ export class GBUtil {
const destEntry = path.join(dest, entry); const destEntry = path.join(dest, entry);
// Recursively copy each entry // Recursively copy each entry
await this.copyIfNewerRecursive(srcEntry, destEntry); await this.copyIfNewerRecursive(srcEntry, destEntry);
} }
} else { } else {
// Source is a file, check if we need to copy it // Source is a file, check if we need to copy it
if (await GBUtil.exists(dest)) { if (await GBUtil.exists(dest)) {
const srcStat = await fs.stat(src); const srcStat = await fs.stat(src);
const destStat = await fs.stat(dest); const destStat = await fs.stat(dest);