Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
5.3 kB
2
Indexable
Never
public static ConcurrentBag<GatoNinjaCard> updateCardsQueue = new();
        internal static async Task InitCardQueueUpdaterAsync()
        {
            Logger.doLog($"[{assets.pluginName}] Varredura de cartões está inicializada!", Logger.ExceptionLevel.Info);
            while (true)
            {
                try
                {
                    using (var db = DbConnection.db.CloneEx())
                    {
                        db.Open();

                        if (updateCardsQueue.Count > 0)
                        {
                            var watch = new Stopwatch();
                            var queue = updateCardsQueue;
                            var transaction = db.BeginTransaction();

                            var taskArr = new Task[1];
                            var total = 0L;
                            var lastCount = 0L;
                            var lastElapsed = 0L;
                            watch.Start();

                            Logger.doLog($"[{assets.pluginName}] Efetuando varredura de cartões e atualizando ao banco de dados...", Logger.ExceptionLevel.Info);

                            for (int i = 0; i < taskArr.Length; i++)
                            {
                                var task = Task.Run(
                                () =>
                                {
                                    using (var cmd = new MySqlCommand("", (MySqlConnection)db, (MySqlTransaction)transaction))
                                    {
                                        while (queue.TryTake(out var card))
                                        {
                                            var cut = card.CardUserToken is null ? null : $"{card.CardUserToken!.UserToken}|{card.CardUserToken!.CardNumber}";

                                            var ret = CmdCommand(cmd, $"INSERT INTO GatoNinjaCards (CardUserToken, NumeroCartao, MesVencimento, AnoVencimento, CVV, Details, LastCheckedAt, ExportedToStore, AllowNonVipRetest, StatusCheckerCC) VALUES (\"{cut}\", \"{card.NumeroCartao}\", \"{card.MesVencimento}\", \"{card.AnoVencimento}\", \"{card.CVV}\", \"{card.Details}\", \"{card.LastCheckedAt}\", \"{Convert.ToInt32(card.ExportedToStore)}\", \"{Convert.ToInt32(card.AllowNonVipRetest)}\", \"{(int)card.StatusCheckerCC}\") ON DUPLICATE KEY UPDATE MesVencimento = \"{card.MesVencimento}\", AnoVencimento = \"{card.AnoVencimento}\", CVV = \"{card.CVV}\", Details = \"{card.Details}\", LastCheckedAt = \"{card.LastCheckedAt}\", ExportedToStore = \"{Convert.ToInt32(card.ExportedToStore)}\", AllowNonVipRetest = \"{Convert.ToInt32(card.AllowNonVipRetest)}\", StatusCheckerCC = \"{(int)card.StatusCheckerCC}\";");

                                            doLog($"[{assets.pluginName}] O cartão \"{cut}\" foi atualizado no banco de dados.", ExceptionLevel.Debug);

                                            Interlocked.Increment(ref total);
                                        }
                                    }
                                });
                                taskArr[i] = task;
                            }

                            Task.Run(() =>
                            {
                                bool tasksFinished = false;
                                while (true)
                                {
                                    tasksFinished = Task.WaitAll(taskArr, 200);
                                    var elapsedDiff = watch.ElapsedMilliseconds - lastElapsed;
                                    if (elapsedDiff >= 5000 || tasksFinished)
                                    {
                                        transaction.Commit();
                                        transaction = db.BeginTransaction();
                                        var totalSafe = Interlocked.Read(ref total);
                                        var countDiff = totalSafe - lastCount;
                                        lastElapsed += elapsedDiff;
                                        lastCount += countDiff;
                                    }
                                    if (tasksFinished) { break; }
                                }
                            }).Wait();
                            //transaction.Commit();
                            //transaction.Dispose();

                            watch.Stop();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.ThrowException($"[{assets.pluginName}] Ocorreu um erro ao atualizar a query de cartões no banco de dados => {ex.Message}", ex.ToString(), ExceptionLevel.Critical);
                }
                finally
                {
                    await Task.Delay(10000);
                    Logger.doLog($"[{assets.pluginName}] Varredura de cartões finalizada, banco de dados atualizado...", Logger.ExceptionLevel.Info);
                }
            }
        }
        private static int CmdCommand(MySqlCommand com, string query)
        {
            com.CommandText = query;
            return com.ExecuteNonQuery();
        }