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')) {
const filePath = path.join(GBConfigService.get('STORAGE_LIBRARY'), gbai, packageName);
GBUtil.copyIfNewerRecursive(filePath, packageWorkFolder);
await GBUtil.copyIfNewerRecursive(filePath, packageWorkFolder);
} else {
await this.downloadFolder(min, path.join('work', `${gbai}`), packageName);
}

View file

@ -36,9 +36,8 @@
import { createRpcServer } from '@push-rpc/core';
import AuthenticationContext from 'adal-node';
import arrayBufferToBuffer from 'arraybuffer-to-buffer';
import { watch } from 'fs';
import debounce from 'lodash.debounce';
import { Semaphore } from 'async-mutex';
import { Mutex } from 'async-mutex';
import chokidar from 'chokidar';
import {
AutoSaveStateMiddleware,
@ -1744,63 +1743,41 @@ await fs.writeFile(localFileName, buffer);
}
// 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')) {
const packagePath = GBUtil.getGBAIPath(min.botId, packageType);
const libraryPath = path.join(GBConfigService.get('STORAGE_LIBRARY'), packagePath);
const watcher = chokidar.watch(libraryPath, {
persistent: true,
ignoreInitial: true, // Ignore initial add events
depth: 99, // Watch subdirectories
awaitWriteFinish: {
stabilityThreshold: 500, // Wait for 500ms to ensure file is written completely
}
depth: 99 // Watch subdirectories
});
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
const handleFileChange = async (filePath) => {
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
// Use mutex to ensure only one deployment runs at a time
await this.mutex.runExclusive(async () => {
if (this.recentChanges.size > 0) {
try {
const workFolder = path.join('work', packagePath);
await this.deployer.deployPackage2(min, null, workFolder, true);
} catch (error) {
GBLogEx.error(min, `Error deploying package: ${GBUtil.toYAML(error)}`);
}
// 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
} finally {
this.recentChanges.clear();
}
}
});
};
// Watch for file changes
watcher.on('change', (filePath) => {
watcher.on('change', filePath => {
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) {
// Check if the source exists
if (!(await GBUtil.exists(src))) {
console.error(`Source path "${src}" does not exist.`);
return;
}
@ -181,12 +181,10 @@ export class GBUtil {
const destEntry = path.join(dest, entry);
// Recursively copy each entry
await this.copyIfNewerRecursive(srcEntry, destEntry);
}
} else {
// Source is a file, check if we need to copy it
if (await GBUtil.exists(dest)) {
const srcStat = await fs.stat(src);
const destStat = await fs.stat(dest);