从Javascript代码启动Azure WebJobs

时间:2020-09-25 13:40:28

标签: javascript azure automation node-modules azure-webjobs

是否有任何节点程序包或其他方法可以从JS启动(运行)Azure Webjob?我是一名自动化人员,我想启动一个Azure Webjob来更改应用程序中的某些状态。手动地,我只是按“运行”,但是我想使用代码来开始工作。一种选择是登录UI并使用自动化功能按“运行”按钮,但是从UI上看对我来说并不专业。

我在那里看到了一些信息,例如Webhook,用户,密码等Webjob属性。也许我可以用某种方式使用代码触发这些事情。

谢谢!

1 个答案:

答案 0 :(得分:1)

如果要通过节点管理Azure WebJob,我们可以使用软件包azure-arm-website。该软件包提供了用于运行触发器WebJob的方法runTriggeredWebJobWithHttpOperationResponse和用于连续WebJob的方法startContinuousWebJobWithHttpOperationResponse

例如

  1. 创建服务主体
az login
az account set --subscription "SUBSCRIPTION_ID"
az ad sp create-for-rbac --role "Contributor" --scopes "/subscriptions/<subscription_id>"
  1. 代码
const {
  ApplicationTokenCredentials,
  AzureEnvironment,
} = require("ms-rest-azure");
const { WebSiteManagementClient } = require("azure-arm-website");

const clientId = "<the sp appId>";
const tenant = "<you AD tenant domain>";
const clientSecret = "<the sp password>";
const subscriptionId = "";
const creds = new ApplicationTokenCredentials(clientId, tenant, clientSecret, {
  environment: AzureEnvironment.Azure,
});

const client = new WebSiteManagementClient(creds, subscriptionId);
const rgName = "rg-webapp-demo";
const webName = "demo-200925164220";

const jobName = "test";
client.webApps
  .runTriggeredWebJobWithHttpOperationResponse(rgName, webName, jobName)
  .then((res) => {
    console.log(res.response.statusMessage);
    console.log(res.response.statusCode);
  })
  .catch((err) => {
    throw err;
  });

  client.webApps.startContinuousWebJob

enter image description here enter image description here