Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SOF-1670 Expose Endpoint To Reload Rundown #88

Merged
merged 10 commits into from
Nov 3, 2023
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,19 @@
"dependencies": {
"@sofie-automation/blueprints-integration": "npm:@tv2media/[email protected]",
"@sofie-automation/server-core-integration": "npm:@tv2media/[email protected]",
"@tv2media/logger": "^1.2.4",
"@sofie-automation/shared-lib": "npm:@tv2media/[email protected]",
"@tv2media/logger": "^1.2.4",
"@types/dotenv": "^6.1.1",
"@types/koa": "^2.13.10",
"@types/koa-router": "^7.4.6",
"@types/xml2js": "^0.4.4",
"async-mutex": "^0.3.1",
"cross-env": "^7.0.3",
"dotenv": "^8.0.0",
"inews": "npm:@tv2media/[email protected]",
"jobs-queue": "git+https://github.com/sparkpunkd/node-jobs-queue.git#master",
"koa": "^2.14.2",
"koa-router": "^12.0.1",
"lodash.clonedeep": "^4.5.0",
"tslib": "^2.0.0",
"underscore": "^1.9.1",
Expand Down
3 changes: 1 addition & 2 deletions src/classes/RundownWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,8 @@ export class RundownWatcher extends EventEmitter {
const rundown = this.rundowns.get(rundownExternalId)

if (!playlist || !rundown) {
this.logger.error(`Rundown ${rundownExternalId} does not exist in playlist ${playlistExternalId}`)
release()
return
throw new Error(`Rundown ${rundownExternalId} does not exist in playlist ${playlistExternalId}`)
}

// Delete cached data for this rundown
Expand Down
44 changes: 44 additions & 0 deletions src/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { Observer } from '@sofie-automation/server-core-integration'
import { ensureLogLevel, setLogLevel } from './logger'
import { ILogger as Logger } from '@tv2media/logger'
import { unprotectString } from '@sofie-automation/shared-lib/dist/lib/protectedString'
import * as Koa from 'koa'
import * as KoaRouter from 'koa-router'

export interface Config {
process: ProcessConfig
Expand All @@ -22,6 +24,15 @@ export interface DeviceConfig {
deviceId: string
deviceToken: string
}

interface KoaContext {
params: Record<string, string>
status: number
response: {
body: string
}
}

export class Connector {
private iNewsFTPHandler: InewsFTPHandler
private _observers: Array<Observer> = []
Expand All @@ -31,6 +42,8 @@ export class Connector {
private _process: Process
private _settings?: INewsDeviceSettings
private _debug: boolean
private koaApp: Koa
private koaRouter: KoaRouter

constructor(logger: Logger, config: Config, debug: boolean) {
this._logger = logger.tag(this.constructor.name)
Expand All @@ -40,6 +53,8 @@ export class Connector {
this.coreHandler = new CoreHandler(this._logger, this._config.device)
this.iNewsFTPHandler = new InewsFTPHandler(this._logger, this.coreHandler)
this.coreHandler.iNewsHandler = this.iNewsFTPHandler
this.koaApp = new Koa()
this.koaRouter = new KoaRouter()
}

async init(): Promise<void> {
Expand All @@ -52,6 +67,7 @@ export class Connector {
this._logger.info('Core is initialized')
this.setupObserver()
this._logger.info('Initialization of FTP-monitor done')
this.setupReloadDataKoaEndpoint()
} catch (err) {
this._logger.data(err).error(`Error during initialization:`)

Expand Down Expand Up @@ -134,4 +150,32 @@ export class Connector {

addedChanged(unprotectString(this.coreHandler.core.deviceId))
}

private setupReloadDataKoaEndpoint(): void {
const KOA_PORT: number = 3007
const RUNDOWN_EXTERNAL_ID_SUFFIX: string = '_1'

this.koaRouter.post(
'/rundowns/:rundownName/reingest-data',
async (context: KoaContext ): Promise<void> => {
const rundownName: string = context.params.rundownName
if (!this.iNewsFTPHandler.iNewsWatcher) {
context.status = 503
context.response.body = 'Error: iNewsWatcher is undefined'
return
}

try {
await this.iNewsFTPHandler.iNewsWatcher.ResyncRundown(rundownName + RUNDOWN_EXTERNAL_ID_SUFFIX)
context.response.body = `Reingested data for rundown with name ${rundownName}`
} catch (error) {
context.status = 500
context.response.body = `Error: ${error}`
}
}
)

this.koaApp.use(this.koaRouter.routes()).use(this.koaRouter.allowedMethods())
this.koaApp.listen(KOA_PORT, () => {})
}
}
1 change: 1 addition & 0 deletions src/coreHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ export class CoreHandler {
this.logger.info(`Reloading rundown: ${rundownId}`)
if (this.iNewsHandler?.iNewsWatcher) {
await this.iNewsHandler.iNewsWatcher.ResyncRundown(rundownId)
.catch(error => this.logger.data(error).error(`Failed reloading rundown with rundown id '${rundownId}'.`))
}
return null
}
Expand Down
Loading
Loading