From 571389d43b8fc8aaf27e77c06f19b320b08dbbc9 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 10 Nov 2017 17:27:49 +0100 Subject: Make it compile at least --- .../http-request-broadcast-handler.ts | 21 ++++++++++---- .../http-request-job-scheduler.ts | 7 ++++- .../http-request-unicast-handler.ts | 19 +++++++++---- server/lib/jobs/job-scheduler.ts | 33 +++++++++------------- .../transcoding-job-scheduler.ts | 13 ++++++--- .../video-file-optimizer-handler.ts | 17 ++++++----- .../video-file-transcoder-handler.ts | 11 +++----- 7 files changed, 69 insertions(+), 52 deletions(-) (limited to 'server/lib/jobs') diff --git a/server/lib/jobs/http-request-job-scheduler/http-request-broadcast-handler.ts b/server/lib/jobs/http-request-job-scheduler/http-request-broadcast-handler.ts index 6b6946d02..799b86e1c 100644 --- a/server/lib/jobs/http-request-job-scheduler/http-request-broadcast-handler.ts +++ b/server/lib/jobs/http-request-job-scheduler/http-request-broadcast-handler.ts @@ -1,19 +1,28 @@ -import * as Bluebird from 'bluebird' - -import { database as db } from '../../../initializers/database' import { logger } from '../../../helpers' +import { doRequest } from '../../../helpers/requests' +import { HTTPRequestPayload } from './http-request-job-scheduler' + +async function process (payload: HTTPRequestPayload, jobId: number) { + logger.info('Processing broadcast in job %d.', jobId) -async function process (data: { videoUUID: string }, jobId: number) { + const options = { + uri: '', + json: payload.body + } + for (const uri of payload.uris) { + options.uri = uri + await doRequest(options) + } } function onError (err: Error, jobId: number) { - logger.error('Error when optimized video file in job %d.', jobId, err) + logger.error('Error when broadcasting request in job %d.', jobId, err) return Promise.resolve() } async function onSuccess (jobId: number) { - + logger.info('Job %d is a success.', jobId) } // --------------------------------------------------------------------------- diff --git a/server/lib/jobs/http-request-job-scheduler/http-request-job-scheduler.ts b/server/lib/jobs/http-request-job-scheduler/http-request-job-scheduler.ts index 42cb9139c..ad3349866 100644 --- a/server/lib/jobs/http-request-job-scheduler/http-request-job-scheduler.ts +++ b/server/lib/jobs/http-request-job-scheduler/http-request-job-scheduler.ts @@ -4,7 +4,11 @@ import * as httpRequestBroadcastHandler from './http-request-broadcast-handler' import * as httpRequestUnicastHandler from './http-request-unicast-handler' import { JobCategory } from '../../../../shared' -const jobHandlers: { [ handlerName: string ]: JobHandler } = { +type HTTPRequestPayload = { + uris: string[] + body: any +} +const jobHandlers: { [ handlerName: string ]: JobHandler } = { httpRequestBroadcastHandler, httpRequestUnicastHandler } @@ -13,5 +17,6 @@ const jobCategory: JobCategory = 'http-request' const httpRequestJobScheduler = new JobScheduler(jobCategory, jobHandlers) export { + HTTPRequestPayload, httpRequestJobScheduler } diff --git a/server/lib/jobs/http-request-job-scheduler/http-request-unicast-handler.ts b/server/lib/jobs/http-request-job-scheduler/http-request-unicast-handler.ts index 6b6946d02..13451f042 100644 --- a/server/lib/jobs/http-request-job-scheduler/http-request-unicast-handler.ts +++ b/server/lib/jobs/http-request-job-scheduler/http-request-unicast-handler.ts @@ -1,19 +1,26 @@ -import * as Bluebird from 'bluebird' - -import { database as db } from '../../../initializers/database' import { logger } from '../../../helpers' +import { doRequest } from '../../../helpers/requests' +import { HTTPRequestPayload } from './http-request-job-scheduler' + +async function process (payload: HTTPRequestPayload, jobId: number) { + logger.info('Processing unicast in job %d.', jobId) -async function process (data: { videoUUID: string }, jobId: number) { + const uri = payload.uris[0] + const options = { + uri, + json: payload.body + } + await doRequest(options) } function onError (err: Error, jobId: number) { - logger.error('Error when optimized video file in job %d.', jobId, err) + logger.error('Error when sending request in job %d.', jobId, err) return Promise.resolve() } async function onSuccess (jobId: number) { - + logger.info('Job %d is a success.', jobId) } // --------------------------------------------------------------------------- diff --git a/server/lib/jobs/job-scheduler.ts b/server/lib/jobs/job-scheduler.ts index 89a4bca88..f10f745b3 100644 --- a/server/lib/jobs/job-scheduler.ts +++ b/server/lib/jobs/job-scheduler.ts @@ -1,28 +1,22 @@ import { AsyncQueue, forever, queue } from 'async' import * as Sequelize from 'sequelize' - -import { - database as db, - JOBS_FETCHING_INTERVAL, - JOBS_FETCH_LIMIT_PER_CYCLE, - JOB_STATES -} from '../../initializers' +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 { JobCategory } from '../../../shared' -export interface JobHandler { - process (data: object, jobId: number): T +export interface JobHandler { + process (data: object, jobId: number): Promise onError (err: Error, jobId: number) - onSuccess (jobId: number, jobResult: T) + onSuccess (jobId: number, jobResult: T, jobScheduler: JobScheduler) } type JobQueueCallback = (err: Error) => void -class JobScheduler { +class JobScheduler { constructor ( private jobCategory: JobCategory, - private jobHandlers: { [ id: string ]: JobHandler } + private jobHandlers: { [ id: string ]: JobHandler } ) {} async activate () { @@ -66,13 +60,14 @@ class JobScheduler { ) } - createJob (transaction: Sequelize.Transaction, category: JobCategory, handlerName: string, handlerInputData: object) { + createJob (transaction: Sequelize.Transaction, handlerName: string, handlerInputData: P) { const createQuery = { state: JOB_STATES.PENDING, - category, + category: this.jobCategory, handlerName, handlerInputData } + const options = { transaction } return db.Job.create(createQuery, options) @@ -95,7 +90,7 @@ class JobScheduler { await job.save() try { - const result = await jobHandler.process(job.handlerInputData, job.id) + const result: T = await jobHandler.process(job.handlerInputData, job.id) await this.onJobSuccess(jobHandler, job, result) } catch (err) { logger.error('Error in job handler %s.', job.handlerName, err) @@ -111,7 +106,7 @@ class JobScheduler { callback(null) } - private async onJobError (jobHandler: JobHandler, job: JobInstance, err: Error) { + private async onJobError (jobHandler: JobHandler, job: JobInstance, err: Error) { job.state = JOB_STATES.ERROR try { @@ -122,12 +117,12 @@ class JobScheduler { } } - private async onJobSuccess (jobHandler: JobHandler, job: JobInstance, jobResult: any) { + private async onJobSuccess (jobHandler: JobHandler, job: JobInstance, jobResult: T) { job.state = JOB_STATES.SUCCESS try { await job.save() - jobHandler.onSuccess(job.id, jobResult) + jobHandler.onSuccess(job.id, jobResult, this) } catch (err) { this.cannotSaveJobError(err) } 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 d7c614fb8..c5efe8eeb 100644 --- a/server/lib/jobs/transcoding-job-scheduler/transcoding-job-scheduler.ts +++ b/server/lib/jobs/transcoding-job-scheduler/transcoding-job-scheduler.ts @@ -1,10 +1,14 @@ -import { JobScheduler, JobHandler } from '../job-scheduler' - +import { JobCategory } from '../../../../shared' +import { JobHandler, JobScheduler } from '../job-scheduler' import * as videoFileOptimizer from './video-file-optimizer-handler' import * as videoFileTranscoder from './video-file-transcoder-handler' -import { JobCategory } from '../../../../shared' +import { VideoInstance } from '../../../models/video/video-interface' -const jobHandlers: { [ handlerName: string ]: JobHandler } = { +type TranscodingJobPayload = { + videoUUID: string + resolution?: number +} +const jobHandlers: { [ handlerName: string ]: JobHandler } = { videoFileOptimizer, videoFileTranscoder } @@ -13,5 +17,6 @@ const jobCategory: JobCategory = 'transcoding' const transcodingJobScheduler = new JobScheduler(jobCategory, jobHandlers) export { + TranscodingJobPayload, transcodingJobScheduler } 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 f019c28bc..47603a66c 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,12 +1,13 @@ import * as Bluebird from 'bluebird' +import { computeResolutionsToTranscode, logger } from '../../../helpers' import { database as db } from '../../../initializers/database' -import { logger, computeResolutionsToTranscode } from '../../../helpers' import { VideoInstance } from '../../../models' -import { addVideoToFriends } from '../../friends' +import { sendAddVideo } from '../../activitypub/send-request' import { JobScheduler } from '../job-scheduler' +import { TranscodingJobPayload } from './transcoding-job-scheduler' -async function process (data: { videoUUID: string }, jobId: number) { +async function process (data: TranscodingJobPayload, jobId: number) { const video = await db.Video.loadByUUIDAndPopulateAccountAndPodAndTags(data.videoUUID) // No video, maybe deleted? if (!video) { @@ -24,7 +25,7 @@ function onError (err: Error, jobId: number) { return Promise.resolve() } -async function onSuccess (jobId: number, video: VideoInstance) { +async function onSuccess (jobId: number, video: VideoInstance, jobScheduler: JobScheduler) { if (video === undefined) return undefined logger.info('Job %d is a success.', jobId) @@ -34,10 +35,8 @@ async function onSuccess (jobId: number, video: VideoInstance) { // Video does not exist anymore if (!videoDatabase) return undefined - const remoteVideo = await videoDatabase.toAddRemoteJSON() - - // Now we'll add the video's meta data to our friends - await addVideoToFriends(remoteVideo, null) + // Now we'll add the video's meta data to our followers + await sendAddVideo(video, undefined) const originalFileHeight = await videoDatabase.getOriginalFileHeight() // Create transcoding jobs if there are enabled resolutions @@ -59,7 +58,7 @@ async function onSuccess (jobId: number, video: VideoInstance) { resolution } - const p = JobScheduler.Instance.createJob(t, 'videoFileTranscoder', dataInput) + const p = jobScheduler.createJob(t, 'videoFileTranscoder', dataInput) tasks.push(p) } 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 397b95795..77e5d9f7f 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,8 +1,8 @@ -import { database as db } from '../../../initializers/database' -import { updateVideoToFriends } from '../../friends' +import { VideoResolution } from '../../../../shared' import { logger } from '../../../helpers' +import { database as db } from '../../../initializers/database' import { VideoInstance } from '../../../models' -import { VideoResolution } from '../../../../shared' +import { sendUpdateVideo } from '../../activitypub/send-request' async function process (data: { videoUUID: string, resolution: VideoResolution }, jobId: number) { const video = await db.Video.loadByUUIDAndPopulateAccountAndPodAndTags(data.videoUUID) @@ -32,10 +32,7 @@ async function onSuccess (jobId: number, video: VideoInstance) { // Video does not exist anymore if (!videoDatabase) return undefined - const remoteVideo = videoDatabase.toUpdateRemoteJSON() - - // Now we'll add the video's meta data to our friends - await updateVideoToFriends(remoteVideo, null) + await sendUpdateVideo(video, undefined) return undefined } -- cgit v1.2.3