From 3fd3ab2d34d512b160a5e6084d7609be7b4f4452 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 12 Dec 2017 17:53:50 +0100 Subject: Move models to typescript-sequelize --- .../activitypub-http-broadcast-handler.ts | 3 +-- .../activitypub-http-fetcher-handler.ts | 7 +++---- .../activitypub-http-job-scheduler.ts | 9 ++++----- .../activitypub-http-unicast-handler.ts | 3 +-- server/lib/jobs/job-scheduler.ts | 20 ++++++++++---------- .../transcoding-job-scheduler.ts | 5 +++-- .../video-file-optimizer-handler.ts | 16 ++++++++-------- .../video-file-transcoder-handler.ts | 11 +++++------ 8 files changed, 35 insertions(+), 39 deletions(-) (limited to 'server/lib/jobs') diff --git a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts index 49d4bf5c6..8040dde2a 100644 --- a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts +++ b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts @@ -1,5 +1,4 @@ -import { logger } from '../../../helpers' -import { doRequest } from '../../../helpers/requests' +import { doRequest, logger } from '../../../helpers' import { ActivityPubHttpPayload, computeBody, maybeRetryRequestLater } from './activitypub-http-job-scheduler' async function process (payload: ActivityPubHttpPayload, jobId: number) { diff --git a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-fetcher-handler.ts b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-fetcher-handler.ts index 9adceab84..638150202 100644 --- a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-fetcher-handler.ts +++ b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-fetcher-handler.ts @@ -1,7 +1,6 @@ -import { logger } from '../../../helpers' -import { doRequest } from '../../../helpers/requests' -import { ACTIVITY_PUB } from '../../../initializers/constants' -import { processActivities } from '../../activitypub/process/process' +import { doRequest, logger } from '../../../helpers' +import { ACTIVITY_PUB } from '../../../initializers' +import { processActivities } from '../../activitypub/process' import { ActivityPubHttpPayload } from './activitypub-http-job-scheduler' async function process (payload: ActivityPubHttpPayload, jobId: number) { diff --git a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts index fcc81eb16..76da5b724 100644 --- a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts +++ b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts @@ -1,8 +1,7 @@ import { JobCategory } from '../../../../shared' -import { buildSignedActivity } from '../../../helpers/activitypub' -import { logger } from '../../../helpers/logger' -import { ACTIVITY_PUB } from '../../../initializers/constants' -import { database as db } from '../../../initializers/database' +import { buildSignedActivity, logger } from '../../../helpers' +import { ACTIVITY_PUB } from '../../../initializers' +import { AccountModel } from '../../../models/account/account' import { JobHandler, JobScheduler } from '../job-scheduler' import * as activitypubHttpBroadcastHandler from './activitypub-http-broadcast-handler' @@ -46,7 +45,7 @@ async function computeBody (payload: ActivityPubHttpPayload) { let body = payload.body if (payload.signatureAccountId) { - const accountSignature = await db.Account.load(payload.signatureAccountId) + const accountSignature = await AccountModel.load(payload.signatureAccountId) if (!accountSignature) throw new Error('Unknown signature account id.') body = await buildSignedActivity(accountSignature, payload.body) } diff --git a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts index 4c95197c4..f16cfcec3 100644 --- a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts +++ b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts @@ -1,5 +1,4 @@ -import { logger } from '../../../helpers' -import { doRequest } from '../../../helpers/requests' +import { doRequest, logger } from '../../../helpers' import { ActivityPubHttpPayload, computeBody, maybeRetryRequestLater } from './activitypub-http-job-scheduler' async function process (payload: ActivityPubHttpPayload, jobId: number) { diff --git a/server/lib/jobs/job-scheduler.ts b/server/lib/jobs/job-scheduler.ts index 62ce6927e..88fe8a4a3 100644 --- a/server/lib/jobs/job-scheduler.ts +++ b/server/lib/jobs/job-scheduler.ts @@ -2,8 +2,8 @@ import { AsyncQueue, forever, queue } from 'async' import * as Sequelize from 'sequelize' import { JobCategory } from '../../../shared' import { logger } from '../../helpers' -import { database as db, JOB_STATES, JOBS_FETCH_LIMIT_PER_CYCLE, JOBS_FETCHING_INTERVAL } from '../../initializers' -import { JobInstance } from '../../models' +import { JOB_STATES, JOBS_FETCH_LIMIT_PER_CYCLE, JOBS_FETCHING_INTERVAL } from '../../initializers' +import { JobModel } from '../../models/job/job' export interface JobHandler { process (data: object, jobId: number): Promise @@ -24,12 +24,12 @@ class JobScheduler { logger.info('Jobs scheduler %s activated.', this.jobCategory) - const jobsQueue = queue(this.processJob.bind(this)) + const jobsQueue = queue(this.processJob.bind(this)) // Finish processing jobs from a previous start const state = JOB_STATES.PROCESSING try { - const jobs = await db.Job.listWithLimitByCategory(limit, state, this.jobCategory) + const jobs = await JobModel.listWithLimitByCategory(limit, state, this.jobCategory) this.enqueueJobs(jobsQueue, jobs) } catch (err) { @@ -45,7 +45,7 @@ class JobScheduler { const state = JOB_STATES.PENDING try { - const jobs = await db.Job.listWithLimitByCategory(limit, state, this.jobCategory) + const jobs = await JobModel.listWithLimitByCategory(limit, state, this.jobCategory) this.enqueueJobs(jobsQueue, jobs) } catch (err) { @@ -70,14 +70,14 @@ class JobScheduler { const options = { transaction } - return db.Job.create(createQuery, options) + return JobModel.create(createQuery, options) } - private enqueueJobs (jobsQueue: AsyncQueue, jobs: JobInstance[]) { + private enqueueJobs (jobsQueue: AsyncQueue, jobs: JobModel[]) { jobs.forEach(job => jobsQueue.push(job)) } - private async processJob (job: JobInstance, callback: (err: Error) => void) { + private async processJob (job: JobModel, callback: (err: Error) => void) { const jobHandler = this.jobHandlers[job.handlerName] if (jobHandler === undefined) { const errorString = 'Unknown job handler ' + job.handlerName + ' for job ' + job.id @@ -110,7 +110,7 @@ class JobScheduler { return callback(null) } - private async onJobError (jobHandler: JobHandler, job: JobInstance, err: Error) { + private async onJobError (jobHandler: JobHandler, job: JobModel, err: Error) { job.state = JOB_STATES.ERROR try { @@ -121,7 +121,7 @@ class JobScheduler { } } - private async onJobSuccess (jobHandler: JobHandler, job: JobInstance, jobResult: T) { + private async onJobSuccess (jobHandler: JobHandler, job: JobModel, jobResult: T) { job.state = JOB_STATES.SUCCESS try { diff --git a/server/lib/jobs/transcoding-job-scheduler/transcoding-job-scheduler.ts b/server/lib/jobs/transcoding-job-scheduler/transcoding-job-scheduler.ts index c5efe8eeb..e5530a73c 100644 --- a/server/lib/jobs/transcoding-job-scheduler/transcoding-job-scheduler.ts +++ b/server/lib/jobs/transcoding-job-scheduler/transcoding-job-scheduler.ts @@ -1,14 +1,15 @@ import { JobCategory } from '../../../../shared' +import { VideoModel } from '../../../models/video/video' import { JobHandler, JobScheduler } from '../job-scheduler' + import * as videoFileOptimizer from './video-file-optimizer-handler' import * as videoFileTranscoder from './video-file-transcoder-handler' -import { VideoInstance } from '../../../models/video/video-interface' type TranscodingJobPayload = { videoUUID: string resolution?: number } -const jobHandlers: { [ handlerName: string ]: JobHandler } = { +const jobHandlers: { [ handlerName: string ]: JobHandler } = { videoFileOptimizer, videoFileTranscoder } diff --git a/server/lib/jobs/transcoding-job-scheduler/video-file-optimizer-handler.ts b/server/lib/jobs/transcoding-job-scheduler/video-file-optimizer-handler.ts index e65ab3ee1..1786ce971 100644 --- a/server/lib/jobs/transcoding-job-scheduler/video-file-optimizer-handler.ts +++ b/server/lib/jobs/transcoding-job-scheduler/video-file-optimizer-handler.ts @@ -1,14 +1,14 @@ import * as Bluebird from 'bluebird' import { computeResolutionsToTranscode, logger } from '../../../helpers' -import { database as db } from '../../../initializers/database' -import { VideoInstance } from '../../../models' -import { sendAddVideo } from '../../activitypub/send/send-add' +import { sequelizeTypescript } from '../../../initializers' +import { VideoModel } from '../../../models/video/video' +import { shareVideoByServer } from '../../activitypub' +import { sendAddVideo } from '../../activitypub/send' import { JobScheduler } from '../job-scheduler' import { TranscodingJobPayload } from './transcoding-job-scheduler' -import { shareVideoByServer } from '../../activitypub/share' async function process (data: TranscodingJobPayload, jobId: number) { - const video = await db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(data.videoUUID) + const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(data.videoUUID) // No video, maybe deleted? if (!video) { logger.info('Do not process job %d, video does not exist.', jobId, { videoUUID: video.uuid }) @@ -25,13 +25,13 @@ function onError (err: Error, jobId: number) { return Promise.resolve() } -async function onSuccess (jobId: number, video: VideoInstance, jobScheduler: JobScheduler) { +async function onSuccess (jobId: number, video: VideoModel, jobScheduler: JobScheduler) { if (video === undefined) return undefined logger.info('Job %d is a success.', jobId) // Maybe the video changed in database, refresh it - const videoDatabase = await db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid) + const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid) // Video does not exist anymore if (!videoDatabase) return undefined @@ -50,7 +50,7 @@ async function onSuccess (jobId: number, video: VideoInstance, jobScheduler: Job if (resolutionsEnabled.length !== 0) { try { - await db.sequelize.transaction(async t => { + await sequelizeTypescript.transaction(async t => { const tasks: Bluebird[] = [] for (const resolution of resolutionsEnabled) { diff --git a/server/lib/jobs/transcoding-job-scheduler/video-file-transcoder-handler.ts b/server/lib/jobs/transcoding-job-scheduler/video-file-transcoder-handler.ts index 867580200..8957b4565 100644 --- a/server/lib/jobs/transcoding-job-scheduler/video-file-transcoder-handler.ts +++ b/server/lib/jobs/transcoding-job-scheduler/video-file-transcoder-handler.ts @@ -1,11 +1,10 @@ import { VideoResolution } from '../../../../shared' import { logger } from '../../../helpers' -import { database as db } from '../../../initializers/database' -import { VideoInstance } from '../../../models' -import { sendUpdateVideo } from '../../activitypub/send/send-update' +import { VideoModel } from '../../../models/video/video' +import { sendUpdateVideo } from '../../activitypub/send' async function process (data: { videoUUID: string, resolution: VideoResolution }, jobId: number) { - const video = await db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(data.videoUUID) + const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(data.videoUUID) // No video, maybe deleted? if (!video) { logger.info('Do not process job %d, video does not exist.', jobId, { videoUUID: video.uuid }) @@ -22,13 +21,13 @@ function onError (err: Error, jobId: number) { return Promise.resolve() } -async function onSuccess (jobId: number, video: VideoInstance) { +async function onSuccess (jobId: number, video: VideoModel) { if (video === undefined) return undefined logger.info('Job %d is a success.', jobId) // Maybe the video changed in database, refresh it - const videoDatabase = await db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid) + const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid) // Video does not exist anymore if (!videoDatabase) return undefined -- cgit v1.2.3