Skip to content

Commit

Permalink
Add channel event deletion to clean up outdated resources; remove unn…
Browse files Browse the repository at this point in the history
…eeded checks
  • Loading branch information
Nick Cook authored and grayside committed Jan 18, 2023
1 parent e44aa88 commit eaff4be
Showing 1 changed file with 19 additions and 34 deletions.
53 changes: 19 additions & 34 deletions media/livestream/test/livestream.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,55 +38,40 @@ const outputUri = `gs://${bucketName}/test-output-channel/`;
const cwd = path.join(__dirname, '..');

before(async () => {
// Stop the channel if it already exists
try {
execSync(`node stopChannel.js ${projectId} ${location} ${channelId}`, {
cwd,
});
} catch (err) {
// Ignore not found or not started error
}

// Delete the channel if it already exists
try {
execSync(`node deleteChannel.js ${projectId} ${location} ${channelId}`, {
cwd,
});
} catch (err) {
// Ignore not found error
}

// Delete the default input if it already exists
try {
execSync(`node deleteInput.js ${projectId} ${location} ${inputId}`, {
cwd,
});
} catch (err) {
// Ignore not found error
}
});

after(async () => {
// Delete outstanding channels and inputs created more than 3 days ago
// Delete outstanding channels and inputs created more than 3 hours ago
const {LivestreamServiceClient} = require('@google-cloud/livestream').v1;
const livestreamServiceClient = new LivestreamServiceClient();
const THREE_DAYS_IN_SEC = 60 * 60 * 24 * 3;
const THREE_HOURS_IN_SEC = 60 * 60 * 3;
const DATE_NOW_SEC = Math.floor(Date.now() / 1000);

const [channels] = await livestreamServiceClient.listChannels({
parent: livestreamServiceClient.locationPath(projectId, location),
});
for (const channel of channels) {
if (channel.createTime.seconds < DATE_NOW_SEC - THREE_DAYS_IN_SEC) {
if (channel.createTime.seconds < DATE_NOW_SEC - THREE_HOURS_IN_SEC) {
const request = {
name: channel.name,
};
try {
await livestreamServiceClient.stopChannel(request);
const [operation] = await livestreamServiceClient.stopChannel(request);
await operation.promise();
} catch (err) {
//Ignore error when channel is not started.
console.log(
'Existing channel already stopped. Ignore the following error.'
);
console.log(err);
}

const [events] = await livestreamServiceClient.listEvents({
parent: channel.name,
});

for (const event of events) {
await livestreamServiceClient.deleteEvent({
name: event.name,
});
}
await livestreamServiceClient.deleteChannel(request);
}
}
Expand All @@ -95,7 +80,7 @@ after(async () => {
parent: livestreamServiceClient.locationPath(projectId, location),
});
for (const input of inputs) {
if (input.createTime.seconds < DATE_NOW_SEC - THREE_DAYS_IN_SEC) {
if (input.createTime.seconds < DATE_NOW_SEC - THREE_HOURS_IN_SEC) {
const request = {
name: input.name,
};
Expand Down

0 comments on commit eaff4be

Please sign in to comment.