私は、次のDBの展開の理由で10分間待機する必要があるため、次のようにグローバルコンフィギュレーションオプションhost.jsonとしてNot able to open Azure SQL Server DB immidiately after the creation
約functionTimeout
を述べている:
functionTimeoutすべての機能のタイムアウト時間を示す値。
- ダイナミックSKUでは、有効な範囲は1秒から10分で、デフォルト値は5分です。
- 有償SKUには制限がなく、デフォルト値はnull(タイムアウトがないことを示す)です。私の理解パー
、あなたの登録プロセスはCreateTenant
の下で行われる必要がある場合、私はあなたが、データベースがオンラインになっているときに、あなたがステップ3を行うことができ、ステップ2の後に、データベースの作成状況を確認することができると仮定しました。私はこのシナリオのためにサンプルを書いて、あなたはそれを参照することができます:
run.csx
#r "System.Configuration"
#r "System.Data"
using System.Net;
using System.Configuration;
using System.Data.SqlClient;
using System.Threading.Tasks;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
// Get request body
dynamic data = await req.Content.ReadAsAsync<object>();
string dataBase_Name = data?.dataBase_Name;
string source_dataBase_Name = data?.source_dataBase_Name;
log.Info("Begin create the database for the tenant...");
//Create the database
var str = ConfigurationManager.ConnectionStrings["sqldb_connection"].ConnectionString;
using (SqlConnection conn = new SqlConnection(str))
{
conn.Open();
var copyDbSql = $"CREATE DATABASE [{dataBase_Name}] as COPY OF [{source_dataBase_Name}] (SERVICE_OBJECTIVE='S0')";
try
{
using (SqlCommand cmd = new SqlCommand(copyDbSql, conn))
{
//30s by default, https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtimeout.aspx
//cmd.CommandTimeout=0;
cmd.ExecuteNonQuery(); //30s timeout if the server is not responding, you could change it, but I would leave it for default.
log.Info("create database statement executed...");
}
}
catch (Exception e)
{
//exception for timeout and so on
//you need to make sure the database creation has been accepted, you could execute the following sql statement:
//SELECT * FROM sys.dm_database_copies where partner_database=N'{your-database-name}'
log.Info(e.Message);
}
}
log.Info("check the creation processing...");
//If the database creation is accepted, then check the creation status of your database
bool status = false;
using (SqlConnection conn = new SqlConnection(str))
{
conn.Open();
var text = "Select count(*) from master.sys.databases where name=N'" + dataBase_Name + "' and state_desc='ONLINE'";
using (SqlCommand cmd = new SqlCommand(text, conn))
{
do
{
var count = await cmd.ExecuteScalarAsync();
if (count != null && Convert.ToInt32(count) > 0)
status = true;
if (status)
log.Info($"Database [{dataBase_Name}] is online!!!");
else
{
log.Info($"Database [{dataBase_Name}] is creating...");
Task.Delay(TimeSpan.FromSeconds(30)).Wait(); //sleep for 30s
}
} while (!status);
}
}
if (status)
{
//Database is online, do other operations
}
return req.CreateResponse(HttpStatusCode.OK, "");
}
結果:
また
、ミハイルが示唆したように、データベース作成を実行した後でキューメッセージを送信してから、QueueTriggerメッセージを受け取ってデータベースの状態を確認し、データベースがオンラインになった後にエントリを挿入して登録プロセスを切り離すことができます。ここで
あなたがそれらを参照することができ、いくつかの有用なチュートリアルです:
Use Azure Functions to connect to an Azure SQL Database
sys.dm_database_copies (Azure SQL Database)