From 0c9668f77901e7540e2c7045eb0f2974a4842a69 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 21 Apr 2023 14:55:10 +0200 Subject: Implement remote runner jobs in server Move ffmpeg functions to @shared --- server/models/runner/runner.ts | 112 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 server/models/runner/runner.ts (limited to 'server/models/runner/runner.ts') diff --git a/server/models/runner/runner.ts b/server/models/runner/runner.ts new file mode 100644 index 000000000..1ef0018b4 --- /dev/null +++ b/server/models/runner/runner.ts @@ -0,0 +1,112 @@ +import { FindOptions } from 'sequelize' +import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' +import { MRunner } from '@server/types/models/runners' +import { Runner } from '@shared/models' +import { AttributesOnly } from '@shared/typescript-utils' +import { getSort } from '../shared' +import { RunnerRegistrationTokenModel } from './runner-registration-token' +import { CONSTRAINTS_FIELDS } from '@server/initializers/constants' + +@Table({ + tableName: 'runner', + indexes: [ + { + fields: [ 'runnerToken' ], + unique: true + }, + { + fields: [ 'runnerRegistrationTokenId' ] + } + ] +}) +export class RunnerModel extends Model>> { + + // Used to identify the appropriate runner when it uses the runner REST API + @AllowNull(false) + @Column + runnerToken: string + + @AllowNull(false) + @Column + name: string + + @AllowNull(true) + @Column(DataType.STRING(CONSTRAINTS_FIELDS.RUNNERS.DESCRIPTION.max)) + description: string + + @AllowNull(false) + @Column + lastContact: Date + + @AllowNull(false) + @Column + ip: string + + @CreatedAt + createdAt: Date + + @UpdatedAt + updatedAt: Date + + @ForeignKey(() => RunnerRegistrationTokenModel) + @Column + runnerRegistrationTokenId: number + + @BelongsTo(() => RunnerRegistrationTokenModel, { + foreignKey: { + allowNull: false + }, + onDelete: 'cascade' + }) + RunnerRegistrationToken: RunnerRegistrationTokenModel + + // --------------------------------------------------------------------------- + + static load (id: number) { + return RunnerModel.findByPk(id) + } + + static loadByToken (runnerToken: string) { + const query = { + where: { runnerToken } + } + + return RunnerModel.findOne(query) + } + + static listForApi (options: { + start: number + count: number + sort: string + }) { + const { start, count, sort } = options + + const query: FindOptions = { + offset: start, + limit: count, + order: getSort(sort) + } + + return Promise.all([ + RunnerModel.count(query), + RunnerModel.findAll(query) + ]).then(([ total, data ]) => ({ total, data })) + } + + // --------------------------------------------------------------------------- + + toFormattedJSON (this: MRunner): Runner { + return { + id: this.id, + + name: this.name, + description: this.description, + + ip: this.ip, + lastContact: this.lastContact, + + createdAt: this.createdAt, + updatedAt: this.updatedAt + } + } +} -- cgit v1.2.3