且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何在Azure中获取特定Webjob的进程ID?

更新时间:2022-04-29 02:01:44

我想杀死Web作业,所以我如何获取Web作业进程ID?是否可以获取Web作业进程ID&通过编程杀死网络工作?

I want to kill web job so how can I get web job process id?is it possible to get web job process id & kill a web jobs using programming?

根据您的描述,我建议您可以使用kudu webjob stop rest api停止webjob.

According to your description, I suggest you could use kudu webjob stop rest api stop the webjob.

终止作业后,Web作业将自动重启.

The web job will atuomatic restart after you killed the process.

您可以使用Kudu rest api来满足您的要求.

You could use Kudu rest api to achieve your requirement.

您可以首先在azure Web应用程序中设置部署凭据,如下所示:

You could firstly set a Deployment credentials in your azure web application as below:

注意:记住用户名和密码,我们将使用它们来生成访问令牌

Notice:Remember the user name and password, we will use them to generate the access token

然后,您可以编写代码以发送请求以停止网络作业,如下所示:

Then you could write the code to send the request to stop your web job as below:

网址: https://yourwebsitename.scm.azurewebsites.net/api/continuouswebjobs/TestWebJob/stop

更多详细信息,您可以参考以下代码:

More details, you could refer to below codes:

   string url = @"https://yourwebsitename.scm.azurewebsites.net/api/continuouswebjobs/TestWebJob/stop";

    var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

    httpWebRequest.Method = "POST";
    httpWebRequest.ContentLength = 0;

    string logininforation = "usename:password";

    byte[] byt = System.Text.Encoding.UTF8.GetBytes(logininforation);
    string encode = Convert.ToBase64String(byt);


    httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "Basic " + encode);


    // Get the response
    HttpWebResponse httpResponse = null;

    httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

    Console.WriteLine(httpResponse.StatusCode);

如果您仍然想终止该进程,建议您将请求发送到下面的url,以首先获取该进程.

If you still want to kill the process, I suggest you could send the request to below url to get the process firstly.

网址: https://yourwebsitename.scm.azurewebsites.net/api/processes

如果获得了该进程,则可以向kudu发送删除请求以终止该Webjob的进程.

If you get the process the you could send a delete request to the kudu to kill the webjob's process.

更多详细信息,您可以参考以下C#代码:

More details, you could refer to below C# codes:

    string url = @"https://yourwebsitename.scm.azurewebsites.net/api/processes";

    var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

    httpWebRequest.Method = "GET";
    httpWebRequest.ContentLength = 0;

    string logininforation = "username:password";

    byte[] byt = System.Text.Encoding.UTF8.GetBytes(logininforation);
    string encode = Convert.ToBase64String(byt);


    httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "Basic " + encode);


    using (HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse())
    {
        using (System.IO.StreamReader r = new System.IO.StreamReader(response.GetResponseStream()))
        {
            string jsonResponse = r.ReadToEnd();

            dynamic result = JsonConvert.DeserializeObject(jsonResponse);

            dynamic resultList = result.Children();

            foreach (var item in resultList)
            {
                Console.WriteLine(item.name);
                if (item.name == "yourwebjobname")
                {
                    Console.WriteLine(item.href);
                    //begin to delete the webjob process
                    string url2 = item.href;
                    var httpWebRequest2 = (HttpWebRequest)WebRequest.Create(url2);
                    httpWebRequest2.Headers.Add(HttpRequestHeader.Authorization, "Basic " + encode);
                    httpWebRequest2.Method = "DELETE";
                    httpWebRequest2.ContentLength = 0;
                    HttpWebResponse httpResponse2 = null;
                    httpResponse2 = (HttpWebResponse)httpWebRequest2.GetResponse();

                }
            }
        }
    }