如何在没有“始终开启"的情况下保持 Azure WebJob 运行? [英] How can I keep my Azure WebJob running without "Always On"

查看:34
本文介绍了如何在没有“始终开启"的情况下保持 Azure WebJob 运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与网站相关联的连续网络作业,并且我正在以共享模式运行该网站.我不想选择 Always On 选项,因为我的应用程序并不真正需要.我只想在有人访问我的网站时处理消息.

I have a continous webjob associated with a website and I am running that website on the Shared mode. I don't want to go to Always On option as there is no real need for my application. I only want to process the message when the calls are made to my website.

我的问题是,即使我每 5 分钟不断在我的网站上调用一个虚拟的保持活动方法,该方法会向该网络作业监控的队列中发布一条消息,但几分钟后该作业仍会停止.

My issue is that the job keeps stopping after few minutes even though I am continuously calling a dummy keep alive method on my website at every 5 minute that posts a message to the queue that is monitored by that webjob.

我的 webjob 是一个使用 WebJob SDK 构建的简单控制台应用程序,代码如下

My webjob is a simple console application built using the WebJob SDK that has a code like this

JobHost host = new JobHost(new JobHostConfiguration(storageConnictionSttring));
host.RunAndBlock();

消息处理函数如下所示:

and the message processing function looks like below:

public static void ProcessKeepAliveMessages([QueueTrigger("keepalive")] KeepAliveTrigger message)
{
    Console.WriteLine("Keep Alive message called on :{0}", message.MessageTime);
}

作业的消息日志基本上说

The message log for the job basically says says

[03/05/2015 18:51:02 > 4660f6: SYS INFO] WebJob is stopping due to website shutting down

我不介意以这种方式发生这种情况,但是当网站从下一次保持活动调用开始时,webjob 不会启动.所有消息都在排队,直到我进入管理仪表板或 SCM 门户,如下所示

I don't mind if that happen this way, but when the website starts with the next call to keep alive, the webjob is not started. All the messages are queued till I go to the management dashboard or the SCM portal as shown below

https://mysite.scm.azurewebsites.net/api/continuouswebjobs

我可以看到这样的状态:

I can see the status like this:

[{"status":"Starting","detailed_status":"4660f6 - Starting
","log_url":"https://mysite.scm.azurewebsites.net/vfs/data/jobs/continuous/WebJobs/job_log.txt","name":"WebJobs","run_command":"mysite.WebJobs.exe","url":"https://mysite.scm.azurewebsites.net/api/continuouswebjobs/WebJobs","extra_info_url":"https://mysite.scm.azurewebsites.net/azurejobs/#/jobs/continuous/WebJobs","type":"continuous","error":null,"using_sdk":true,"settings":{}}]

如果有人能帮助我了解这里出了什么问题,我将不胜感激.

I would really appreciate if someone can help me understand what is going wrong here.

推荐答案

我遇到了类似的问题.我有一个网站(共享模式)和一个关联的 webjob(连续类型).查看 webjob 日志,我发现作业在大约 15 分钟后进入停止状态.不活动并停止对触发消息做出反应.这似乎与连续工作概念的概念相矛盾,但显然,要使其真正连续运行,您必须订阅付费网站.你得到你所付出的...

也就是说,我的网站大约每隔几天就需要使用一次,并且在共享模式下运行非常有意义.我不介意网站需要一些额外的时间来启动 - 只要它自动重新启动.webjob 的问题是一旦停止它就不会自行重新启动.所以,我的目标是通过网站重新启动它.

我注意到只需查看 Azure 管理门户中的 web 作业即可启动它.按照这个思路,我发现获取webjob的属性足以将其切换到运行状态.唯一的技巧是如何以编程方式获取属性,以便重新启动网站也将重新启动 webjob.

因为获取 webjob 属性的调用必须经过身份验证,所以第一步是转到 Azure 管理门户并下载网站发布配置文件.在发布配置文件中,您可以找到身份验证凭据:用户名(通常为 $)和 userPWD(密码的哈希值).把它们抄下来.

这是一个获取 webjob 属性并唤醒它的函数(如果尚未运行):

I've run into a similar problem. I have a website (shared mode) and an associated webjob (continuous type). Looking at webjob logs, I found that the job enters stopped state after about 15 min. of inactivity and stops reacting to trigger messages. It seems contradictory to the concept of continuous job concept but, apparently, to get it running truly continuously you have to subscribe to a paid website. You get what you pay for...

That said, my website needs to be used only about every few days and running in a shared mode makes perfect sense. I don't mind that the site needs a bit extra time to get started - as long as it restarts automatically. The problem with the webjob is that once stopped it won't restart by itself. So, my goal was to restart it with the website.

I have noticed that a mere look at the webjob from Azure Management Portal starts it. Following this line of thinking, I have found that fetching webjob properties is enough to switch it to the running state. The only trick is how to fetch the properties programmatically, so that restarting the website will also restart the webjob.

Because the call to fetch webjob properties must be authenticated, the first step is to go to Azure Management Portal and download the website publishing profile. In the publishing profile you can find the authentication credentials: username (usually $<website_name>) and userPWD (hash of the password). Copy them down.

Here is a function that will get webjob properties and wake it up (if not yet running):

class Program
{
    static void Main(string[] args)
    {
        string websiteName = "<website_name>";
        string webjobName = "<webjob_name>";
        string userName = "<from_publishing_profile>";
        string userPWD = "<from_publishing_profile>";
        string webjobUrl = string.Format("https://{0}.scm.azurewebsites.net/api/continuouswebjobs/{1}", websiteName, webjobName);
        var result = GetWebjobState(webjobUrl, userName, userPWD);
        Console.WriteLine(result);
        Console.ReadKey(true);
    }

    private static JObject GetWebjobState(string webjobUrl, string userName, string userPWD)
    {
        HttpClient client = new HttpClient();
        string auth = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(userName + ':' + userPWD));
        client.DefaultRequestHeaders.Add("authorization", auth);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        var data = client.GetStringAsync(webjobUrl).Result;
        var result = JsonConvert.DeserializeObject(data) as JObject;
        return result;
    }
}

您可以使用类似的函数来获取您网站中的所有 webjobs(使用端点 https://.scm.azurewebsites.net/api/webjobs).您还可以查看返回的 JObject 以验证 webjob 和其他属性的实际状态.

You can use a similar function to get all webjobs in your website (use endpoint https://<website_name>.scm.azurewebsites.net/api/webjobs). You may also look at the returned JObject to verify the actual state of the webjob and other properties.

这篇关于如何在没有“始终开启"的情况下保持 Azure WebJob 运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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