1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-04 14:13:28 +01:00
bitwarden-server/util/Function/DatabaseMaintenance.cs

42 lines
1.5 KiB
C#
Raw Normal View History

2017-11-15 13:59:03 +01:00
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
namespace Bit.Function
{
public static class DatabaseMaintenance
{
[FunctionName("DatabaseMaintenance")]
2017-11-15 14:10:23 +01:00
public static void Run([TimerTrigger("0 0 4 * * *")]TimerInfo myTimer, TraceWriter log)
2017-11-15 13:59:03 +01:00
{
2017-11-15 14:10:23 +01:00
var connectionString = ConfigurationManager.ConnectionStrings["Vault"].ConnectionString;
2017-11-15 13:59:03 +01:00
using(var connection = new SqlConnection(connectionString))
{
connection.Open();
// ref: http://bit.ly/2zFNcZo
var cmd = new SqlCommand("[dbo].[AzureSQLMaintenance]", connection)
{
CommandType = CommandType.StoredProcedure
};
// Options: "all", "index", "statistics"
cmd.Parameters.Add("@operation", SqlDbType.NVarChar).Value = "all";
// Options: "smart", "dummy"
cmd.Parameters.Add("@mode", SqlDbType.NVarChar).Value = "smart";
// Options: 0, 1
cmd.Parameters.Add("@LogToTable", SqlDbType.Bit).Value = 1;
// Asynchronous BeginExecuteNonQuery for this long running sproc to avoid timeouts
var result = cmd.BeginExecuteNonQuery();
cmd.EndExecuteNonQuery(result);
2017-11-15 14:10:23 +01:00
log.Info($"Started [dbo].[AzureSQLMaintenance] at {DateTime.UtcNow}.");
2017-11-15 13:59:03 +01:00
}
}
}
}