2017-08-16 45 views

回答

1

一種方法是將消息發送到服務總線隊列ScheduledEnqueueTimeUtc財產從現在設置爲10分鐘。 (docs

然後有一個由該服務總線隊列觸發的Azure功能。 (docs

1

因爲我需要等待10分鐘以下DB部署的原因:Not able to open Azure SQL Server DB immidiately after the creation

隨着全球配置選項host.json州約functionTimeout如下:

functionTimeout指示所有函數的超時持續時間的值。

  • 在動態SKU中,有效範圍爲1秒到10分鐘,默認值爲5分鐘。
  • 付費SKU中沒有限制,默認值爲空(表示沒有超時)。

按我的理解,如果您的註冊過程中需要CreateTenant下做的,我認爲你可以第二步後,檢查數據庫的製作狀態,那麼當數據庫聯機,你可以做第三步。我寫了一個樣品這種情況下,你可以參考一下吧:

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, ""); 
} 

結果:

enter image description here

而且,米哈伊爾·建議,可以在執行數據庫創建之後發送隊列消息,然後使用QueueTrigger接收郵件並檢查數據庫的狀態,並在數據庫聯機後插入條目以解除註冊過程。

下面是一些有用的教程,你可以參考他們:

Use Azure Functions to connect to an Azure SQL Database

sys.dm_database_copies (Azure SQL Database)