Skip to content

Commit

Permalink
Merge pull request #28 from peppy/static-db-helper
Browse files Browse the repository at this point in the history
Move database connection method to static helper class
  • Loading branch information
smoogipoo authored Nov 14, 2023
2 parents 8d19ac5 + e1521dd commit 848eaa8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 28 deletions.
47 changes: 47 additions & 0 deletions osu.Server.QueueProcessor/DatabaseAccess.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using MySqlConnector;

namespace osu.Server.QueueProcessor
{
/// <summary>
/// Provides access to a MySQL database.
/// </summary>
public static class DatabaseAccess
{
/// <summary>
/// Retrieve a fresh MySQL connection. Should be disposed after use.
/// </summary>
public static MySqlConnection GetConnection()
{
string connectionString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING") ?? String.Empty;

if (string.IsNullOrEmpty(connectionString))
{
string host = (Environment.GetEnvironmentVariable("DB_HOST") ?? "localhost");
string port = (Environment.GetEnvironmentVariable("DB_PORT") ?? "3306");
string user = (Environment.GetEnvironmentVariable("DB_USER") ?? "root");
string password = (Environment.GetEnvironmentVariable("DB_PASS") ?? string.Empty);
string name = (Environment.GetEnvironmentVariable("DB_NAME") ?? "osu");

string passwordString = string.IsNullOrEmpty(password) ? string.Empty : $"Password={password};";

connectionString = $"Server={host};Port={port};Database={name};User ID={user};{passwordString}ConnectionTimeout=5;ConnectionReset=false;Pooling=true;";
}

var connection = new MySqlConnection(connectionString);
connection.Open();

// TODO: remove this when we have set a saner time zone server-side.
using (var cmd = connection.CreateCommand())
{
cmd.CommandText = "SET time_zone = '+00:00';";
cmd.ExecuteNonQuery();
}

return connection;
}
}
}
29 changes: 1 addition & 28 deletions osu.Server.QueueProcessor/QueueProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,34 +261,7 @@ public void PublishMessage<TMessage>(string channelName, TMessage message)
/// <summary>
/// Retrieve a database connection.
/// </summary>
public virtual MySqlConnection GetDatabaseConnection()
{
string connectionString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING") ?? String.Empty;

if (string.IsNullOrEmpty(connectionString))
{
string host = (Environment.GetEnvironmentVariable("DB_HOST") ?? "localhost");
string user = (Environment.GetEnvironmentVariable("DB_USER") ?? "root");
string password = (Environment.GetEnvironmentVariable("DB_PASS") ?? string.Empty);
string name = (Environment.GetEnvironmentVariable("DB_NAME") ?? "osu");

string passwordString = string.IsNullOrEmpty(password) ? string.Empty : $"Password={password};";

connectionString = $"Server={host};Database={name};User ID={user};{passwordString}ConnectionTimeout=5;ConnectionReset=false;Pooling=true;";
}

var connection = new MySqlConnection(connectionString);
connection.Open();

// TODO: remove this when we have set a saner time zone server-side.
using (var cmd = connection.CreateCommand())
{
cmd.CommandText = "SET time_zone = '+00:00';";
cmd.ExecuteNonQuery();
}

return connection;
}
public MySqlConnection GetDatabaseConnection() => DatabaseAccess.GetConnection();

/// <summary>
/// Implement to process a single item from the queue. Will only be invoked if <see cref="ProcessResults"/> is not implemented.
Expand Down

0 comments on commit 848eaa8

Please sign in to comment.