Skip to content

Commit

Permalink
v5.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
trueai-org committed Aug 22, 2024
1 parent c9c6a66 commit d17acd3
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 113 deletions.
6 changes: 4 additions & 2 deletions src/Midjourney.API/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,10 @@ public async Task<Result> AccountAdd([FromBody] DiscordAccountConfig accountConf
return Result.Fail("演示模式,禁止操作");
}

var model = DbHelper.AccountStore.GetCollection().Query()
.Where(c => c.ChannelId == accountConfig.ChannelId).FirstOrDefault();
var model = DbHelper.AccountStore.GetCollection()
.Query()
.Where(c => c.ChannelId == accountConfig.ChannelId)
.FirstOrDefault();

if (model != null)
{
Expand Down
187 changes: 96 additions & 91 deletions src/Midjourney.API/DiscordAccountInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ public async Task StartAsync(CancellationToken cancellationToken)
MongoAutoMigrate();
}


var oss = GlobalConfiguration.Setting.AliyunOss;
if (oss?.Enable == true && oss?.IsAutoMigrationLocalFile == true)
{
Expand Down Expand Up @@ -509,7 +508,7 @@ public static void CheckAndDeleteOldDocuments()
/// <returns></returns>
public async Task Initialize(params DiscordAccountConfig[] appends)
{
var isLock = await AsyncLocalLock.TryLockAsync("Initialize:all", TimeSpan.FromSeconds(10), async () =>
var isLock = await AsyncLocalLock.TryLockAsync("initialize:all", TimeSpan.FromSeconds(10), async () =>
{
var db = DbHelper.AccountStore;
var accounts = db.GetAll().OrderBy(c => c.Sort).ToList();
Expand Down Expand Up @@ -559,7 +558,7 @@ public async Task Initialize(params DiscordAccountConfig[] appends)
});
if (!isLock)
{
throw new LogicException("初始化中,请稍后重拾");
throw new LogicException("初始化中,请稍后重试");
}

await Task.CompletedTask;
Expand All @@ -575,10 +574,18 @@ public async Task StartCheckAccount(DiscordAccount account)
return;
}

var isLock = await AsyncLocalLock.TryLockAsync($"Initialize:{account.Id}", TimeSpan.FromSeconds(5), async () =>
var isLock = await AsyncLocalLock.TryLockAsync($"initialize:{account.Id}", TimeSpan.FromSeconds(5), async () =>
{
var db = DbHelper.AccountStore;
Infrastructure.LoadBalancer.DiscordInstance disInstance = null;

// 获取获取值
account = db.Get(account.Id)!;
if (account.Enable != true)
{
return;
}

DiscordInstance disInstance = null;
try
{
disInstance = _discordLoadBalancer.GetDiscordInstance(account.ChannelId);
Expand Down Expand Up @@ -648,7 +655,7 @@ public async Task StartCheckAccount(DiscordAccount account)
}

account.DayDrawCount = dayCount;
db.Update(account);
db.Update("SubChannels,SubChannelValues,FastExhausted,DayDrawCount", account);

// 连接前先判断账号是否正常
var success = await _discordAccountHelper.ValidateAccount(account);
Expand All @@ -658,7 +665,7 @@ public async Task StartCheckAccount(DiscordAccount account)
}

disInstance = await _discordAccountHelper.CreateDiscordInstance(account);
_discordLoadBalancer.AddInstance((Infrastructure.LoadBalancer.DiscordInstance)disInstance);
_discordLoadBalancer.AddInstance(disInstance);

// 这里应该等待初始化完成,并获取用户信息验证,获取用户成功后设置为可用状态
// 多账号启动时,等待一段时间再启动下一个账号
Expand All @@ -673,7 +680,7 @@ public async Task StartCheckAccount(DiscordAccount account)
if (disInstance?.Account != null && disInstance.Account.FastExhausted)
{
// 每 3~6 小时,和启动时检查账号快速用量是否用完了
if (disInstance.Account.InfoUpdated == null || disInstance.Account.InfoUpdated.Value.AddMinutes((double)5) < DateTime.Now)
if (disInstance.Account.InfoUpdated == null || disInstance.Account.InfoUpdated.Value.AddMinutes(5) < DateTime.Now)
{
// 检查账号快速用量是否用完了
// 随机 3~6 小时,执行一次
Expand Down Expand Up @@ -702,16 +709,16 @@ await _memoryCache.GetOrCreateAsync(key, async c =>
}

// 判断 info 检查时间是否在 5 分钟内
if (disInstance.Account.InfoUpdated != null && disInstance.Account.InfoUpdated.Value.AddMinutes((double)5) >= DateTime.Now)
if (disInstance.Account.InfoUpdated != null && disInstance.Account.InfoUpdated.Value.AddMinutes(5) >= DateTime.Now)
{
// 提取 fastime
// 如果检查完之后,快速超过 1 小时,则标记为快速未用完
var fastTime = disInstance.Account.FastTimeRemaining?.ToString()?.Split('/')?.FirstOrDefault<string>()?.Trim();
if (!string.IsNullOrWhiteSpace((string)fastTime) && double.TryParse((string)fastTime, out var ftime) && ftime >= 1)
var fastTime = disInstance.Account.FastTimeRemaining?.ToString()?.Split('/')?.FirstOrDefault()?.Trim();
if (!string.IsNullOrWhiteSpace(fastTime) && double.TryParse(fastTime, out var ftime) && ftime >= 1)
{
// 标记未用完快速
disInstance.Account.FastExhausted = false;
db.Update((DiscordAccount)disInstance.Account);
db.Update("FastExhausted", disInstance.Account);

disInstance.ClearAccountCache(account.Id);

Expand Down Expand Up @@ -744,10 +751,9 @@ await _memoryCache.GetOrCreateAsync(key, async c =>
disInstance = null;
}
});

if (!isLock)
{
throw new LogicException("初始化中,请稍后重拾");
throw new LogicException("初始化中,请稍后重试");
}

await Task.CompletedTask;
Expand All @@ -759,98 +765,97 @@ await _memoryCache.GetOrCreateAsync(key, async c =>
/// <param name="param"></param>
public void UpdateAccount(DiscordAccount param)
{
DiscordAccount model = null;

var disInstance = _discordLoadBalancer.GetDiscordInstance(param.ChannelId);
if (disInstance != null)
{
model = disInstance.Account;
}

var model = DbHelper.AccountStore.Get(param.Id);
if (model == null)
{
model = DbHelper.AccountStore.Get(param.Id);
throw new LogicException("账号不存在");
}

if (model == null)
// 更新一定要加锁,因为其他进程会修改 account 值,导致值覆盖
var isLock = AsyncLocalLock.TryLock($"initialize:{param.Id}", TimeSpan.FromSeconds(3), () =>
{
throw new LogicException("账号不存在");
}
model = DbHelper.AccountStore.Get(param.Id)!;

// 渠道 ID 和 服务器 ID 禁止修改
//model.ChannelId = account.ChannelId;
//model.GuildId = account.GuildId;
// 渠道 ID 和 服务器 ID 禁止修改
//model.ChannelId = account.ChannelId;
//model.GuildId = account.GuildId;

// 更新账号重连时,自动解锁
model.Lock = false;
model.CfHashCreated = null;
model.CfHashUrl = null;
model.CfUrl = null;
// 更新账号重连时,自动解锁
model.Lock = false;
model.CfHashCreated = null;
model.CfHashUrl = null;
model.CfUrl = null;

// 验证 Interval
if (param.Interval < 1.2m)
{
param.Interval = 1.2m;
}
// 验证 Interval
if (param.Interval < 1.2m)
{
param.Interval = 1.2m;
}

// 验证 WorkTime
if (!string.IsNullOrEmpty(param.WorkTime))
{
var ts = param.WorkTime.ToTimeSlots();
if (ts.Count == 0)
// 验证 WorkTime
if (!string.IsNullOrEmpty(param.WorkTime))
{
param.WorkTime = null;
var ts = param.WorkTime.ToTimeSlots();
if (ts.Count == 0)
{
param.WorkTime = null;
}
}
}

// 验证 FishingTime
if (!string.IsNullOrEmpty(param.FishingTime))
{
var ts = param.FishingTime.ToTimeSlots();
if (ts.Count == 0)
// 验证 FishingTime
if (!string.IsNullOrEmpty(param.FishingTime))
{
param.FishingTime = null;
var ts = param.FishingTime.ToTimeSlots();
if (ts.Count == 0)
{
param.FishingTime = null;
}
}
}

model.EnableFastToRelax = param.EnableFastToRelax;
model.IsBlend = param.IsBlend;
model.IsDescribe = param.IsDescribe;
model.IsShorten = param.IsShorten;
model.DayDrawLimit = param.DayDrawLimit;
model.IsVerticalDomain = param.IsVerticalDomain;
model.VerticalDomainIds = param.VerticalDomainIds;
model.SubChannels = param.SubChannels;

model.PermanentInvitationLink = param.PermanentInvitationLink;
model.FishingTime = param.FishingTime;
model.EnableNiji = param.EnableNiji;
model.EnableMj = param.EnableMj;
model.AllowModes = param.AllowModes;
model.WorkTime = param.WorkTime;
model.Interval = param.Interval;
model.AfterIntervalMin = param.AfterIntervalMin;
model.AfterIntervalMax = param.AfterIntervalMax;
model.Sort = param.Sort;
model.Enable = param.Enable;
model.PrivateChannelId = param.PrivateChannelId;
model.NijiBotChannelId = param.NijiBotChannelId;
model.UserAgent = param.UserAgent;
model.RemixAutoSubmit = param.RemixAutoSubmit;
model.CoreSize = param.CoreSize;
model.QueueSize = param.QueueSize;
model.MaxQueueSize = param.MaxQueueSize;
model.TimeoutMinutes = param.TimeoutMinutes;
model.Weight = param.Weight;
model.Remark = param.Remark;
model.BotToken = param.BotToken;
model.UserToken = param.UserToken;
model.Mode = param.Mode;
model.Sponsor = param.Sponsor;

DbHelper.AccountStore.Update(model);

disInstance?.ClearAccountCache(model.Id);
model.EnableFastToRelax = param.EnableFastToRelax;
model.IsBlend = param.IsBlend;
model.IsDescribe = param.IsDescribe;
model.IsShorten = param.IsShorten;
model.DayDrawLimit = param.DayDrawLimit;
model.IsVerticalDomain = param.IsVerticalDomain;
model.VerticalDomainIds = param.VerticalDomainIds;
model.SubChannels = param.SubChannels;

model.PermanentInvitationLink = param.PermanentInvitationLink;
model.FishingTime = param.FishingTime;
model.EnableNiji = param.EnableNiji;
model.EnableMj = param.EnableMj;
model.AllowModes = param.AllowModes;
model.WorkTime = param.WorkTime;
model.Interval = param.Interval;
model.AfterIntervalMin = param.AfterIntervalMin;
model.AfterIntervalMax = param.AfterIntervalMax;
model.Sort = param.Sort;
model.Enable = param.Enable;
model.PrivateChannelId = param.PrivateChannelId;
model.NijiBotChannelId = param.NijiBotChannelId;
model.UserAgent = param.UserAgent;
model.RemixAutoSubmit = param.RemixAutoSubmit;
model.CoreSize = param.CoreSize;
model.QueueSize = param.QueueSize;
model.MaxQueueSize = param.MaxQueueSize;
model.TimeoutMinutes = param.TimeoutMinutes;
model.Weight = param.Weight;
model.Remark = param.Remark;
model.BotToken = param.BotToken;
model.UserToken = param.UserToken;
model.Mode = param.Mode;
model.Sponsor = param.Sponsor;

DbHelper.AccountStore.Update(model);

var disInstance = _discordLoadBalancer.GetDiscordInstance(model.ChannelId);
disInstance?.ClearAccountCache(model.Id);
});
if (!isLock)
{
throw new LogicException("作业执行中,请稍后重试");
}
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/Midjourney.API/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// The use of this software for any form of illegal face swapping,
// invasion of privacy, or any other unlawful purposes is strictly prohibited.
// Violation of these terms may result in termination of the license and may subject the violator to legal action.

global using Midjourney.Infrastructure;
global using Midjourney.Infrastructure.Models;

Expand Down
22 changes: 11 additions & 11 deletions src/Midjourney.Infrastructure/BotMessageListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public void OnMessage(JsonElement raw)
Account.CfHashUrl = hashUrl;
Account.CfHashCreated = DateTime.Now;

DbHelper.AccountStore.Save(Account);
DbHelper.AccountStore.Update(Account);
_discordInstance.ClearAccountCache(Account.Id);

try
Expand Down Expand Up @@ -405,7 +405,7 @@ public void OnMessage(JsonElement raw)

Account.DisabledReason = "CF 人工验证...";

DbHelper.AccountStore.Save(Account);
DbHelper.AccountStore.Update(Account);
_discordInstance.ClearAccountCache(Account.Id);
}
}
Expand Down Expand Up @@ -525,7 +525,7 @@ public void OnMessage(JsonElement raw)
}
}

DbHelper.AccountStore.Save(Account);
DbHelper.AccountStore.Update("Components,NijiComponents", Account);
_discordInstance.ClearAccountCache(Account.Id);

return;
Expand All @@ -540,13 +540,13 @@ public void OnMessage(JsonElement raw)
if (applicationId == Constants.NIJI_APPLICATION_ID)
{
Account.NijiComponents = eventDataMsg.Components;
DbHelper.AccountStore.Update(Account);
DbHelper.AccountStore.Update("NijiComponents", Account);
_discordInstance.ClearAccountCache(Account.Id);
}
else if (applicationId == Constants.MJ_APPLICATION_ID)
{
Account.Components = eventDataMsg.Components;
DbHelper.AccountStore.Update(Account);
DbHelper.AccountStore.Update("Components", Account);
_discordInstance.ClearAccountCache(Account.Id);
}
}
Expand Down Expand Up @@ -781,7 +781,7 @@ public void OnMessage(JsonElement raw)

// 标记快速模式已经用完了
Account.FastExhausted = true;
DbHelper.AccountStore.Save(Account);
DbHelper.AccountStore.Update("FastExhausted", Account);
_discordInstance?.ClearAccountCache(Account.Id);

// 如果开启自动切换慢速模式
Expand All @@ -805,7 +805,7 @@ public void OnMessage(JsonElement raw)
Account.Enable = false;
Account.DisabledReason = "账号用量已经用完";

DbHelper.AccountStore.Save(Account);
DbHelper.AccountStore.Update(Account);
_discordInstance?.ClearAccountCache(Account.Id);
_discordInstance?.Dispose();

Expand Down Expand Up @@ -854,7 +854,7 @@ public void OnMessage(JsonElement raw)
Account.Enable = false;
Account.DisabledReason = title;

DbHelper.AccountStore.Save(Account);
DbHelper.AccountStore.Update(Account);

_discordInstance?.ClearAccountCache(Account.Id);
_discordInstance?.Dispose();
Expand Down Expand Up @@ -967,7 +967,7 @@ public void OnMessage(JsonElement raw)
var db = DbHelper.AccountStore;
Account.InfoUpdated = DateTime.Now;

db.Update(Account);
db.Update("InfoUpdated", Account);
_discordInstance?.ClearAccountCache(Account.Id);
}
}
Expand All @@ -987,15 +987,15 @@ public void OnMessage(JsonElement raw)
Account.NijiComponents = eventDataMsg.Components;
Account.NijiSettingsMessageId = id;

DbHelper.AccountStore.Update(Account);
DbHelper.AccountStore.Update("NijiComponents,NijiSettingsMessageId", Account);
_discordInstance?.ClearAccountCache(Account.Id);
}
else if (applicationId == Constants.MJ_APPLICATION_ID)
{
Account.Components = eventDataMsg.Components;
Account.SettingsMessageId = id;

DbHelper.AccountStore.Update(Account);
DbHelper.AccountStore.Update("Components,SettingsMessageId", Account);
_discordInstance?.ClearAccountCache(Account.Id);
}
}
Expand Down
Loading

0 comments on commit d17acd3

Please sign in to comment.