Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion packages/mos-gateway/src/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@ import {
PeripheralDeviceId,
loadCertificatesFromDisk,
CertificatesConfig,
stringifyError,
HealthConfig,
HealthEndpoints,
IConnector,
} from '@sofie-automation/server-core-integration'

export interface Config {
certificates: CertificatesConfig
device: DeviceConfig
core: CoreConfig
mos: MosConfig
health: HealthConfig
}
export interface DeviceConfig {
deviceId: PeripheralDeviceId
deviceToken: string
}
export class Connector {
export class Connector implements IConnector {
public initialized = false
public initializedError: string | undefined = undefined

private mosHandler: MosHandler | undefined
private coreHandler: CoreHandler | undefined
private _config: Config | undefined
Expand All @@ -38,11 +46,18 @@ export class Connector {
this._logger.info('Initializing Core...')
await this.initCore(certificates)

if (!this.coreHandler) throw Error('coreHandler is undefined!')

new HealthEndpoints(this, this.coreHandler, config.health)

this._logger.info('Initializing Mos...')
await this.initMos()

this._logger.info('Initialization done')
this.initialized = true
} catch (e: any) {
this.initializedError = stringifyError(e)

this._logger.error('Error during initialization:', e, e.stack)

this._logger.info('Shutting down in 10 seconds!')
Expand Down
53 changes: 36 additions & 17 deletions packages/mos-gateway/src/coreHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
stringifyError,
PeripheralDevicePubSub,
PeripheralDevicePubSubCollectionsNames,
ICoreHandler,
} from '@sofie-automation/server-core-integration'
import * as Winston from 'winston'

Expand All @@ -29,14 +30,16 @@ export interface CoreConfig {
/**
* Represents a connection between mos-integration and Core
*/
export class CoreHandler {
export class CoreHandler implements ICoreHandler {
core: CoreConnection | undefined
logger: Winston.Logger
public _observers: Array<Observer<any>> = []
public connectedToCore = false
private _deviceOptions: DeviceConfig
private _coreMosHandlers: Array<CoreMosDeviceHandler> = []
private _onConnected?: () => any
private _isInitialized = false
private _isDestroyed = false
private _executedFunctions = new Set<PeripheralDeviceCommandId>()
private _coreConfig?: CoreConfig
private _certificates?: Buffer[]
Expand All @@ -54,10 +57,12 @@ export class CoreHandler {

this.core.onConnected(() => {
this.logger.info('Core Connected!')
this.connectedToCore = true
if (this._isInitialized) this.onConnectionRestored()
})
this.core.onDisconnected(() => {
this.logger.info('Core Disconnected!')
this.connectedToCore = false
})
this.core.onError((err) => {
this.logger.error('Core Error: ' + (typeof err === 'string' ? err : err.message || err.toString()))
Expand All @@ -74,31 +79,45 @@ export class CoreHandler {
}
await this.core.init(ddpConfig)

if (!this.core) {
throw Error('core is undefined!')
}

this.core
.setStatus({
statusCode: StatusCode.GOOD,
// messages: []
})
.catch((e) => this.logger.warn('Error when setting status:' + e))
// nothing

await this.setupSubscriptionsAndObservers()

this._isInitialized = true

await this.updateCoreStatus()
}
getCoreStatus(): {
statusCode: StatusCode
messages: string[]
} {
let statusCode = StatusCode.GOOD
const messages: string[] = []

if (!this._isInitialized) {
statusCode = StatusCode.BAD
messages.push('Starting up...')
}
if (this._isDestroyed) {
statusCode = StatusCode.FATAL
messages.push('Shut down')
}
return {
statusCode,
messages,
}
}
async updateCoreStatus(): Promise<void> {
if (!this.core) throw Error('core is undefined!')

await this.core.setStatus(this.getCoreStatus())
}

async dispose(): Promise<void> {
this._isDestroyed = true
if (!this.core) {
throw Error('core is undefined!')
}

await this.core.setStatus({
statusCode: StatusCode.FATAL,
messages: ['Shutting down'],
})
await this.updateCoreStatus()

await Promise.all(
this._coreMosHandlers.map(async (cmh: CoreMosDeviceHandler) => {
Expand Down
6 changes: 6 additions & 0 deletions packages/mos-gateway/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ let deviceToken: string = process.env.DEVICE_TOKEN || ''
let disableWatchdog: boolean = process.env.DISABLE_WATCHDOG === '1' || false
let unsafeSSL: boolean = process.env.UNSAFE_SSL === '1' || false
const certs: string[] = (process.env.CERTIFICATES || '').split(';') || []
let healthPort: number | undefined = parseInt(process.env.HEALTH_PORT + '') || undefined
let debug = false
let printHelp = false

Expand Down Expand Up @@ -46,6 +47,8 @@ process.argv.forEach((val) => {
} else if (val.match(/-unsafeSSL/i)) {
// Will cause the Node applocation to blindly accept all certificates. Not recommenced unless in local, controlled networks.
unsafeSSL = true
} else if (prevProcessArg.match(/-healthPort/i)) {
healthPort = parseInt(val)
}
prevProcessArg = nextPrevProcessArg + ''
})
Expand Down Expand Up @@ -207,6 +210,9 @@ const config: Config = {
port: port,
watchdog: !disableWatchdog,
},
health: {
port: healthPort,
},
mos: {
self: {
debug: debug,
Expand Down
6 changes: 4 additions & 2 deletions packages/playout-gateway/src/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import {
PeripheralDeviceId,
loadCertificatesFromDisk,
stringifyError,
HealthConfig,
HealthEndpoints,
IConnector,
} from '@sofie-automation/server-core-integration'
import { HealthConfig, HealthEndpoints } from './health'

export interface Config {
certificates: CertificatesConfig
Expand All @@ -23,7 +25,7 @@ export interface DeviceConfig {
deviceId: PeripheralDeviceId
deviceToken: string
}
export class Connector {
export class Connector implements IConnector {
public initialized = false
public initializedError: string | undefined = undefined

Expand Down
3 changes: 2 additions & 1 deletion packages/playout-gateway/src/coreHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
stringifyError,
PeripheralDevicePubSub,
PeripheralDevicePubSubCollectionsNames,
ICoreHandler,
} from '@sofie-automation/server-core-integration'
import { MediaObject, DeviceOptionsAny, ActionExecutionResult } from 'timeline-state-resolver'
import * as _ from 'underscore'
Expand Down Expand Up @@ -40,7 +41,7 @@ export interface MemoryUsageReport {
/**
* Represents a connection between the Gateway and Core
*/
export class CoreHandler {
export class CoreHandler implements ICoreHandler {
core!: CoreConnection
logger: Logger
public _observers: Array<Observer<any>> = []
Expand Down
6 changes: 6 additions & 0 deletions packages/server-core-integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,17 @@
"rundown",
"production"
],
"devDependencies": {
"@types/koa": "^3.0.0",
"@types/koa__router": "^12.0.4"
},
"dependencies": {
"@koa/router": "^14.0.0",
"@sofie-automation/shared-lib": "1.52.9-nrk",
"ejson": "^2.2.3",
"faye-websocket": "^0.11.4",
"got": "^11.8.6",
"koa": "^3.0.1",
"tslib": "^2.8.1",
"underscore": "^1.13.7"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/server-core-integration/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export * from './lib/coreConnection'
export * from './lib/configManifest'
export * from './lib/ddpClient'
export * from './lib/gateway-types'
export * from './lib/health'
export * from './lib/methods'
export * from './lib/process'
export { SubscriptionId } from './lib/subscriptions'
Expand Down
11 changes: 11 additions & 0 deletions packages/server-core-integration/src/lib/gateway-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { StatusCode } from '@sofie-automation/shared-lib/dist/lib/status'

export interface IConnector {
initialized: boolean
initializedError: string | undefined
}

export interface ICoreHandler {
getCoreStatus: () => { statusCode: StatusCode; messages: string[] }
connectedToCore: boolean
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as Koa from 'koa'
import * as Router from '@koa/router'
import { StatusCode } from 'timeline-state-resolver'
import { assertNever } from '@sofie-automation/server-core-integration'
import { CoreHandler } from './coreHandler'
import { Connector } from './connector'
import { StatusCode } from '@sofie-automation/shared-lib/dist/lib/status'
import { assertNever } from '@sofie-automation/shared-lib/dist/lib/lib'
import { IConnector, ICoreHandler } from './gateway-types'

export interface HealthConfig {
/** If set, exposes health HTTP endpoints on the given port */
Expand All @@ -16,7 +15,7 @@ export interface HealthConfig {
*/
export class HealthEndpoints {
private app = new Koa()
constructor(private connector: Connector, private coreHandler: CoreHandler, private config: HealthConfig) {
constructor(private connector: IConnector, private coreHandler: ICoreHandler, private config: HealthConfig) {
if (!config.port) return // disabled

const router = new Router()
Expand Down
4 changes: 4 additions & 0 deletions packages/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6064,10 +6064,14 @@ __metadata:
version: 0.0.0-use.local
resolution: "@sofie-automation/server-core-integration@workspace:server-core-integration"
dependencies:
"@koa/router": "npm:^14.0.0"
"@sofie-automation/shared-lib": "npm:1.52.9-nrk"
"@types/koa": "npm:^3.0.0"
"@types/koa__router": "npm:^12.0.4"
ejson: "npm:^2.2.3"
faye-websocket: "npm:^0.11.4"
got: "npm:^11.8.6"
koa: "npm:^3.0.1"
tslib: "npm:^2.8.1"
underscore: "npm:^1.13.7"
languageName: unknown
Expand Down
Loading