我目前有一个天蓝色的网络作业,每天执行从一个数据库到另一个数据库的同步,但也想添加手动触发同步的功能。我在webjob项目中设置的功能如下:
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 请求触发这些功能(here https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook):
http://<yourapp>.azurewebsites.net/api/<funcname>
当我向该端点发出请求时,我收到 404 响应 - 我需要做什么才能通过 http 请求手动触发这些功能?
谢谢,
瑞安
我们可以使用 Kudu网络作业API https://github.com/projectkudu/kudu/wiki/WebJobs-API开始或停止连续工作
POST https://{用户}:{密码}@{站点名称}.scm.azurewebsites.net/api/continuouswebjobs/{作业名称}/start
我们可以从Azure Web App发布设置文件中获取用户和密码信息。我们可以从门户下载该文件
使用 fiddler 测试 Rest API
Note:如何创建Azure函数请参考document https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-first-azure-function.
Update:
有没有办法可以在连续工作中触发特定功能?
根据我的经验,我找不到一种方法或 Rest API 来直接触发连续工作中的特定功能。
我的解决方法是我们可以使用 Webjob QueueTrigger。根据队列消息信息触发特定功能。
我们可以创建一个带有 QueueTrigger 的 WebJob https://learn.microsoft.com/en-us/azure/app-service-web/websites-dotnet-webjobs-sdk-storage-queues-how-to
以下是我根据您的代码的详细步骤。
1.为触发器创建Azure存储队列
2.创建Webjob项目并添加以下代码
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 https://learn.microsoft.com/en-us/rest/api/storageservices/fileservices/put-message创建队列消息。
4.从控制台检查结果。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)