Skip to content

Commit c7f32d3

Browse files
committed
fix: a few minor ftp fixes & optimizations
1 parent f435656 commit c7f32d3

2 files changed

Lines changed: 25 additions & 12 deletions

File tree

shared/packages/worker/src/worker/accessorHandlers/ftp.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,15 @@ export class FTPAccessorHandle<Metadata> extends GenericAccessorHandle<Metadata>
198198
} else throw new Error(`getPackageActualVersion: ${response.reason.user}: ${response.reason.tech}`)
199199
}
200200
async ensurePackageFulfilled(): Promise<void> {
201-
await this.fileHandler.clearPackageRemoval(this.filePath)
201+
if ((this.workOptions.removeDelay ?? -1) >= 0) {
202+
// Only handle this if there is a removeDelay set, otherwise we can skip it to save time:
203+
await this.fileHandler.clearPackageRemoval(this.filePath)
204+
}
202205
}
203206
async removePackage(reason: string): Promise<void> {
204207
await this.fileHandler.handleRemovePackage(this.filePath, this.packageName, reason)
205208
}
206209
async getPackageReadStream(): Promise<PackageReadStream> {
207-
// important that this is a 'read', so that it doesn't go into a deadlock with putPackageStream() in case of an upload/download to the same accessorPackageContainer
208210
const ftp = await this.prepareFTPClient()
209211

210212
const response = await ftp.download(this.fullPath)
@@ -267,7 +269,10 @@ export class FTPAccessorHandle<Metadata> extends GenericAccessorHandle<Metadata>
267269
operationName: string,
268270
source: string | GenericAccessorHandle<any>
269271
): Promise<PackageOperation> {
270-
await this.fileHandler.clearPackageRemoval(this.filePath)
272+
if ((this.workOptions.removeDelay ?? -1) >= 0) {
273+
// Only handle this if there is a removeDelay set, otherwise we can skip it to save time:
274+
await this.fileHandler.clearPackageRemoval(this.filePath)
275+
}
271276
return this.logWorkOperation(operationName, source, this.packageName)
272277
}
273278
async finalizePackage(operation: PackageOperation): Promise<void> {
@@ -482,18 +487,20 @@ export class FTPAccessorHandle<Metadata> extends GenericAccessorHandle<Metadata>
482487
[accessorType: string]: CachedClients | undefined
483488
}
484489

485-
const cacheKey = JSON.stringify([
486-
this.accessorId,
487-
ftpOptions.serverType,
488-
ftpOptions.host,
489-
ftpOptions.port,
490-
this.accessor.basePath ?? '/',
491-
])
490+
const cacheKey = JSON.stringify([this.accessorId, options, this.accessor.basePath ?? '/'])
492491

493492
let cachedClients = accessorCache[cacheKey]
494493
if (cachedClients) {
495494
// Check that options matches:
496495
if (!isEqual(cachedClients.options, options)) {
496+
// It the options don't match, something is wrong with the cacheKey
497+
498+
this.worker.logger.error(
499+
`Something is wrong with the FTP client cacheKey. The options do not match the cacheKey. Deleting cached clients for this key. cacheKey: ${cacheKey}, options: ${JSON.stringify(
500+
options
501+
)}, cachedOptions: ${JSON.stringify(cachedClients.options)}`
502+
)
503+
497504
for (const c of cachedClients.clients) {
498505
await c.client.destroy()
499506
}
@@ -532,9 +539,10 @@ export class FTPAccessorHandle<Metadata> extends GenericAccessorHandle<Metadata>
532539
}
533540

534541
if (!cachedClient) {
542+
this.worker.logger.info(`Creating new FTP client for purpose="${purpose}", ${JSON.stringify(options)}`)
535543
// Set up a new FTP client:
536544
cachedClient = {
537-
client: createFTPClient(ftpOptions.serverType, this.worker.logger, options),
545+
client: createFTPClient(options.serverType, this.worker.logger, options),
538546
purpose: purpose,
539547
}
540548
cachedClients.clients.push(cachedClient)
@@ -580,6 +588,11 @@ export class FTPAccessorHandle<Metadata> extends GenericAccessorHandle<Metadata>
580588
return ftp.downloadContent(fullPath)
581589
}
582590
async readFileIfExists(fullPath: string): Promise<Buffer | undefined> {
591+
// On FTP, it is a much lighter operation to check if the file exists that reading it,
592+
// so we'll do that first:
593+
const exists = await this.fileExists(fullPath)
594+
if (!exists) return undefined
595+
583596
try {
584597
return await this.readFile(fullPath)
585598
} catch (e) {

shared/packages/worker/src/worker/accessorHandlers/lib/json-write-file.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ export class JSONWriteFilesBestEffortHandler extends JSONWriteHandler {
302302
const newValue = cbManipulate(oldValue0?.value)
303303
const newValueStr = newValue !== undefined ? JSON.stringify(newValue) : ''
304304

305-
if (oldValue0?.str === newValueStr) {
305+
if (oldValue0?.str === newValueStr || (oldValue0 === undefined && newValue === undefined)) {
306306
// do nothing
307307
return
308308
}

0 commit comments

Comments
 (0)