OnStart()有最大时间限制吗?

时间:2011-07-26 17:26:59

标签: .net azure

我正在使用AzureRubyRails sln,它是一个扩展RoleEntryPoint类的WorkerRole。 我注意到我的应用程序到达特定点(就在复制之前),然后Azure决定在表日志中没有任何推理的情况下重新启动角色。我是否有可能在OnStart中超过我的时间限制?

public override bool OnStart()
{
    try
    {
        LogInfo("Worker Role OnStart Entered");

        RoleEnvironment.Changing += RoleEnvironmentChanging;

        DiagnosticMonitorConfiguration dmc = DiagnosticMonitor.GetDefaultInitialConfiguration();

        dmc.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
        dmc.Logs.ScheduledTransferLogLevelFilter = LogLevel.Verbose;

        DiagnosticMonitor.Start("DiagnosticsConnectionString", dmc);

        this.roleId = RoleEnvironment.CurrentRoleInstance.Id;
        string outputContainer = RoleEnvironment.GetConfigurationSettingValue("OutputContainer");
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageAccount"));
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        this.container = blobClient.GetContainerReference(outputContainer);
        this.container.CreateIfNotExist();

        BlobContainerPermissions permissions = this.container.GetPermissions();
        permissions.PublicAccess = BlobContainerPublicAccessType.Container;
        this.container.SetPermissions(permissions);

        //Get the local Storage resources 
        LocalResource disk = RoleEnvironment.GetLocalResource("App");

        //Copy files across (log files and database are writeable)

        DirectoryInfo localStorageRoot = new DirectoryInfo(disk.RootPath);

        //Get the root of this role

        this.roleRoot = System.Environment.GetEnvironmentVariable("RoleRoot");
        this.roleRoot += "\\approot";

        try
        {
            LogInfo("Starting Git extract");
            var proc = new Process
            {
                StartInfo = new ProcessStartInfo(string.Format("{0}\\Ruby\\7za.exe", this.roleRoot), "x -oGit -y Git.7z")
                {
                    UseShellExecute = false,
                    WorkingDirectory = string.Format("{0}\\Ruby", this.roleRoot)
                },
                EnableRaisingEvents = true
            };
            proc.Start();

            proc.WaitForExit();
        }
        catch (Exception fErr)
        {
            LogError(fErr.Message);
        }

        try
        {
            LogInfo("Starting Git clone");
            var proc = new Process
            {
                StartInfo = new ProcessStartInfo(string.Format("{0}\\Ruby\\git\\bin\\git.exe", this.roleRoot), string.Format("clone git://github.com/callumj/InPerthAzure.git web_app", this.roleRoot))
                {
                    UseShellExecute = false,
                    WorkingDirectory = string.Format("{0}\\Ruby", this.roleRoot)
                },
                EnableRaisingEvents = true
            };
            proc.Start();

            proc.WaitForExit();
        }
        catch (Exception fErr)
        {
            LogError(fErr.Message);
        }

        string rubyFolderName = RoleEnvironment.GetConfigurationSettingValue("RubyFolder");
        this.rubyLocation = string.Format("{0}\\{1}", localStorageRoot.FullName, rubyFolderName);

        string rubySrc = string.Format("{0}\\{1}", this.roleRoot, rubyFolderName);

        try
        {
            LogInfo("Starting Ruby extraction");
            var proc = new Process
            {
                StartInfo = new ProcessStartInfo(Path.Combine(rubySrc, @"Ruby"))
                {
                    UseShellExecute = false,
                    WorkingDirectory = rubySrc
                },
                EnableRaisingEvents = true
            };
            proc.Start();

            proc.WaitForExit();
        }
        catch (Exception fErr)
        {
            LogError(fErr.Message);
        }

        LogInfo("Beginning copy");
        CopyFolder(string.Format("{0}\\{1}", this.roleRoot, rubyFolderName), this.rubyLocation);

        string appFolderName = RoleEnvironment.GetConfigurationSettingValue("AppFolder");
        this.appLocation = string.Format("{0}\\{1}", localStorageRoot.FullName, appFolderName);
        //CopyFolder(string.Format("{0}\\{1}", this.roleRoot, appFolderName), this.appLocation);

        string memcacheFolderName = RoleEnvironment.GetConfigurationSettingValue("MemcacheFolder");
        this.memcacheLocation = string.Format("{0}\\{1}", localStorageRoot.FullName, memcacheFolderName);

        //Get the local endpoint 
        LogInfo("Acquiring endpoint");
        this.endPoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Server"].IPEndpoint;

        // Start the server

        LogInfo("Beginning bootup");

        BootApp();

        LogInfo("Worker Role OnStart Exited");
    }
    catch (Exception err)
    {
        LogError(err.Message);
    }
    return base.OnStart();
}

我发现它在我的本地开发人员计算模拟器上运行良好(但我认为这并不意味着太多)。

1 个答案:

答案 0 :(得分:1)

我怀疑您的代码在Role中崩溃,因为缺少某些依赖项。要解决这个问题,我会尝试将您的代码拆分为具有巨大try / catch的控制台应用程序。然后我会使用RDP登录到计算机,然后尝试运行控制台应用程序。这将为您提供堆栈跟踪,并允许您找出失败的位置。只需确保从与RoleEntryPoint相同的目录启动它,因此它具有相同的相对路径。

但一般来说,OnStart()中没有超时,你会遇到。您的角色看起来会一直很忙,可能会被报告为“无响应”,但仍会运行OnStart。

我的一般经验法则是你的OnStart和Startup任务完成时间不到20分钟,或者你做的太多了。

最后 - 因为你正在使用Git和诸如此类的东西 - 看看Steve的smarxrole(bing it),因为它有所有的启动工作,并且是这类事物的一个很好的起点。