Skip to content

Commit

Permalink
Merge pull request #427 from Atralupus/feat/base
Browse files Browse the repository at this point in the history
Setup hangfire base
  • Loading branch information
Atralupus authored Jul 17, 2023
2 parents e1a119c + 4c5653f commit e6ba2f0
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6</TargetFramework>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn),1573,1591,1712,SA1009,SA1101,SA1111,SA1309,SA1600,SA1633,S1134</NoWarn>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn),1573,1591,1712,SA1009,SA1101,SA1111,SA1309,SA1600,SA1633,S1134</NoWarn>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\NineChronicles.DataProvider\NineChronicles.DataProvider.csproj" />
<PackageReference Include="Hangfire.Core" Version="1.8.3" />
<PackageReference Include="Hangfire.MySql" Version="0.0.7" />
<PackageReference Include="Hangfire.MySqlStorage" Version="2.0.3" />
<PackageReference Include="MySql.Data" Version="8.0.33" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.18.0.27296">
<PrivateAssets>all</PrivateAssets>
Expand Down
65 changes: 26 additions & 39 deletions NineChronicles.DataProvider.Batch/Program.cs
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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");
}
}
}
2 changes: 1 addition & 1 deletion NineChronicles.DataProvider.Batch/appsettings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}

14 changes: 14 additions & 0 deletions NineChronicles.DataProvider.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions init-db/01.sql
Original file line number Diff line number Diff line change
@@ -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'@'%';

0 comments on commit e6ba2f0

Please sign in to comment.