-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from dfpc-coe/config-cron
Config Cron
- Loading branch information
Showing
8 changed files
with
2,720 additions
and
1,408 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
FROM bluenviron/mediamtx:1.9.0-ffmpeg | ||
|
||
ENV MEDIA_VERSION=1.9.1 | ||
ENV MEDIA_VERSION=1.9.3 | ||
|
||
RUN apk add bash vim yq | ||
RUN apk add bash vim yq nodejs npm | ||
|
||
COPY mediamtx.yml /mediamtx.base.yml | ||
ADD mediamtx.yml /mediamtx.base.yml | ||
ADD start / | ||
|
||
COPY start / | ||
ADD package.json / | ||
ADD package-lock.json / | ||
|
||
RUN npm install | ||
|
||
ADD persist.ts / | ||
|
||
ENTRYPOINT [ "/start" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import fs from 'node:fs/promises'; | ||
import cron from 'node-cron'; | ||
import YAML from 'yaml'; | ||
|
||
cron.schedule('0,10,20,30,40,50 * * * * *', async () => { | ||
try { | ||
const base = await globalConfig(); | ||
const paths = (await globalPaths()).map((path) => { | ||
return { | ||
name: path.name, | ||
source: path.source, | ||
sourceOnDemand: path.sourceOnDemand | ||
}; | ||
}); | ||
|
||
base.paths = {}; | ||
|
||
for (const path of paths) { | ||
base.paths[path.name] = path; | ||
} | ||
|
||
console.error('PATHS', JSON.stringify(base.paths)); | ||
|
||
let config = YAML.stringify(base, (key, value) => { | ||
if (typeof value === 'boolean') { | ||
return value === true ? 'yes' : 'no'; | ||
} else { | ||
return value; | ||
} | ||
}); | ||
|
||
// This is janky but MediaMTX wants `no` as a string and not a boolean | ||
// and I can't get the YAML library to respect that... | ||
config = config.split('\n').map((line) => { | ||
line = line.replace(/^encryption: no/, 'encryption: "no"'); | ||
line = line.replace(/^rtmpEncryption: no/, 'rtmpEncryption: "no"'); | ||
return line; | ||
}).join('\n'); | ||
|
||
await fs.writeFile('/opt/mediamtx/mediamtx.yml.new', config); | ||
|
||
// Ref: https://github.com/bluenviron/mediamtx/issues/937 | ||
await fs.rename( | ||
'/opt/mediamtx/mediamtx.yml.new', | ||
'/opt/mediamtx/mediamtx.yml' | ||
); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}); | ||
|
||
async function globalPaths(): any { | ||
let total = 0; | ||
let page = -1; | ||
|
||
const paths = []; | ||
|
||
do { | ||
++page; | ||
|
||
const url = new URL('http://localhost:9997/v3/config/paths/list'); | ||
url.searchParams.append('itemsPerPage', '1000'); | ||
url.searchParams.append('page', String(page)); | ||
|
||
const res = await fetch(url, { | ||
method: 'GET', | ||
headers: { | ||
Authorization: `Basic ${Buffer.from('management:' + process.env.MANAGEMENT_PASSWORD).toString('base64')}` | ||
} | ||
}); | ||
|
||
const body = await res.json(); | ||
|
||
total = body.itemCount; | ||
|
||
paths.push(...body.items); | ||
} while (total > page * 1000); | ||
|
||
return paths; | ||
} | ||
|
||
async function globalConfig(): any { | ||
const res = await fetch('http://localhost:9997/v3/config/global/get', { | ||
method: 'GET', | ||
headers: { | ||
Authorization: `Basic ${Buffer.from('management:' + process.env.MANAGEMENT_PASSWORD).toString('base64')}` | ||
} | ||
}); | ||
|
||
const body = await res.json(); | ||
|
||
return body; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters