停止触发的 Web 作业的最佳方法是什么 [英] What is the best way to stop triggered web jobs

查看:22
本文介绍了停止触发的 Web 作业的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了团队的要求,例如,目前他们通过设置计划以特定时间间隔运行触发作业.他们设置了计划,因此根据计划时间,这些触发作业将运行,但在部署时我们必须确保触发的作业不应该运行,因为它不会接受部署 jar 到 azure.所以我想运行 webjobs 连续作业停止并且触发的作业不应该运行.它应该检查当时正在运行的触发作业的部署,如果它正在运行,即使您停止连续作业,我们也无法部署 jar.请帮助我.如何完成上述任务?我们是否需要添加任何脚本来检查触发的作业是否运行,如果它运行如何部署 jar.

和其他要求一样,目前我们正在传递像

这样的参数

-webjobs @(@{"name"="abc";"typeName"='continuous'},@{"name"="def";"ty‌ peName"='continuous'‌ }) -网站 kgh -rg ghi....

但是团队希望这些参数将在一个单独的文件中传递.这样当他们添加新的 Web 作业时,他们可以自己添加一个文件.如果我将参数脚本放在一个单独的文件中并传递给我,我该如何调用webjobs 脚本.

webjobs 的任何后期部署我都应该验证 webjobs 是否正确启动.所以这 3 个要求我需要在脚本中实现.请分享我完全满足上述要求的脚本.

解决方案

您可以通过 处理kudu API,然后检查是否有与WebJob(进程名)相关的进程,如果有,则杀死该进程.

例如:

param([字符串]$webJobName,[字符串]$用户名,[字符串]$密码,[字符串]$webAppName)$kuduApiAuthorisationToken="基本{0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $userName, $password)))$kuduApiUrl="https://$webAppName.scm.azurewebsites.net/api/processes/"$processes=Invoke-RestMethod -Uri $kuduApiUrl -Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} -Method GETforeach($p 在 $processes){if($p.name -eq $webJobName){$killAPI="https://$webAppName.scm.azurewebsites.net/api/processes/$($p.id)"Invoke-RestMethod -Uri $killAPI -Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} -Method DELETE}}

参数:-webJobName "XX" -userName "XX" -password "XX" -webAppName "XX"

注意:如果用户名中有$,如$test,可以指定用户名如-userName "`$test"

您可以手动或以编程方式从发布配置文件中获取用户名和密码:(在您的线程中回答:谁能帮助我如何通过 vsts 停止和启动 azure webjobs)

关于将参数放入文件:

参数.json

<预><代码>[{"文件路径": "data.csv","Cols": "Col1,Col2,Col3"},{"文件路径": "data2.csv","Cols": "Col1,Col6,Col7,Col8"}]

代码:

[object[]]$fileObj=Get-Content "parameter.json"|ConvertFrom-Jsonforeach($fileObj 中的 $fo){}

更新:

Parameter.json:

<代码>{"userName": "user1",密码":密码1","webAppName": "webapp1","resourceGroup": "resourceGroup1",网络工作":[{"name": "abc","typeName": "连续"},{"name": "def","typeName": "连续‌‌"}]}

脚本:

 [object]$paramObj=Get-Content "PowerShellModuleProject1parameter2.json"|ConvertFrom-Json$userName =$paramObj.userName$password =$paramObj.password$webAppName =$paramObj.webAppName$resourceGroup=$paramObj.resourceGroup[object[]]$webJobs=$paramObj.webJobsforeach($webjobs 中的 $wj){if($wj.typeName -eq "连续"){Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroup -ResourceType Microsoft.Web/sites/ContinuousWebJobs -ResourceName "$webAppName/$($wj.name)" -Action start -ApiVersion 2015-08-01 -Force写主机连续"写主机 $wj.name}别的{Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroup -ResourceType Microsoft.Web/sites/TriggeredWebJobs -ResourceName "$webAppName/$($wj.name)" -Action run -ApiVersion 2015-08-01 -force写主机触发"写主机 $wj.name}}

I got requirement from team like ,currently they are running the triggered jobs at particular intervals by setting up the schedule.They have setup schedule ,so as per the scheduled time those triggered jobs will run but at the time of deploy we must make sure the triggered jobs shouldn't run as it will not accept the deploy jar's into azure.So I want to run webjobs continuous jobs stop and as well as triggered jobs shouldn't run.It should check the triggered job running at the time of deploy if its running we can't deploy jar's even though if u stop continuous jobs.Please help me.How to accomplish the above task? whether we need to add any script to check triggered jobs running or not if it runs how to deploy jar's.

And other requirement is like,currently we are passing the parameters like

-webjobs @(@{"name"="abc";"typeName"='continuous'},@{"name"="def";"ty‌​peName"='continuous'‌​}) -website kgh -rg ghi....

But team want these parameters will be passed in a separate file.So that when they add new web job they can add in a file itself.how can I call if I put the parameter script in a separate file and pass to the webjobs script.

Any post deploy of webjobs I should validate whether the webjobs are started properly or not.So these 3 requirements I need to implement in the script.Kindly share me the script full filling the above requirement.

解决方案

You can get the processes of current web app through Process kudu API, then check whether there is the process related to the WebJob (process name), if so, kill that process.

For example:

param(
[string]$webJobName,
[string]$userName,
[string]$password,
[string]$webAppName
)
$kuduApiAuthorisationToken="Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $userName, $password)))
$kuduApiUrl="https://$webAppName.scm.azurewebsites.net/api/processes/"
$processes=Invoke-RestMethod -Uri $kuduApiUrl -Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} -Method GET
foreach($p in $processes){
    if($p.name -eq $webJobName){
    $killAPI="https://$webAppName.scm.azurewebsites.net/api/processes/$($p.id)"
    Invoke-RestMethod -Uri $killAPI -Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} -Method DELETE
    }
}

Parameter: -webJobName "XX" -userName "XX" -password "XX" -webAppName "XX"

Note: If there is $ in the username, such as $test, you can specify the username like -userName "`$test"

You can get the username and password from publish profile manually or programmatically: (Answered in your thread: Could any one help me how to stop and start azure webjobs through vsts)

Regarding put the parameter in a file:

parameter.json

[
  {
    "filepath": "data.csv",
    "Cols": "Col1,Col2,Col3"
  },
  {
    "filepath": "data2.csv",
    "Cols": "Col1,Col6,Col7,Col8"
  }
]

Code:

[object[]]$fileObj=Get-Content "parameter.json"|ConvertFrom-Json
foreach($fo in $fileObj){

}

Update:

Parameter.json:

{
    "userName": "user1",
    "password": "password1",
    "webAppName": "webapp1",
    "resourceGroup": "resourceGroup1",
    "webJobs": [   
      {
        "name": "abc",
        "typeName": "continuous"
      },
      {
        "name": "def",
        "typeName": "continuou‌"
      }
    ]
  }

Script:

 [object]$paramObj=Get-Content "PowerShellModuleProject1parameter2.json"|ConvertFrom-Json 
    $userName =$paramObj.userName
    $password =$paramObj.password
    $webAppName =$paramObj.webAppName
    $resourceGroup=$paramObj.resourceGroup
    [object[]]$webJobs=$paramObj.webJobs
    foreach($wj in $webjobs){
     if($wj.typeName -eq "continuous")
     {
Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroup -ResourceType Microsoft.Web/sites/ContinuousWebJobs -ResourceName "$webAppName/$($wj.name)" -Action start -ApiVersion 2015-08-01 -Force
      Write-Host "continuous"
     Write-Host $wj.name
     }
     else{
     Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroup -ResourceType Microsoft.Web/sites/TriggeredWebJobs -ResourceName "$webAppName/$($wj.name)" -Action run -ApiVersion 2015-08-01 -force
     Write-Host "triggered"
     Write-Host $wj.name
     }
     }

这篇关于停止触发的 Web 作业的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆