Skip to content

Commit 0ce249d

Browse files
authored
Merge pull request #301 from Sofie-Automation/fix/media-convert-keywords
Fix: Add media-convert keywords
2 parents 538267b + e51cd19 commit 0ce249d

2 files changed

Lines changed: 77 additions & 1 deletion

File tree

shared/packages/api/src/inputApi.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,16 @@ export namespace ExpectedPackage {
201201
*/
202202
needsLocalTarget?: boolean
203203

204+
/** When set, require keywords to be present in the stdOut output of the executable, in order for the conversion to be considered successful. */
205+
requiredStdOut?: string[]
206+
/** When set, require keywords to be present in the stdErr output of the executable, in order for the conversion to be considered successful. */
207+
requiredStdErr?: string[]
208+
209+
/** When set, require keywords to NOT be present in the stdOut output of the executable, in order for the conversion to be considered successful. */
210+
forbiddenStdOut?: string[]
211+
/** When set, require keywords to NOT be present in the stdErr output of the executable, in order for the conversion to be considered successful. */
212+
forbiddenStdErr?: string[]
213+
204214
/**
205215
* Force the output filename from this step.
206216
* This can be useful in multi-step scenarios where you want to specify the inter-step filename.

shared/packages/worker/src/worker/workers/genericWorker/expectationHandlers/mediaFileConvert.ts

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,12 +951,31 @@ class MediaConversionOperation {
951951

952952
try {
953953
await new Promise<void>((resolve, reject) => {
954+
const recordStdOut: string[] = []
955+
const recordStdErr: string[] = []
954956
this.spawnedProcess = spawnProcess(
955957
executable,
956958
args,
957959
() => {
958960
// On Done
959-
resolve()
961+
try {
962+
if (this.conversion.requiredStdOut?.length) {
963+
this.requireStringsInProcessOutput(recordStdOut, this.conversion.requiredStdOut)
964+
}
965+
if (this.conversion.forbiddenStdOut?.length) {
966+
this.requireNoStringsInProcessOutput(recordStdOut, this.conversion.forbiddenStdOut)
967+
}
968+
if (this.conversion.requiredStdErr?.length) {
969+
this.requireStringsInProcessOutput(recordStdErr, this.conversion.requiredStdErr)
970+
}
971+
if (this.conversion.forbiddenStdErr?.length) {
972+
this.requireNoStringsInProcessOutput(recordStdErr, this.conversion.forbiddenStdErr)
973+
}
974+
975+
resolve()
976+
} catch (err) {
977+
reject(err)
978+
}
960979
},
961980
(err) => {
962981
// On Error
@@ -968,6 +987,21 @@ class MediaConversionOperation {
968987
}
969988
// this.logger.silly
970989
)
990+
991+
// Only collect stdout if needed:
992+
if (this.conversion.requiredStdOut?.length || this.conversion.forbiddenStdOut?.length) {
993+
this.spawnedProcess.execProcess.stdout.on('data', (data: Buffer) => {
994+
const str = data.toString()
995+
recordStdOut.push(str)
996+
})
997+
}
998+
// Only collect stderr if needed:
999+
if (this.conversion.requiredStdErr?.length || this.conversion.forbiddenStdErr?.length) {
1000+
this.spawnedProcess.execProcess.stderr.on('data', (data: Buffer) => {
1001+
const str = data.toString()
1002+
recordStdErr.push(str)
1003+
})
1004+
}
9711005
})
9721006
} finally {
9731007
this.spawnedProcess = undefined
@@ -1056,6 +1090,38 @@ class MediaConversionOperation {
10561090
}
10571091
)
10581092
}
1093+
1094+
private limitToLastFewLines(lines: string[], maxLines: number): string {
1095+
return lines.slice(-maxLines).join('\n')
1096+
}
1097+
private requireStringsInProcessOutput(recordStdOut: string[], requiredStrings: string[] | undefined) {
1098+
if (!requiredStrings?.length) return
1099+
1100+
for (const requiredStr of requiredStrings) {
1101+
if (!recordStdOut.some((line) => line.includes(requiredStr))) {
1102+
throw new Error(
1103+
`Required string "${requiredStr}" not found in stdout. Recorded stdout:\n${this.limitToLastFewLines(
1104+
recordStdOut,
1105+
10
1106+
)}`
1107+
)
1108+
}
1109+
}
1110+
}
1111+
private requireNoStringsInProcessOutput(recordStdOut: string[], forbiddenStrings: string[] | undefined) {
1112+
if (!forbiddenStrings?.length) return
1113+
1114+
for (const forbiddenStr of forbiddenStrings) {
1115+
if (recordStdOut.some((line) => line.includes(forbiddenStr))) {
1116+
throw new Error(
1117+
`Forbidden string "${forbiddenStr}" found in stdout. Recorded stdout:\n${this.limitToLastFewLines(
1118+
recordStdOut,
1119+
10
1120+
)}`
1121+
)
1122+
}
1123+
}
1124+
}
10591125
}
10601126
/**
10611127
* This is a pointer to a file, along with the means to access it.

0 commit comments

Comments
 (0)