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
24 changes: 24 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 @@ -31,6 +33,9 @@ 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 +45,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 +59,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 +142,20 @@ export class Connector {

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

private setupReloadDataKoaEndpoint(): void {
this.koaRouter.get('/reloadData/:rundownName', async (ctx, next): Promise<void> => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I guess ctx stands for context?
  2. Do we have types for ctx and next?
  3. What is the response, when calling this endpoint? And is it formatted such that the caller can, in a structured way, decipher it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. True, it is short for context.
  2. We do have types, which is e.g. next: Koa.next and that would be nice to add but i decided not to since the type for context: ctx: Koa.ParameterizedContext<any, KoaRouter.IRouterParamContext<any, {}>, any>.
  3. I guess there really shouldn't be a specific response other than maybe success or "failure".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use a custom type that encapsulates the data that you need from the koa context.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps another method than GET should be used, since we are not reading the data out, but triggering a computation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did think about this, what is more correct in this scenario? POST and PUT, for instance, would signify that the we actually submit something rather than trigger a computation, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now it has been changed to a POST.

const RUNDOWN_EXTERNAL_ID_SUFFIX = '_1'
if (!this.iNewsFTPHandler.iNewsWatcher) {
return
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case where we can't reload the data, wouldn't the caller be interested in knowing that the rundown has not been reloaded?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added more handling towards this purpose!

this.iNewsFTPHandler.iNewsWatcher?.ResyncRundown(ctx.params.rundownName + RUNDOWN_EXTERNAL_ID_SUFFIX)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need the ?? You just checked that you had an iNewsWatcher on line 149.
If you still need it, then you shouldn't need the if-statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A mistake on my part, i must have forgot to remove it after i made the guard for that very purpose :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have changed it!


await next()
})

this.koaApp.use(this.koaRouter.routes()).use(this.koaRouter.allowedMethods())

this.koaApp.listen(3007, () => {})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice if the port was also in a constant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved into a constant KOA_PORT.

}
}
Loading
Loading