diff options
Diffstat (limited to 'server/lib/runners/runner.ts')
-rw-r--r-- | server/lib/runners/runner.ts | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/server/lib/runners/runner.ts b/server/lib/runners/runner.ts new file mode 100644 index 000000000..74c814ba1 --- /dev/null +++ b/server/lib/runners/runner.ts | |||
@@ -0,0 +1,36 @@ | |||
1 | import express from 'express' | ||
2 | import { retryTransactionWrapper } from '@server/helpers/database-utils' | ||
3 | import { logger, loggerTagsFactory } from '@server/helpers/logger' | ||
4 | import { sequelizeTypescript } from '@server/initializers/database' | ||
5 | import { MRunner } from '@server/types/models/runners' | ||
6 | |||
7 | const lTags = loggerTagsFactory('runner') | ||
8 | |||
9 | const updatingRunner = new Set<number>() | ||
10 | |||
11 | function updateLastRunnerContact (req: express.Request, runner: MRunner) { | ||
12 | const now = new Date() | ||
13 | |||
14 | // Don't update last runner contact too often | ||
15 | if (now.getTime() - runner.lastContact.getTime() < 2000) return | ||
16 | if (updatingRunner.has(runner.id)) return | ||
17 | |||
18 | updatingRunner.add(runner.id) | ||
19 | |||
20 | runner.lastContact = now | ||
21 | runner.ip = req.ip | ||
22 | |||
23 | logger.debug('Updating last runner contact for %s', runner.name, lTags(runner.name)) | ||
24 | |||
25 | retryTransactionWrapper(() => { | ||
26 | return sequelizeTypescript.transaction(async transaction => { | ||
27 | return runner.save({ transaction }) | ||
28 | }) | ||
29 | }) | ||
30 | .catch(err => logger.error('Cannot update last runner contact for %s', runner.name, { err, ...lTags(runner.name) })) | ||
31 | .finally(() => updatingRunner.delete(runner.id)) | ||
32 | } | ||
33 | |||
34 | export { | ||
35 | updateLastRunnerContact | ||
36 | } | ||