Skip to content

Commit

Permalink
fix: improve operations logging
Browse files Browse the repository at this point in the history
  • Loading branch information
nytamin committed Feb 9, 2024
1 parent 145349b commit 112ae2d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 16 deletions.
4 changes: 3 additions & 1 deletion shared/packages/worker/src/worker/accessorHandlers/atem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,15 @@ export class ATEMAccessorHandle<Metadata> extends GenericAccessorHandle<Metadata
}
}
}
async removePackage(): Promise<void> {
async removePackage(reason: string): Promise<void> {
const atem = await this.getAtem()
if (this.accessor.mediaType && typeof this.accessor.bankIndex === 'number') {
if (this.accessor.mediaType === 'clip') {
await atem.clearMediaPoolClip(this.accessor.bankIndex)
this.worker.logOperation(`Remove package: Removed clip "${this.packageName}" (${reason})`)
} else {
await atem.clearMediaPoolStill(this.accessor.bankIndex)
this.worker.logOperation(`Remove package: Removed still "${this.packageName}" (${reason})`)
}
} else {
throw new Error('mediaType or bankIndex were undefined')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,17 @@ export class FileShareAccessorHandle<Metadata> extends GenericFileAccessorHandle
async removePackage(reason: string): Promise<void> {
await this.prepareFileAccess()
if (this.workOptions.removeDelay) {
this.worker.logOperation(
`Remove package: Delay remove file "${this.packageName}", delay: ${this.workOptions.removeDelay} (${reason})`
)
await this.delayPackageRemoval(this.filePath, this.workOptions.removeDelay)
} else {
await this.removeMetadata()
if (await this.unlinkIfExists(this.fullPath))
if (await this.unlinkIfExists(this.fullPath)) {
this.worker.logOperation(`Remove package: Removed file "${this.packageName}", ${reason}`)
} else {
this.worker.logOperation(`Remove package: File already removed "${this.packageName}" (${reason})`)
}
}
}

Expand Down
17 changes: 13 additions & 4 deletions shared/packages/worker/src/worker/accessorHandlers/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,19 @@ export class HTTPAccessorHandle<Metadata> extends GenericAccessorHandle<Metadata

return this.convertHeadersToVersion(header.headers)
}
async removePackage(): Promise<void> {
async removePackage(reason: string): Promise<void> {
if (this.workOptions.removeDelay) {
await this.delayPackageRemoval(this.workOptions.removeDelay)
this.worker.logOperation(
`Remove package: Delay remove package "${this.packageName}", delay: ${this.workOptions.removeDelay} (${reason})`
)
} else {
await this.removeMetadata()
await this.deletePackageIfExists(this.fullUrl)
if (await this.deletePackageIfExists(this.fullUrl)) {
this.worker.logOperation(`Remove package: Removed file "${this.packageName}" (${reason})`)
} else {
this.worker.logOperation(`Remove package: File already removed "${this.packageName}" (${reason})`)
}
}
}
async getPackageReadStream(): Promise<PackageReadStream> {
Expand Down Expand Up @@ -361,17 +368,19 @@ export class HTTPAccessorHandle<Metadata> extends GenericAccessorHandle<Metadata
}
return null
}
private async deletePackageIfExists(url: string): Promise<void> {
/** Returns false if it doesn't exist */
private async deletePackageIfExists(url: string): Promise<boolean> {
const result = await fetchWithTimeout(url, {
method: 'DELETE',
})
if (result.status === 404) return undefined // that's ok
if (result.status === 404) return false // that's ok
if (result.status >= 400) {
const text = await result.text()
throw new Error(
`deletePackageIfExists: Bad response: [${result.status}]: ${result.statusText}, DELETE ${this.fullUrl}, ${text}`
)
}
return true
}
/** Full path to the file containing deferred removals */
private get deferRemovePackagesPath(): string {
Expand Down
23 changes: 15 additions & 8 deletions shared/packages/worker/src/worker/accessorHandlers/httpProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,19 @@ export class HTTPProxyAccessorHandle<Metadata> extends GenericAccessorHandle<Met

return this.convertHeadersToVersion(header.headers)
}
async removePackage(): Promise<void> {
async removePackage(reason: string): Promise<void> {
if (this.workOptions.removeDelay) {
await this.delayPackageRemoval(this.workOptions.removeDelay)
this.worker.logOperation(
`Remove package: Delay remove package "${this.packageName}", delay: ${this.workOptions.removeDelay} (${reason})`
)
await this.delayPackageRemoval(this.filePath, this.workOptions.removeDelay)
} else {
await this.removeMetadata()
await this.deletePackageIfExists(this.fullUrl)
if (await this.deletePackageIfExists(this.fullUrl)) {
this.worker.logOperation(`Remove package: Removed file "${this.packageName}" (${reason})`)
} else {
this.worker.logOperation(`Remove package: File already removed "${this.packageName}" (${reason})`)
}
}
}
async getPackageReadStream(): Promise<PackageReadStream> {
Expand Down Expand Up @@ -297,11 +304,9 @@ export class HTTPProxyAccessorHandle<Metadata> extends GenericAccessorHandle<Met
}
}

async delayPackageRemoval(ttl: number): Promise<void> {
async delayPackageRemoval(filePath: string, ttl: number): Promise<void> {
const packagesToRemove = await this.getPackagesToRemove()

const filePath = this.filePath

// Search for a pre-existing entry:
let found = false
for (const entry of packagesToRemove) {
Expand Down Expand Up @@ -375,17 +380,19 @@ export class HTTPProxyAccessorHandle<Metadata> extends GenericAccessorHandle<Met
}
return null
}
private async deletePackageIfExists(url: string): Promise<void> {
/** Returns false if nothing was removed */
private async deletePackageIfExists(url: string): Promise<boolean> {
const result = await fetchWithTimeout(url, {
method: 'DELETE',
})
if (result.status === 404) return undefined // that's ok
if (result.status === 404) return false // that's ok
if (result.status >= 400) {
const text = await result.text()
throw new Error(
`deletePackageIfExists: Bad response: [${result.status}]: ${result.statusText}, DELETE ${this.fullUrl}, ${text}`
)
}
return true
}
/** Full path to the file containing deferred removals */
private get deferRemovePackagesPath(): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,17 @@ export class LocalFolderAccessorHandle<Metadata> extends GenericFileAccessorHand
}
async removePackage(reason: string): Promise<void> {
if (this.workOptions.removeDelay) {
this.worker.logOperation(
`Remove package: Delay remove file "${this.packageName}", delay: ${this.workOptions.removeDelay} (${reason})`
)
await this.delayPackageRemoval(this.filePath, this.workOptions.removeDelay)
} else {
await this.removeMetadata()
if (await this.unlinkIfExists(this.fullPath))
this.worker.logOperation(`Remove package: Removed file "${this.fullPath}" (${reason})`)
if (await this.unlinkIfExists(this.fullPath)) {
this.worker.logOperation(`Remove package: Removed file "${this.packageName}" (${reason})`)
} else {
this.worker.logOperation(`Remove package: File already removed "${this.packageName}" (${reason})`)
}
}
}
async getPackageReadStream(): Promise<{ readStream: NodeJS.ReadableStream; cancel: () => void }> {
Expand Down

0 comments on commit 112ae2d

Please sign in to comment.