Skip to content

Commit

Permalink
refactor: refactor container parameter initialization
Browse files Browse the repository at this point in the history
- Refactor container creation parameters to utilize object initializer syntax in `StartRecordContainer` method.
- Incorporate `HostConfig` with `Binds` list initialization directly within container parameters.
- Directly initialize container `Labels`, `NetworkingConfig`, and `Cmd` within the parameters declaration.
- Combine `AttachStdout`, `AttachStdin`, and `AttachStderr` settings declaration with the rest of the container parameters.
- Remove unnecessary instantiation of `ContainerStartParameters` before container start logging.
  • Loading branch information
konnokai committed Mar 15, 2024
1 parent ff34a3a commit 8a5634d
Showing 1 changed file with 49 additions and 46 deletions.
95 changes: 49 additions & 46 deletions YoutubeStreamRecord/Subscribe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -463,64 +463,67 @@ public static async Task<ResultType> SubRecord(string outputPath, string tempPat

private static async Task StartRecordContainer(string videoId, VideoSnippet snippetData, bool dontSendStartMessage)
{
var parms = new CreateContainerParameters();
parms.Image = "jun112561/youtube-record:master";
parms.Name = $"record-{videoId.Replace("@", "-")}-{DateTime.Now:yyyyMMdd-HHmmss}";

parms.Env = new List<string>
{
$"GoogleApiKey={Utility.GetEnvironmentVariable("GoogleApiKey", typeof(string), true)}",
$"RedisOption={Utility.GetEnvironmentVariable("RedisOption", typeof(string), true)}"
};

List<string> binds = new List<string>
var parms = new CreateContainerParameters
{
$"{Utility.GetEnvironmentVariable("RecordPath", typeof(string), true)}:/output",
$"{Utility.GetEnvironmentVariable("TempPath", typeof(string), true)}:/temp_path",
$"{Utility.GetEnvironmentVariable("UnArchivedPath", typeof(string), true)}:/unarchived",
$"{Utility.GetEnvironmentVariable("MemberOnlyPath", typeof(string), true)}:/member_only",
$"{Utility.GetEnvironmentVariable("CookiesFilePath", typeof(string), true)}:/app/cookies.txt"
};
parms.HostConfig = new HostConfig() { Binds = binds };
Image = "jun112561/youtube-record:master",
Name = $"record-{videoId.Replace("@", "-")}-{DateTime.Now:yyyyMMdd-HHmmss}",

parms.Labels = new Dictionary<string, string>
{
{ "me.konnokai.record.video.title", snippetData.Title },
{ "me.konnokai.record.video.id", videoId.Replace("@", "-") },
{ "me.konnokai.record.channel.title", snippetData.ChannelTitle },
{ "me.konnokai.record.channel.id", snippetData.ChannelId }
};
Env = new List<string>
{
$"GoogleApiKey={Utility.GetEnvironmentVariable("GoogleApiKey", typeof(string), true)}",
$"RedisOption={Utility.GetEnvironmentVariable("RedisOption", typeof(string), true)}"
},

parms.NetworkingConfig = new NetworkingConfig()
{
EndpointsConfig = new Dictionary<string, EndpointSettings>()
HostConfig = new HostConfig()
{
{ "" , new EndpointSettings() { NetworkID = NetworkId } }
}
};
Binds = new List<string>()
{
$"{Utility.GetEnvironmentVariable("RecordPath", typeof(string), true)}:/output",
$"{Utility.GetEnvironmentVariable("TempPath", typeof(string), true)}:/temp_path",
$"{Utility.GetEnvironmentVariable("UnArchivedPath", typeof(string), true)}:/unarchived",
$"{Utility.GetEnvironmentVariable("MemberOnlyPath", typeof(string), true)}:/member_only",
$"{Utility.GetEnvironmentVariable("CookiesFilePath", typeof(string), true)}:/app/cookies.txt"
}
},

parms.Cmd = new List<string>
{
"onceondocker",
videoId,
"--disable-live-from-start",
dontSendStartMessage ? "--dont-send-start-message" : ""
};
Labels = new Dictionary<string, string>
{
{ "me.konnokai.record.video.title", snippetData.Title },
{ "me.konnokai.record.video.id", videoId.Replace("@", "-") },
{ "me.konnokai.record.channel.title", snippetData.ChannelTitle },
{ "me.konnokai.record.channel.id", snippetData.ChannelId }
},

// 不要讓程式自己Attach以免Log混亂
parms.AttachStdout = false;
parms.AttachStdin = false;
parms.AttachStderr = false;
NetworkingConfig = new NetworkingConfig()
{
EndpointsConfig = new Dictionary<string, EndpointSettings>()
{
{ "" , new EndpointSettings() { NetworkID = NetworkId } }
}
},

// 允許另外透過其他方法Attach進去交互
parms.OpenStdin = true;
parms.Tty = true;
Cmd = new List<string>
{
"onceondocker",
videoId,
"--disable-live-from-start",
dontSendStartMessage ? "--dont-send-start-message" : ""
},

// 不要讓程式自己Attach以免Log混亂
AttachStdout = false,
AttachStdin = false,
AttachStderr = false,

// 允許另外透過其他方法Attach進去交互
OpenStdin = true,
Tty = true
};

try
{
var containerResponse = await dockerClient.Containers.CreateContainerAsync(parms, CancellationToken.None);
Log.Info($"已建立容器: {containerResponse.ID}");
ContainerStartParameters containerStartParameters = new ContainerStartParameters();

if (containerResponse.Warnings.Any())
Log.Warn($"容器警告: {string.Join('\n', containerResponse.Warnings)}");
Expand Down

0 comments on commit 8a5634d

Please sign in to comment.