Beispiel #1
0
        protected override void StartModuleCycle(object state)
        {
            IsModuleRunning = true;
            string latestJsonStr = null;

            try {
                Utils.Log("CheckAPIStart2 Started a new checking cycle.", "ApiStart2Module", ConsoleColor.DarkGreen);
                var tt = new TimeTracker("start");
                if (!m_offlineDebug)
                {
                    latestJsonStr = UpdateApiStart2Json().Result; // return valid json or error code
                }
                else
                {
                    m_server      = "203.104.209.55";
                    latestJsonStr = File.ReadAllText(Path.Combine(m_exportPath, "api_start2.debug.json"));
                }
                tt.AddLap("end");
                if (latestJsonStr != null && latestJsonStr.Length > 3)
                {
                    Utils.Log("Latest api_start2 JSON downloaded. Time elapsed: " + tt.GetLapTimeDiff("start", "end", TimeTracker.TimeFormat.Seconds) + "s.", "ApiStart2Module");
                    var existingJsonFile = Path.Combine(m_exportPath, "api_start2.latest.json");
                    if (File.Exists(existingJsonFile))
                    {
                        string  existingJsonStr = File.ReadAllText(existingJsonFile);
                        dynamic oldJson         = JValue.Parse(existingJsonStr);
                        dynamic newJson         = JValue.Parse(latestJsonStr);
                        if (!JToken.DeepEquals(oldJson, newJson))
                        {
                            Utils.Log("Found newer api_start2, achieve and update to latest version.", "ApiStart2Module", ConsoleColor.Red, ConsoleColor.White);
                            var utcDate     = DateTime.UtcNow.ToString("yyyyMMdd");
                            var newFilePath = Path.Combine(m_exportPath, string.Format("api_start2.{0}.json", utcDate));
                            ExportNewContents(oldJson, newJson, utcDate);
                            File.Copy(existingJsonFile, newFilePath, true);
                            File.WriteAllText(existingJsonFile, latestJsonStr, new UTF8Encoding(false));
                            Utils.Log("All changes are saved. Current checking cycle ended.", "ApiStart2Module", ConsoleColor.Green);
                        }
                        else
                        {
                            Utils.Log("Nothing change. Current checking cycle ended.", "ApiStart2Module");
                        }
                    }
                    else
                    {
                        File.WriteAllText(existingJsonFile, latestJsonStr, new UTF8Encoding(false));
                    }
                }
                else
                {
                    Utils.Log("Error occurred on UpdateApiStart2Json. Current checking cycle ended.", "ApiStart2Module", ConsoleColor.DarkRed);
                }
            } catch (Exception ex) {
                Utils.Log(ex.Message, "ApiStart2Module", ConsoleColor.White, ConsoleColor.DarkRed);
            }
            m_forceUpdateToken = !(latestJsonStr != null && latestJsonStr.Length > 3);
            if (latestJsonStr == null)
            {
                latestJsonStr = "";
            }
            m_checkingTimer.Change((m_forceUpdateToken && !latestJsonStr.Equals("200")) ? 3000 : m_timeInterval, Timeout.Infinite);
            IsModuleRunning = false;
        }
Beispiel #2
0
        protected override void StartModuleCycle(object state)
        {
            IsModuleRunning = true;
            m_rndServerAddr = Consts.WorldServerAddr[m_random.Next(0, Consts.WorldServerAddr.Count)];
            Utils.Log("DownloadAndCheck Start. Server address: " + m_rndServerAddr, "SwfModule");
            var tt         = new TimeTracker("start");
            var taskResult = DownloadAllSwfsAsync(SwfPaths).Result;

            tt.AddLap("end");
            if (taskResult != null)
            {
                Utils.Log("All files downloaded. Time elapsed: " + tt.GetLapTimeDiff("start", "end", TimeTracker.TimeFormat.Seconds) + "s.", "SwfModule");
                var existingFiles = Directory.GetFiles(m_exportPath);
                if (existingFiles.Length < 1)
                {
                    foreach (var stream in taskResult)
                    {
                        File.WriteAllBytes(Path.Combine(m_exportPath, stream.Key), stream.Value.ToArray());
                    }
                }
                foreach (var fileWithPath in existingFiles)
                {
                    string filename = Path.GetFileName(fileWithPath);
                    if (!SwfFileNames.Contains(filename))
                    {
                        if (taskResult.ContainsKey(filename))
                        {
                            Utils.Log("File: " + filename + " is missing. Get a latest version.", "SwfModule");
                            File.WriteAllBytes(fileWithPath, taskResult[filename].ToArray());
                        }
                        else
                        {
                            Utils.Log("File: " + filename + " is missing from the source. Achieve the missing file.", "SwfModule");
                            var utcDate      = DateTime.UtcNow.ToString("yyyyMMddHH");
                            var achievedPath = Path.Combine(m_exportPath, utcDate);
                            Directory.CreateDirectory(achievedPath);
                            var newFilePath = Path.Combine(achievedPath, filename);
                            File.Move(fileWithPath, newFilePath);
                        }
                    }
                    else
                    {
                        var existingSwf = File.ReadAllBytes(fileWithPath);
                        if (!Utils.StreamAreEqualHash(existingSwf, taskResult[filename].ToArray()))
                        {
                            Utils.Log("File: " + filename + " is different from stored version, achieve and update to latest version.", "SwfModule", ConsoleColor.Red, ConsoleColor.Green);
                            var utcDate      = DateTime.UtcNow.ToString("yyyyMMddHH");
                            var achievedPath = Path.Combine(m_exportPath, utcDate);
                            Directory.CreateDirectory(achievedPath);
                            var newFilePath = Path.Combine(achievedPath, filename);
                            File.Move(fileWithPath, newFilePath);
                            File.WriteAllBytes(fileWithPath, taskResult[filename].ToArray());
                        }
                    }
                }
                Utils.Log("Current execution cycle is completed.", "SwfModule");
            }
            else
            {
                Utils.Log("Some of files are not properly downloaded, current execution cycle canceled.", "SwfModule");
            }
            m_checkingTimer.Change(m_timeInterval, Timeout.Infinite);
            IsModuleRunning = false;
        }