具有Http触发功能的连续Azure WebJob

编程入门 行业动态 更新时间:2024-10-25 02:23:06
本文介绍了具有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):

<yourapp>.azurewebsites/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 {user}:{password} @ {sitename} .scm.azurewebsites/api/continuouswebjobs/{job name}/start

POST {user}:{password}@{sitename}.scm.azurewebsites/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

注意:如何创建Azure函数,请参考文档.

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.根据队列消息信息触发特定功能. 我们可以创建使用QueueTrigger的WebJob

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.

更多推荐

具有Http触发功能的连续Azure WebJob

本文发布于:2023-11-28 07:50:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641543.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:功能   Http   WebJob   Azure

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!