问题
I have found one plugin azure app service webjob start in VSTS but I can't start/stop the webjob.Please help me guys. I am getting the below error while running the task in VSTS.
Error:
2017-09-25T12:26:37.3203696Z Preparing task execution handler.
2017-09-25T12:26:43.2660796Z Executing the powershell script:
d:\a\_tasks\Start_cbebb02b-11c6-4e24-b5a8-0b51366d51b7\0.2.5\Start.ps1
2017-09-25T12:26:43.6830868Z ##
[error]System.Management.Automation.ParentContainsErrorRecordException: At
D:\a\_tasks\Start_cbebb02b-11c6-4e24-b5a8-0b51366d51b7\0.2.5\Start.ps1:13
char:16
2017-09-25T12:26:43.6830868Z + $WebAppName
2017-09-25T12:26:43.6830868Z + ~
2017-09-25T12:26:43.6830868Z Missing ')' in function parameter list.
2017-09-25T12:26:43.6830868Z
2017-09-25T12:26:43.6830868Z At D:\a\_tasks\Start_cbebb02b-11c6-4e24-b5a8-
0b51366d51b7\0.2.5\Start.ps1:17 char:1
2017-09-25T12:26:43.6830868Z + )
2017-09-25T12:26:43.6830868Z + ~
2017-09-25T12:26:43.6830868Z Unexpected token ')' in expression or
statement.
回答1:
Using Invoke-AzureRMResourceAction
command:
- Add Azure PowerShell task to build/release definition
Code:
Triggered WebJob:
Invoke-AzureRmResourceAction -ResourceGroupName XXX -ResourceType Microsoft.Web/sites/TriggeredWebJobs -ResourceName [web app name]/[web job name] -Action run -ApiVersion 2015-08-01 -force
Continuous WebJob:
Invoke-AzureRmResourceAction -ResourceGroupName XXX -ResourceType Microsoft.Web/sites/ContinuousWebJobs -ResourceName [web app name]/[web job name] -Action [start/stop] -ApiVersion 2015-08-01 -Force
Simple sample script with multiple webjobs:
param(
[object[]]$webjobs,
[string]$website,
[string]$rg
)
foreach($wj in $webjobs){
if($wj.typeName -eq "continuous")
{
Invoke-AzureRmResourceAction -ResourceGroupName $rg -ResourceType Microsoft.Web/sites/ContinuousWebJobs -ResourceName "$website/$($wj.name)" -Action start -ApiVersion 2015-08-01 -Force
}
else{
Invoke-AzureRmResourceAction -ResourceGroupName $rg -ResourceType Microsoft.Web/sites/TriggeredWebJobs -ResourceName "$website/$($wj.name)" -Action run -ApiVersion 2015-08-01 -force
}
}
Specify parameter:
-webjobs @(@{"name"="webjob1";"typeName"='continuous'},@{"name"="webjob2";"typeName"='continuous'},@{"name"="webjob3";"typeName"='triggered '}) -website XX -rg XX
回答2:
public static string token = System.Configuration.ConfigurationManager.AppSettings["token"];
public static string continuouswebjobs = System.Configuration.ConfigurationManager.AppSettings["continuouswebjobs"];
public static string triggeredwebjobs = System.Configuration.ConfigurationManager.AppSettings["triggeredwebjobs"];
public static string hostName = System.Configuration.ConfigurationManager.AppSettings["DLUHostName"];
public static void StopContinuousWebJob(TextWriter log)
{
HttpClient request = new HttpClient();
request.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
request.DefaultRequestHeaders.Add("Authorization", token);
if (!string.IsNullOrEmpty(continuouswebjobs))
{
string[] jobsArray = continuouswebjobs.Split(',');
for (int i = 0; i < jobsArray.Length; i++)
{
HttpResponseMessage response = request.PostAsync(new Uri(hostName + "continuouswebjobs/" + jobsArray[i] + "/stop"), null).Result;
if (response.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine("Job " + jobsArray[i] + " stopped successfully.");
}
}
}
}
来源:https://stackoverflow.com/questions/46405285/could-any-one-help-me-how-to-stop-and-start-azure-webjobs-through-vsts