From 4c5653fc44e5f798b5aac6e3389ddcccc67f11bc Mon Sep 17 00:00:00 2001 From: Atralupus Date: Mon, 17 Jul 2023 14:44:41 +0900 Subject: [PATCH] Setup hangfire base --- .../NineChronicles.DataProvider.Batch.csproj | 9 +-- NineChronicles.DataProvider.Batch/Program.cs | 65 ++++++++----------- .../appsettings.json | 2 +- NineChronicles.DataProvider.sln | 14 ++++ docker-compose.yml | 10 ++- init-db/01.sql | 5 ++ 6 files changed, 55 insertions(+), 50 deletions(-) create mode 100644 init-db/01.sql diff --git a/NineChronicles.DataProvider.Batch/NineChronicles.DataProvider.Batch.csproj b/NineChronicles.DataProvider.Batch/NineChronicles.DataProvider.Batch.csproj index 775366b8..22b17709 100644 --- a/NineChronicles.DataProvider.Batch/NineChronicles.DataProvider.Batch.csproj +++ b/NineChronicles.DataProvider.Batch/NineChronicles.DataProvider.Batch.csproj @@ -1,18 +1,19 @@ - net6 + Exe + net6.0 8.0 true enable - true - $(NoWarn),1573,1591,1712,SA1009,SA1101,SA1111,SA1309,SA1600,SA1633,S1134 + true + $(NoWarn),1573,1591,1712,SA1009,SA1101,SA1111,SA1309,SA1600,SA1633,S1134 - + all diff --git a/NineChronicles.DataProvider.Batch/Program.cs b/NineChronicles.DataProvider.Batch/Program.cs index 1a7b3a68..f622d916 100644 --- a/NineChronicles.DataProvider.Batch/Program.cs +++ b/NineChronicles.DataProvider.Batch/Program.cs @@ -1,28 +1,39 @@ -using Hangfire; -using Hangfire.MySql; -using Microsoft.Extensions.Configuration; -using MySql.Data.MySqlClient; -using System; -using System.IO; - -namespace NineChronicles.DataProvider.Batch +namespace NineChronicles.DataProvider.Batch { - class Program + using System; + using System.IO; + using System.Transactions; + using Hangfire; + using Hangfire.MySql; + using Microsoft.Extensions.Configuration; + + public static class Program { - static void Main(string[] args) + public static void Main(string[] args) { IConfiguration configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .Build(); - string connectionString = configuration.GetConnectionString("MysqlConnectionString"); - - GlobalConfiguration.Configuration.UseStorage(new MySqlStorage(connectionString)); + string connectionString = configuration.GetConnectionString("HangfireMysqlConnectionString"); + var options = new MySqlStorageOptions + { + TransactionIsolationLevel = IsolationLevel.ReadCommitted, + QueuePollInterval = TimeSpan.FromSeconds(15), + JobExpirationCheckInterval = TimeSpan.FromHours(1), + CountersAggregateInterval = TimeSpan.FromMinutes(5), + PrepareSchemaIfNecessary = true, + DashboardJobListLimit = 50000, + TransactionTimeout = TimeSpan.FromMinutes(1), + TablesPrefix = "Hangfire", + }; + + GlobalConfiguration.Configuration.UseStorage(new MySqlStorage(connectionString, options)); var server = new BackgroundJobServer(); - RecurringJob.AddOrUpdate(() => UpdateDatabaseTables(connectionString), Cron.Minutely); // Or set your own schedule + RecurringJob.AddOrUpdate("TempID", () => UpdateDatabaseTables(connectionString), Cron.Minutely); // Or set your own schedule Console.WriteLine("Hangfire Server started. Press any key to exit..."); Console.ReadKey(); @@ -32,31 +43,7 @@ static void Main(string[] args) public static void UpdateDatabaseTables(string connectionString) { - string selectQuery = "SELECT * FROM temp_table"; - - string updateQuery1 = "UPDATE table1 SET ..."; - string updateQuery2 = "UPDATE table2 SET ..."; - - using (var connection = new MySqlConnection(connectionString)) - { - connection.Open(); - - MySqlCommand selectCommand = new MySqlCommand(selectQuery, connection); - var reader = selectCommand.ExecuteReader(); - - while (reader.Read()) - { - // Logic based on selectQuery's result - - reader.Close(); - - MySqlCommand updateCommand1 = new MySqlCommand(updateQuery1, connection); - MySqlCommand updateCommand2 = new MySqlCommand(updateQuery2, connection); - - updateCommand1.ExecuteNonQuery(); - updateCommand2.ExecuteNonQuery(); - } - } + Console.WriteLine("Job"); } } } diff --git a/NineChronicles.DataProvider.Batch/appsettings.json b/NineChronicles.DataProvider.Batch/appsettings.json index d7416f98..17a9d5e5 100644 --- a/NineChronicles.DataProvider.Batch/appsettings.json +++ b/NineChronicles.DataProvider.Batch/appsettings.json @@ -1,6 +1,6 @@ { "ConnectionStrings": { - "MysqlConnectionString": "Server=db;Database=data_provider;User=user;Password=user-pw;Port=3306" + "HangfireMysqlConnectionString": "Server=127.0.0.1;Database=hangfire;User=root;Password=local;Port=3306" } } \ No newline at end of file diff --git a/NineChronicles.DataProvider.sln b/NineChronicles.DataProvider.sln index b6a01696..0bb2f8d9 100644 --- a/NineChronicles.DataProvider.sln +++ b/NineChronicles.DataProvider.sln @@ -18,6 +18,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NineChronicles.DataProvider EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NineChronicles.DataProvider.Batch", "NineChronicles.DataProvider.Batch\NineChronicles.DataProvider.Batch.csproj", "{F9EA10C0-4CF6-4E77-BB83-3B667409531D}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NineChronicles.DataProvider.HangfireDashboard", "NineChronicles.DataProvider.HangfireDashboard\NineChronicles.DataProvider.HangfireDashboard.csproj", "{34CEA9C1-62D2-4963-B3AB-DE76EA146E20}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -88,6 +90,18 @@ Global {F9EA10C0-4CF6-4E77-BB83-3B667409531D}.Release|x64.Build.0 = Release|Any CPU {F9EA10C0-4CF6-4E77-BB83-3B667409531D}.Release|x86.ActiveCfg = Release|Any CPU {F9EA10C0-4CF6-4E77-BB83-3B667409531D}.Release|x86.Build.0 = Release|Any CPU + {34CEA9C1-62D2-4963-B3AB-DE76EA146E20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {34CEA9C1-62D2-4963-B3AB-DE76EA146E20}.Debug|Any CPU.Build.0 = Debug|Any CPU + {34CEA9C1-62D2-4963-B3AB-DE76EA146E20}.Debug|x64.ActiveCfg = Debug|Any CPU + {34CEA9C1-62D2-4963-B3AB-DE76EA146E20}.Debug|x64.Build.0 = Debug|Any CPU + {34CEA9C1-62D2-4963-B3AB-DE76EA146E20}.Debug|x86.ActiveCfg = Debug|Any CPU + {34CEA9C1-62D2-4963-B3AB-DE76EA146E20}.Debug|x86.Build.0 = Debug|Any CPU + {34CEA9C1-62D2-4963-B3AB-DE76EA146E20}.Release|Any CPU.ActiveCfg = Release|Any CPU + {34CEA9C1-62D2-4963-B3AB-DE76EA146E20}.Release|Any CPU.Build.0 = Release|Any CPU + {34CEA9C1-62D2-4963-B3AB-DE76EA146E20}.Release|x64.ActiveCfg = Release|Any CPU + {34CEA9C1-62D2-4963-B3AB-DE76EA146E20}.Release|x64.Build.0 = Release|Any CPU + {34CEA9C1-62D2-4963-B3AB-DE76EA146E20}.Release|x86.ActiveCfg = Release|Any CPU + {34CEA9C1-62D2-4963-B3AB-DE76EA146E20}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/docker-compose.yml b/docker-compose.yml index 4d00b1ba..bc799118 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,14 +2,12 @@ version: '3.8' services: db: image: mysql:8.0.30 - volumes: - - db_data:/var/lib/mysql restart: always environment: - MYSQL_ROOT_PASSWORD: root-pw - MYSQL_DATABASE: data_provider - MYSQL_USER: user - MYSQL_PASSWORD: user-pw + MYSQL_ROOT_PASSWORD: local + volumes: + - ./init-db:/docker-entrypoint-initdb.d + - db_data:/var/lib/mysql ports: - 3306:3306 networks: diff --git a/init-db/01.sql b/init-db/01.sql new file mode 100644 index 00000000..12750a2b --- /dev/null +++ b/init-db/01.sql @@ -0,0 +1,5 @@ +CREATE DATABASE IF NOT EXISTS `data_provider`; +CREATE DATABASE IF NOT EXISTS `hangfire`; + +CREATE USER 'root'@'localhost' IDENTIFIED BY 'local'; +GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';