因为我需要等待 10 分钟,因为以下数据库部署原因:Not able to open Azure SQL Server DB immidiately after the creation
由于全局配置选项host.json 对functionTimeout 的陈述如下:
functionTimeout值表示所有函数的超时时间。
- 在动态 SKU 中,有效范围为 1 秒到 10 分钟,默认值为 5 分钟。
- 在付费 SKU 中没有限制,默认值为 null(表示没有超时)。
据我了解,如果您的注册过程需要在CreateTenant下完成,我假设您可以在Step2之后检查您的数据库的创建状态,然后当数据库在线时,您可以进行Step3。我为这个场景写了一个示例,你可以参考它:
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, "");
}
结果:
另外,正如 Mikhail 所建议的,您可以在执行数据库创建后发送队列消息,然后使用 QueueTrigger 获取消息并检查数据库的状态并在数据库联机后插入条目以解耦您的注册过程。
这里有一些有用的教程,你可以参考一下:
Use Azure Functions to connect to an Azure SQL Database
sys.dm_database_copies (Azure SQL Database)