0

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.

PDBR PRAVEEN
  • 93
  • 1
  • 10

1 Answers1

1

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 "PowerShellModuleProject1\parameter2.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
     }
     }
starian chen-MSFT
  • 33,174
  • 2
  • 29
  • 53
  • Hi MSFT,Thanks for the reply. I asked you the parameters calling in a separate file for webjobs not for table storage.Can you please comment on that. Suppose : Parameter: -webJobName "XX" -userName "XX" -password "XX" -webAppName "XX" and -webjobs @(@{"name"="abc";"typeName"='continuous'},@{"name"="def";"ty‌​peName"='continuous'‌​}) -website kgh -rg ghi.... these parameters we are passing to the script using arguments section in powershell task right so how to pass these parameters in a file. – PDBR PRAVEEN Oct 31 '17 at 08:35
  • @PDBRPRAVEEN Updated. – starian chen-MSFT Oct 31 '17 at 09:18
  • Hi MSFT, Here Webapp name means ResourceName or ResourceGroupName. Please confirm me. – PDBR PRAVEEN Oct 31 '17 at 10:24
  • I tried with -webJobName "XX" -userName "XX" -password "XX" -webAppName " ResourceGroupName" and -webJobName "XX" -userName "XX" -password "XX" -webAppName "ResourceName"...But I am getting the below error: – PDBR PRAVEEN Oct 31 '17 at 10:29
  • 2017-10-31T10:09:54.3429667Z ##[error]The remote server returned an error: (401) Unauthorized. 2017-10-31T10:09:54.3659661Z ##[section]Finishing: Azure PowerShell script: FilePath – PDBR PRAVEEN Oct 31 '17 at 10:31
  • As per this below line which you shared in the above script to include the parameter.json file ---->[object]$paramObj=Get-Content "PowerShellModuleProject1\parameter.json"|ConvertFrom-Json ........I have kept 2 files in VSTS repo under deployment scripts folder...one is parameter.json and another is for webjobsstart.ps1 script...But in the powershell script you are referencing the parameter.json by the above line.. – PDBR PRAVEEN Oct 31 '17 at 11:33
  • But as per my VSTS repo path,I have given like this: [object]$paramObj=Get-Content "d:\a\1\s\DeploymentScripts\Parameter.json"|ConvertFrom-Json but it is throwing error.can not find parameter.json file under this path . – PDBR PRAVEEN Oct 31 '17 at 11:33
  • So currently unauthorized issue and parameter.json file can't find issue is happening.Please help me out .. – PDBR PRAVEEN Oct 31 '17 at 11:35
  • @PDBRPRAVEEN The webAppName parameter is the name of web app in azure (app services). Regarding the file path, you can set the Working Folder of PowerShell task (e.g. `$(build.sourcesDirectory)`), then specify the relative path of file (e.g. DeploymentScripts\Parameter.json, `d:\a\1\s` is equal to `$(build.sourcesDirectory)`), you can check the file path in Get Source step. On the other hand, make sure the username and password are correct. – starian chen-MSFT Nov 01 '17 at 01:24
  • Hi MSFT, I tried with this [object]$paramObj=Get-Content "$(build.sourcesDirectory)\DeploymentScripts\Parameter.json" |ConvertFrom-Json and with this also [object]$paramObj=Get-Content $(build.sourcesDirectory)\DeploymentScripts\Parameter.json |ConvertFrom-Json both ways with double quotes and without double quotes but still I am facing the same error.. ##[error]The term 'build.sourcesDirectory' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct – PDBR PRAVEEN Nov 01 '17 at 03:17
  • I am firstly publishing the script file and parameter.son file from build defi and getting those 2 files into release defi there I am running the webjobs stop and start since it needs at the time of release right.So path I think I need to change because the parameter.json file is published and ready to take execute for the release..The path is d:\a\r1\a\WebJobs\drop\Parameter.json" now it is taking. – PDBR PRAVEEN Nov 01 '17 at 04:01
  • 2017-11-01T03:55:29.5316946Z ##[error]Unrecognized escape sequence. (49): { "userName": "abc\$def", "password": "password", "webAppName": "wan", "resourceGroup": "rg", "webJobs": [ { "name": "webjobname1", "typeName": "continuous" }, { "name": "webjobname2", "typeName": "continuouâ?Os" } ] } Now it is failing with escape sequence.in the above code – PDBR PRAVEEN Nov 01 '17 at 04:09
  • @PDBRPRAVEEN Using DeploymentScripts\Parameter.json instead (`Get-Content " DeploymentScripts\Parameter.json"`) and specify $(build.sourcesDirectory) in Working Folder of PowerShell task. Regarding the userName, the value is userName attribute of publishProfile section in Publish profile file(e.g. ` – starian chen-MSFT Nov 01 '17 at 06:24
  • If there is \, you can use `"userName":"abc\\$def"` – starian chen-MSFT Nov 01 '17 at 06:29
  • its throwing [error]Invalid JSON primitive: webappname – PDBR PRAVEEN Nov 01 '17 at 06:32
  • Hi MSFT, Now, Its running only one webjob..I have given 2 webjobs after running one webjob start then its throwing wrror that pipeline has been stopped.Do I need to change the script which you have given to include webjobs list – PDBR PRAVEEN Nov 01 '17 at 06:40
  • { "userName": "user1", "password": "password1", "webAppName": "webapp1", "resourceGroup": "resourceGroup1", "webJobs": [ { "name": "abc", "typeName": "continuous" }, { "name": "def", "typeName": "continuou‌" } ] } tell me any change is required in this script..currently I can execute only one webjob.. – PDBR PRAVEEN Nov 01 '17 at 06:54
  • @PDBRPRAVEEN `{ "name": "def", "typeName": "continuou‌s" }`, it is continuous not continuou. – starian chen-MSFT Nov 01 '17 at 07:04
  • The second job also continuous job only..But its not starting the second webjob – PDBR PRAVEEN Nov 01 '17 at 07:15
  • @PDBRPRAVEEN I mean the word is wrong, missing `s` letter. – starian chen-MSFT Nov 01 '17 at 07:18
  • No.I have added still its failing. – PDBR PRAVEEN Nov 01 '17 at 07:18
  • What's the result if you just start the second webjob? What's the detail error message? – starian chen-MSFT Nov 01 '17 at 07:20
  • ##[error]The pipeline has been stopped.only I can see this error. – PDBR PRAVEEN Nov 01 '17 at 07:22
  • But can I have the validation script as well after tht deploy of webapp I will start the webjobs and after that I need to validate whether all the continuous and triggered webjobs are running or not.. – PDBR PRAVEEN Nov 01 '17 at 07:34
  • I'd suggest asking separate questions in new forum threads and we can discuss it in a new thread. – starian chen-MSFT Nov 01 '17 at 07:35