为什么停止/天青缩放的时候的onStop不叫? [英] Why is Stopping/OnStop not called when scaling in in Azure?

查看:138
本文介绍了为什么停止/天青缩放的时候的onStop不叫?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RoleEnvironment.Stopping / RoleEntryPoint.OnStop()是的不要求临终情况下,当我减少我的角色实例计数的。他们的当实例重新启动或部署停止时调用的。我在做什么错了,还是我只是不应该需要在这种情况下,清理?

RoleEnvironment.Stopping/RoleEntryPoint.OnStop() are not called for dying instances when I reduce my role instance count. They are called when the instance is rebooted or the deployment is stopped. What am I doing wrong, or am I just not supposed to need to clean up in this case?

我有一个简单工作者角色(VS2012更新1,默认的云项目,一个工人的作用,增加的 smarx的表存储跟踪侦听)。所有code在这里;没有其他的依赖关系:

I have a simple worker role (VS2012 update 1, default Cloud project with one worker role, added smarx's Table storage trace listener). All code here; no other dependencies:

using System;
using System.Collections.Generic;
using System.Data.Services.Client;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Threading;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;

namespace WorkerRole1
{
    public class WorkerRole : RoleEntryPoint
    {
        bool shouldRun = true;
        EventWaitHandle runFinished = new EventWaitHandle(true, EventResetMode.ManualReset);

        public override bool OnStart()
        {
            ServicePointManager.DefaultConnectionLimit = 12;
            RoleEnvironment.Stopping += (object sender, RoleEnvironmentStoppingEventArgs e) => {
                Trace.WriteLine("WorkerRole1 Stopping called", "Information");
                shouldRun = false;
            };
            return base.OnStart();
        }

        public override void Run()
        {
            runFinished.Reset();
            try {
                Trace.WriteLine("WorkerRole1 entry point called", "Information");
                while (shouldRun) {
                    Thread.Sleep(10000);
                    Trace.WriteLine("Working", "Information");
                }
                Trace.WriteLine("Finished", "Information");
            } finally {
                runFinished.Set();
            }
        }

        public override void OnStop()
        {
            Trace.WriteLine("OnStop: Waiting for Run() to finish", "Information");
            runFinished.WaitOne();
            Trace.WriteLine("OnStop: Run() finished", "Information");
            base.OnStop();
        }
    }

    public class LogMessage : TableServiceEntity
    {
        public DateTime Time { get; set; }
        public string Message { get; set; }
        public string InstanceId { get; set; }
        public string Category { get; set; }

        public LogMessage() { }
        public LogMessage(string message, string category)
        {
            Message = message;
            Category = category;
            Time = DateTime.UtcNow;
            InstanceId = RoleEnvironment.CurrentRoleInstance.Id;
            PartitionKey = RoleEnvironment.DeploymentId;
            RowKey = (DateTime.MaxValue.Ticks - Time.Ticks).ToString("d19");
        }
    }

    public class TableTraceListener : TraceListener
    {
        private TableServiceContext _context = null;
        private TableServiceContext context
        {
            get
            {
                if (_context == null) {
                    var tables = CloudStorageAccount
                        .Parse(RoleEnvironment.GetConfigurationSettingValue(
                            Attributes["connectionStringName"] ?? "DataConnectionString"))
                        .CreateCloudTableClient();
                    tables.CreateTableIfNotExist("log");
                    _context = tables.GetDataServiceContext();
                    _context.MergeOption = MergeOption.NoTracking;
                }
                return _context;
            }
        }

        protected override string[] GetSupportedAttributes() { return new[] { "connectionStringName" }; }

        public override void Write(string message, string category)
        {
            context.AddObject("log", new LogMessage(message, category));
            context.SaveChangesWithRetries();
        }

        public override void WriteLine(string message, string category) { Write(message + "\n", category); }
        public override void Write(string message) { Write(message, null); }
        public override void WriteLine(string message) { Write(message + "\n"); }
    }
}


推荐答案

从我的运行实验,看来该结构控制器作用过程中删除从DB服务器的动态白名单中的作用的IP缩小操作(前的作用是充分去除)。

From my experimenting, it appears that the fabric controller removes the role IPs from the dynamic whitelist of the DB server during role scale down operations (before the role is fully removed).

如果这也是你的问题的原因,那么可能的解决方法可以是IP范围0.0.0.0-255.255.255.255手动添加到数据库服务器的白名单(在一些安全为代价)。或者你可以重新设计你的应用程序编写调用OnStop过程中的数据/消息队列,而不是DB(用于辅助角色后复制到DB)。

If this is also the cause of your problem, then a possible workaround could be to manually add the IP range 0.0.0.0-255.255.255.255 to the DB server's whitelist (at the expense of some security). Or you could re-architect your application to write data/messages to queues instead of the DB during OnStop (for a worker role to copy to DB later).

这篇关于为什么停止/天青缩放的时候的onStop不叫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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