aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/schedulers/remove-dangling-resumable-uploads-scheduler.ts
blob: 61e93eafa89402d6508b3da15956ce7f2d210da4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { logger, loggerTagsFactory } from '@server/helpers/logger'
import { SCHEDULER_INTERVALS_MS } from '@server/initializers/constants'
import { uploadx } from '../uploadx'
import { AbstractScheduler } from './abstract-scheduler'

const lTags = loggerTagsFactory('scheduler', 'resumable-upload', 'cleaner')

export class RemoveDanglingResumableUploadsScheduler extends AbstractScheduler {

  private static instance: AbstractScheduler
  private lastExecutionTimeMs: number

  protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.REMOVE_DANGLING_RESUMABLE_UPLOADS

  private constructor () {
    super()

    this.lastExecutionTimeMs = new Date().getTime()
  }

  protected async internalExecute () {
    logger.debug('Removing dangling resumable uploads', lTags())

    const now = new Date().getTime()

    try {
      // Remove files that were not updated since the last execution
      await uploadx.storage.purge(now - this.lastExecutionTimeMs)
    } catch (error) {
      logger.error('Failed to handle file during resumable video upload folder cleanup', { error, ...lTags() })
    } finally {
      this.lastExecutionTimeMs = now
    }
  }

  static get Instance () {
    return this.instance || (this.instance = new this())
  }
}