具有Http触发功能的连续Azure WebJob
我目前有一个天蓝色的webjob,它每天执行从一个数据库到另一个数据库的同步,但是还想增加手动触发同步的功能.我已经在webjob项目中设置了以下功能:
I currently have an azure webjob that performs a daily sync from one database to another, but would like to add the ability to manually trigger the sync as well. I have set up the functions as follows in the webjob project:
public static void SyncData([TimerTrigger("0 0 5 * * *", RunOnStartup = false)] TimerInfo timerInfo) { }
[NoAutomaticTrigger]
public static Task SyncAll(TraceWriter log){ }
[NoAutomaticTrigger]
public static Task SyncBranches(TraceWriter log){ }
[NoAutomaticTrigger]
public static Task SyncCustomers(TraceWriter log){ }
[NoAutomaticTrigger]
public static Task SyncInventory(TraceWriter log){ }
我可以在webjob下的Kudu仪表板中看到这些功能,但是不确定如何使用MS文档中列出的http请求触发该功能(
I can see the functions in the Kudu dashboard under the webjob, but am not sure how I can trigger the functions with an http request as listed on the MS documentation (here):
http://<yourapp>.azurewebsites.net/api/<funcname>
当我向该端点发出请求时,我会收到404响应-要通过http请求手动触发这些功能,该怎么办?
When I make a request to that endpoint I get a 404 response - what do I need to do in order to trigger those functions manually via http request?
谢谢, 瑞安
我们可以使用Kudu WebJob API 即可启动或停止连续作业
We can use the Kudu WebJob API to start or stop a continuous job
POST https://{user}:{password} @ {sitename} .scm.azurewebsites.net/api/continuouswebjobs/{job name}/start
POST https://{user}:{password}@{sitename}.scm.azurewebsites.net/api/continuouswebjobs/{job name}/start
我们可以从Azure WebApp的publishSetting文件中获取用户和密码信息.而且我们可以从门户网站下载文件
We can get user and password info from the Azure WebApp publishSetting file. And We can download the file from the portal
使用提琴手测试Rest API
Test the Rest API using the fiddler
Note: How to create Azure function please refer to the document.
更新:
有什么办法可以在该连续作业中触发特定功能?
is there a way I can trigger a specific function in that continuous job?
根据我的经验,我找不到直接在连续作业中触发特定功能的方法或Rest API.
Based on my experience, I can't find a way or Rest API to trigger a sepcific function in continuous job directly.
我的解决方法是我们可以使用Webjob QueueTrigger.根据队列消息信息触发特定功能. 我们可以创建
My work around is that we can use Webjob QueueTrigger. According the queue message info to trigger the sepcific function. We can create a WebJob with QueueTrigger
以下是我根据您的代码执行的详细步骤.
The following is my detail steps according to your code.
1.为触发器创建一个Azure存储队列
1.Create an Azure storage queue for trigger
2.创建一个Webjob项目并添加以下代码
2.Create a Webjob project and add the following code
public static void SyncData([QueueTrigger("backup")] string logMessage, TextWriter logger)
{
Console.WriteLine($"Start time:{DateTime.Now}");
switch (logMessage.ToLower())
{
case "syncall":
SyncAll(logger);
break;
case "syncbranches":
SyncBranches(logger);
break;
case "synccustomers":
SyncCustomers(logger);
break;
case "syncinventory":
SyncInventory(logger);
break;
default:
Console.WriteLine("Default case");
break;
}
Console.Write($"Endtime:{DateTime.Now}");
}
[NoAutomaticTrigger]
public static Task SyncAll(TextWriter log)
{
Console.WriteLine("SyncAll :"+DateTime.Now);
return null;
//await Task.Delay(10);
}
[NoAutomaticTrigger]
public static Task SyncBranches(TextWriter log)
{
Console.WriteLine("SyncBranches :" + DateTime.Now);
return null;
}
[NoAutomaticTrigger]
public static Task SyncCustomers(TextWriter log)
{
Console.WriteLine("SyncCustomers :" + DateTime.Now);
return null;
}
[NoAutomaticTrigger]
public static Task SyncInventory(TextWriter log)
{
Console.WriteLine("SyncInventory :" + DateTime.Now);
return null;
}
3.使用Azure 存储队列REST API 创建队列消息.
3.Use Azure Storage Queue REST API to create a queue message.
4.从控制台检查结果.
4.Check result the from the console.