Exemplo n.º 1
0
        static int Main(string[] args)
        {
            CommandLineArgs commandLine;

            try
            {
                commandLine = new CommandLineArgs(args);
                if (commandLine.ShowHelp)
                {
                    commandLine.DisplayHelp(Console.Out);
                    return((int)ExitCode.Success);
                }

                IConfigurationBuilder configBuilder = new ConfigurationBuilder()
                                                      .AddXmlFile(commandLine.ConfigFile);

                IConfigurationRoot rawConfig = configBuilder.Build();
                config = rawConfig.Get <Config>();

                if (config.LoggingConfigPath != null)
                {
                    Logging.SetUpLogging(config.LoggingConfigPath);
                }
                else
                {
                    Console.Error.WriteLine("No logging configuration file set. Logging to console.");
                    Logging.SetUpConsoleLogging();
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("Fatal error: {0}", ex, ex.Message);
                return((int)ExitCode.Failure);
            }

            try
            {
                Logging.Log.Debug($"Command line args parsed. ConfigFile={commandLine.ConfigFile}");

                using (IMyAnimeListApi basicApi = new MyAnimeListApi()
                {
                    TimeoutInMs = config.MalTimeoutInMs, UserAgent = config.MalApiUserAgentString
                })
                    using (IMyAnimeListApi rateLimitingApi = new RateLimitingMyAnimeListApi(basicApi, TimeSpan.FromMilliseconds(config.DelayBetweenRequestsInMs)))
                        using (IMyAnimeListApi malApi = new RetryOnFailureMyAnimeListApi(rateLimitingApi, config.NumMalRequestFailuresBeforeGivingUp, config.DelayAfterMalRequestFailureInMs))
                            using (NpgsqlConnection conn = new NpgsqlConnection(config.ConnectionStrings.AnimeRecs))
                            {
                                conn.Open();
                                int usersAddedSoFar = 0;
                                while (usersAddedSoFar < config.UsersPerRun)
                                {
                                    RecentUsersResults recentMalUsers = malApi.GetRecentOnlineUsers();

                                    foreach (string user in recentMalUsers.RecentUsers)
                                    {
                                        using (var transaction = conn.BeginTransaction(System.Data.IsolationLevel.RepeatableRead))
                                        {
                                            if (!UserIsInDatabase(user, conn, transaction))
                                            {
                                                MalUserLookupResults userLookup = malApi.GetAnimeListForUser(user);
                                                if (UserMeetsCriteria(userLookup, conn, transaction))
                                                {
                                                    InsertUserAndRatingsInDatabase(userLookup, conn, transaction);
                                                    usersAddedSoFar++;
                                                    Logging.Log.Debug("Committing transaction.");
                                                    transaction.Commit();
                                                    Logging.Log.Debug("Transaction committed.");

                                                    if (usersAddedSoFar == config.UsersPerRun)
                                                    {
                                                        break;
                                                    }
                                                }
                                                else
                                                {
                                                    Logging.Log.InfoFormat("{0} does not meet criteria for inclusion, skipping", user);
                                                }
                                            }
                                            else
                                            {
                                                Logging.Log.InfoFormat("{0} is already in the database, skipping.", user);
                                            }
                                        }
                                    }
                                }

                                using (var transaction = conn.BeginTransaction(System.Data.IsolationLevel.RepeatableRead))
                                {
                                    TrimDatabaseToMaxUsers(config.MaxUsersInDatabase, conn, transaction);
                                    transaction.Commit();
                                }
                            }
            }
            catch (Exception ex)
            {
                Logging.Log.FatalFormat("Fatal error: {0}", ex, ex.Message);
                return((int)ExitCode.Failure);
            }

            return((int)ExitCode.Success);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Logging.SetUpLogging();

            try
            {
                config = new Config();

                using (IMyAnimeListApi basicApi = new MyAnimeListApi() { TimeoutInMs = config.MalTimeoutInMs, UserAgent = config.MalApiUserAgentString })
                using (IMyAnimeListApi rateLimitingApi = new RateLimitingMyAnimeListApi(basicApi, TimeSpan.FromMilliseconds(config.DelayBetweenRequestsInMs)))
                using (IMyAnimeListApi malApi = new RetryOnFailureMyAnimeListApi(rateLimitingApi, config.NumMalRequestFailuresBeforeGivingUp, config.DelayAfterMalRequestFailureInMs))
                using (NpgsqlConnection conn = new NpgsqlConnection(config.PostgresConnectionString))
                {
                    conn.Open();
                    int usersAddedSoFar = 0;
                    while (usersAddedSoFar < config.UsersPerRun)
                    {
                        RecentUsersResults recentMalUsers = malApi.GetRecentOnlineUsers();

                        foreach (string user in recentMalUsers.RecentUsers)
                        {
                            using (var transaction = conn.BeginTransaction(System.Data.IsolationLevel.RepeatableRead))
                            {
                                if (!UserIsInDatabase(user, conn, transaction))
                                {
                                    MalUserLookupResults userLookup = malApi.GetAnimeListForUser(user);
                                    if (UserMeetsCriteria(userLookup, conn, transaction))
                                    {
                                        InsertUserAndRatingsInDatabase(userLookup, conn, transaction);
                                        usersAddedSoFar++;
                                        Logging.Log.Debug("Committing transaction.");
                                        transaction.Commit();
                                        Logging.Log.Debug("Transaction committed.");

                                        if (usersAddedSoFar == config.UsersPerRun)
                                        {
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        Logging.Log.InfoFormat("{0} does not meet criteria for inclusion, skipping", user);
                                    }
                                }
                                else
                                {
                                    Logging.Log.InfoFormat("{0} is already in the database, skipping.", user);
                                }
                            }
                        }
                    }

                    using (var transaction = conn.BeginTransaction(System.Data.IsolationLevel.RepeatableRead))
                    {
                        TrimDatabaseToMaxUsers(config.MaxUsersInDatabase, conn, transaction);
                        transaction.Commit();
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.Log.FatalFormat("Fatal error: {0}", ex, ex.Message);
                Environment.ExitCode = 1;
            }
        }