应用程序关闭Azure但不关闭本地PC

时间:2016-11-21 01:11:03

标签: azure azure-webjobs long-running-processes

我在Azure上有一个很长的运行过程。碰巧执行在随机时刻停止,甚至没有留下日志或错误消息。有时它会连续几小时运行,有时只需几分钟。这不会发生在我的本地PC上。

有些人已经回答了类似的问题,建议将应用程序转为“永远在线”解决了这个问题,但在我的情况下却没有。问题还在继续。

我阅读了有关此事的其他帖子,并建议尝试使用WebJobs。我不能,因为我的应用程序有150MB,并且超过了WebJobs的最大文件大小。

关于项目:它是由第三方提供的重型人脸检测和识别算法的实现。 我看不到的所有代码都被try语句包围。

这就是我调用函数的方式:

Task.Run(()=> loopDeReconhecimento(biometricClient, code, photosInfo,ultimoIndiceReconhecido, totalNumberOfFiles,outPutDestionation));


private async void loopDeReconhecimento(NBiometricClient biometricClient, string code, List<PhotoInfo> photosInfo,int ultimoIndiceReconhecido, int totalNumberOfFiles,string outPutDestionation)
        {
            WebClient wc = new WebClient();
            for (int i = ultimoIndiceReconhecido; i < totalNumberOfFiles; i++)
            {
                if (forceStop.Contains(code))
                {
                    Log.register(code, "STOPPING!!!!");
                    forceStop.Remove(code);
                    return;
                }
                if (i >= photosInfo.Count)
                {
                    i--;
                    try
                    {
                        Log.register(code, "Fim das fotos upadas por enquanto foi encontrado. Esperando trinta segundos, baixando novamente as informações e tentando de novo " + DateTime.Now.ToLongTimeString());
                        Thread.Sleep(30000);

                        wc.DownloadFile(pathWebBase + code + @"/" + @"1.Eventos_grande_simples/imagensConfig.txt", outPutDestionation);
                        //Log.register(code,"Tempo de download: " + tempoTotal);
                        PhotoInfo.init(File.ReadAllLines(outPutDestionation), photosInfo);
                    } catch
                    {
                        Log.register(code, "Attempt to download failed. Try again in 30 seconds");
                    }
                    continue;
                }
                Log.register(code, "Starting photo " + i.ToString() + " de " + totalNumberOfFiles);


                recognizePhoto(biometricClient,wc, code, photosInfo[i], photosInfo, tentativasPorFoto);

                status = i.ToString() + @"/" + totalNumberOfFiles.ToString();
                if (forceSave.Contains(code) ||  (double)i / salvarACadaQuantas == Math.Floor((double)i / salvarACadaQuantas))
                {
                    forceSave.Remove(code);
                    salvar(i, code, photosInfo);

                }

            }

            Log.register(code, "Fim.");

        }


void recognizePhoto(NBiometricClient biometricClient,WebClient wc, string code, PhotoInfo photoInfo, List<PhotoInfo> photosInfo, int attempts)
{



            try
            {

                Log.register(code, "Foto iniciada: " + photoInfo.shortAdress);

                NBiometricStatus status;

                string localPath = localPathBase + code + @"\Fotos a separar\1.Eventos_grande" + photoInfo.shortAdress;
                Stopwatch sw = new Stopwatch();
                sw.Start();


                NSubject candidateSubject = CreateSubjectFromURL(pathWebBase + code + @"/1.Eventos_grande_simples" + photoInfo.shortAdress, true);

                status = biometricClient.CreateTemplate(candidateSubject);
                if (status != NBiometricStatus.Ok)
                {
                    Log.register(code, "Template creation was unsuccessful. Status: " + status);
                    return;
                }
                else
                {
                    Log.register(code, "Created: Status: " + status);

                }

                // Set ids to candidate subjects and related subjects
                int i = 1;
                candidateSubject.Id = "ID_0";
                Log.register(code, "Subject na foto: Status: " + candidateSubject.Id);
                PersonTagInfo pti = detalharFace(candidateSubject, biometricClient, code);
                if (pti != null)
                    photoInfo.peopleTags.Add(pti);
                foreach (var subject in candidateSubject.RelatedSubjects)
                {
                    subject.Id = string.Format("ID_{0}", i++);
                    Log.register(code, "Subject found in photo: Status: " + subject.Id);
                    pti = detalharFace(subject, biometricClient, code);
                    if (pti != null)
                        photoInfo.peopleTags.Add(pti);

                }


                identificarESalvarPersonTagInfo(biometricClient, photoInfo, candidateSubject, code);
                foreach (NSubject candidato in candidateSubject.RelatedSubjects)
                {
                    identificarESalvarPersonTagInfo(biometricClient, photoInfo, candidato, code);
                }

                photoInfo.done = true;
                Log.register(code, "Tempo de processamento: " + sw.ElapsedMilliseconds);
            } catch
            {
                if (attempts > 0)
                {
                    Log.register(code, "Erro ao processar foto. Tentando novamente em 1 segundo. Tentativas restantes: " + attempts.ToString());

                    Thread.Sleep(1000);
                    recognizePhoto(biometricClient,wc, code, photoInfo,photosInfo, attempts - 1);

                }

            }





        }

1 个答案:

答案 0 :(得分:0)

根据您的描述,您需要在平台上运行一个很长的过程。请尝试使用worker角色。工作者角色是定义为填充此角色的角色。它可以用于循环处理工作。它不是150M尺寸限制。我们还可以更好地控制VM。有关职员角色的更多信息,请参阅document

还有一些关于如何使用辅助角色进行编程的教程。  https://channel9.msdn.com/Series/Windows-Azure-Cloud-Services-Tutorials/Introduction-to-Windows-Azure-Worker-Roles-Part-1

https://channel9.msdn.com/Series/Windows-Azure-Cloud-Services-Tutorials/Introduction-to-Windows-Azure-Worker-Roles-Part-2