0

我有一个持续时间很长的网络作业(TimerTrigger)正在运行。

它偶尔会失败。

它持续4到5个小时。

当它失败时,不会引发异常。

这可能是一个尺寸错误的计划问题。

有没有办法定义正确的计划?

此外,当重新启动 webjob 时,先前的事件会重新启动,我们希望避免这种情况,并且只应重新启动下一个事件。

环境:Azure WebJob、.netcore (3.1) 堆栈、始终开启、64 位、存储帐户写作、网络作业的 autofac 依赖注入。

任何想法 ?

代码 :

        public FeedJob(IFeedConfiguration configuration, IKeyVaultService keyVaultService, IStorageTableService storageTableService, IBlobStorageService blobStorageService)
    {
        _configuration = configuration as FeedConfiguration;
        _keyVaultService = keyVaultService;
        _storageTableService = storageTableService;
        _blobStorageService = blobStorageService;
    }
    /// <summary>
    /// run periodically to request  web service
    /// </summary>
    /// <param name="myTimer"></param>
    /// <param name="log"></param>
    /// <returns></returns>
    public async Task ProcessTimerJob([TimerTrigger("%RunEvery%")] TimerInfo myTimer, ILogger log)
    {
        try
        {
            logJob = log;
            _cptTotal = _cptErrors = _cptNominals = 0;
            _dtStart = DateTime.UtcNow;
            var dtStartForReconstruction = _dtStart.AddHours(-1);

            logJob.LogInformation($"OP- =========== Job -  - =========== Started at : {_dtStart}");

            // certificate necessary to be allowed to request  (soap)
            //var Saurcertificate = X509Helper.GetCertificate(_configuration.CertificateThumbprint);
            var Saurcertificate = await _keyVaultService.GetCertificateFromKeyVaultByThumbprint(_configuration.CertificateThumbprint);

            var requests = await _blobStorageService.GetRequests(_configuration.InRequest);
            var cl = new List<DataColumn>();


            // parquet data are loaded in memory (about 17500 records)
            cl = await _blobStorageService.GetDataParquet(_dtStart);

            var borneInf = 0;
            var borneSup = cl[2].Data.Length;
            _cptTotal = 0;
            _errors = new Dictionary<string, int>();

            foreach (var req in requests)
            {
                var subEltsXml = req.Descendants().ToList();
                var endPointXml = subEltsXml.FirstOrDefault(e => e.Name == "EndPoint");
                var soapRequestXml = subEltsXml.FirstOrDefault(e => e.Name == "ReqSoap");
                _prefix = subEltsXml.FirstOrDefault(e => e.Name == "Prefix").Value;
                _title = subEltsXml.FirstOrDefault(e => e.Name == "Title").Value;

                DateTime dateFin, dateDeb;

                if (_prefix == "CCPM")
                {
                    dateDeb = _dtStart;
                    dateFin = _dtStart.AddYears(1).AddDays(-1);
                }
                else
                {
                    //CDC
                    dateFin = _dtStart;
                    dateDeb = _dtStart.AddDays(-1);
                }
                _dayFolder = $"{dateFin:yyyyMMdd}";

                logJob.LogInformation($@"OP- {_prefix} {_dtStart} =========== Job -  - =========== Extraction {_title} from  {dateDeb:yyyy-MM-dd} to {dateFin:yyyy-MM-dd}");
                logJob.LogInformation($@"OP- {_prefix} {_dtStart} =========== Job -  - =========== Extraction {_title} from pdl {borneInf} to pdl {borneSup - 1}");

                if (endPointXml != null && soapRequestXml != null)
                {
                    _cptTotalReq = _cptErrorsReq = _cptNominalsReq = 0;

                    for (int i = borneInf; i < borneSup; i++)
                    {
                        _cptTotalReq++;

                        var pdl = ((string[])cl[2].Data)[i];
                        var tarif = ((string[])cl[0].Data)[i];

                        var res = SetRequest(req, pdl, tarif, dateDeb, dateFin);

                        if (res.Item2)
                        {

                            var req2 = res.Item1;

                            var client = new RestClient(endPointXml.Value);
                            client.ClientCertificates = new X509CertificateCollection() { Saurcertificate };
                            client.Timeout = -1;

                            var request = new RestRequest(Method.POST);
                            request.AddHeader("Content-Type", "application/xml");
                            request.AddParameter("application/xml", req2.Descendants().ToList().FirstOrDefault().ToString(), ParameterType.RequestBody);

                            var response = await client.ExecuteAsync(request);

                            await WriteResultAsync(pdl, endPointXml.Value, response.Content, req2.Descendants().ToList().FirstOrDefault().ToString());
                        }

                        if (_cptTotalReq % 3000 == 0)
                        {
                            logJob.LogInformation($"OP- {_prefix} {DateTime.UtcNow} =========== Job -  - =========== {_cptTotalReq} / {borneSup - borneInf} pdls treated ");
                        }
                    }


                    _cptTotal += _cptTotalReq;
                    _cptNominals += _cptNominalsReq;
                    _cptErrors += _cptErrorsReq;
                }
            }


            await _storageTableService.WriteReport(new ReportRecord
            {
                PartitionKey = $"{_dtStart:yyyy-MM-dd-HH-mm-ss-fff}",
                RowKey = _prefix,
                DayFolder = _dayFolder,
                PdlsRetrieved = _cptTotalReq,
                PdlsTreated = _cptNominalsReq + _cptErrorsReq,
                PdlsTreatedOK = _cptNominalsReq,
                PdlsTreatedKO = _cptErrorsReq,
                PdlsNotTreated = _cptTotalReq - _cptNominalsReq - _cptErrorsReq,
                StatusesErrorXML = statusesErrorXML.ToString(),
                TariffsNotTreatedXML = tariffsNotTreatedXML.ToString()
            });

            logJob.LogInformation($@"OP- {DateTime.UtcNow}  Job -  - ================================== Total all extractions ==================================");
            logJob.LogInformation($@"OP- {DateTime.UtcNow}  Job -  - =========== Total pdls treated : {_cptTotal}");
            logJob.LogInformation($@"OP- {DateTime.UtcNow}  Job -  - =========== Total pdls OK : {_cptNominals}");
            logJob.LogInformation($@"OP- {DateTime.UtcNow}  Job -  - =========== Total pdls KO : {_cptErrors}");

            logJob.LogInformation($@"OP- {_dtStart}  Job -  - =========== Ended at : {DateTime.UtcNow}");

        }
        catch (Exception e)
        {
            logJob.LogInformation($@"OP- {_prefix} {_dtStart} =========== Job -  - =========== Exception generale at : {DateTime.UtcNow}");
            if (e.InnerException != null)
            {
                logJob.LogError($@"OP- {_prefix} {_dtStart} =========== Job -  - =========== Exception generale Message : {e.Message}");
                logJob.LogError($@"OP- {_prefix} {_dtStart} =========== Job -  - =========== Exception generale Inner Message : {e.InnerException.Message}");
            }
            else
            {
                logJob.LogError($@"OP- {_prefix} {_dtStart} =========== Job -  - =========== Exception generale Message : {e.Message}");
            }
        }
    }

问候。

4

0 回答 0