ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

runner.ts 1.6 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { RunnerJobState, RunnerJobStateType } from '@peertube/peertube-models'
  2. import { retryTransactionWrapper } from '@server/helpers/database-utils.js'
  3. import { logger, loggerTagsFactory } from '@server/helpers/logger.js'
  4. import { RUNNER_JOBS } from '@server/initializers/constants.js'
  5. import { sequelizeTypescript } from '@server/initializers/database.js'
  6. import { MRunner, MRunnerJob } from '@server/types/models/runners/index.js'
  7. import express from 'express'
  8. const lTags = loggerTagsFactory('runner')
  9. const updatingRunner = new Set<number>()
  10. function updateLastRunnerContact (req: express.Request, runner: MRunner) {
  11. const now = new Date()
  12. // Don't update last runner contact too often
  13. if (now.getTime() - runner.lastContact.getTime() < RUNNER_JOBS.LAST_CONTACT_UPDATE_INTERVAL) return
  14. if (updatingRunner.has(runner.id)) return
  15. updatingRunner.add(runner.id)
  16. runner.lastContact = now
  17. runner.ip = req.ip
  18. logger.debug('Updating last runner contact for %s', runner.name, lTags(runner.name))
  19. retryTransactionWrapper(() => {
  20. return sequelizeTypescript.transaction(async transaction => {
  21. return runner.save({ transaction })
  22. })
  23. })
  24. .catch(err => logger.error('Cannot update last runner contact for %s', runner.name, { err, ...lTags(runner.name) }))
  25. .finally(() => updatingRunner.delete(runner.id))
  26. }
  27. function runnerJobCanBeCancelled (runnerJob: MRunnerJob) {
  28. const allowedStates = new Set<RunnerJobStateType>([
  29. RunnerJobState.PENDING,
  30. RunnerJobState.PROCESSING,
  31. RunnerJobState.WAITING_FOR_PARENT_JOB
  32. ])
  33. return allowedStates.has(runnerJob.state)
  34. }
  35. export {
  36. updateLastRunnerContact,
  37. runnerJobCanBeCancelled
  38. }