Exemplo n.º 1
0
        private async void DoWork()
        {
            while (true)
            {
                using (var scope = Services.CreateScope())
                {
                    var db    = scope.ServiceProvider.GetRequiredService <MVDContext>();
                    var steps = await db.MonitorSteps
                                .Where(x =>
                                       x.Type == MVDMonitorStepTypes.Request && x.Status != MVDMonitorStepStatusTypes.Processing &&
                                       DateTime.UtcNow > x.LastCheckDate.AddSeconds(x.Interval)
                                       )
                                .OrderBy(x => x.LastCheckDate)
                                .Take(20)
                                .ToListAsync();

                    foreach (var step in steps)
                    {
                        var settings = step.SettingsAsRequest();
                        if (!string.IsNullOrEmpty(settings.Url))
                        {
                            var log = new MVDMonitorStepLog
                            {
                                MonitorId     = step.MonitorId,
                                MonitorStepId = step.MonitorStepId,
                                StartDate     = DateTime.UtcNow,
                                Interval      = step.Interval,
                                Status        = MVDMonitorStepStatusTypes.Processing
                            };
                            db.Add(log);
                            await db.SaveChangesAsync(_token);

                            try
                            {
                                var client = new HttpClient();
                                client.Timeout = TimeSpan.FromSeconds(15);
                                var result = await client.GetAsync(settings.Url, _token);

                                if (result.IsSuccessStatusCode)
                                {
                                    log.Status = MVDMonitorStepStatusTypes.Success;
                                }
                                else
                                {
                                    log.Status = MVDMonitorStepStatusTypes.Fail;
                                }
                            }
                            catch (HttpRequestException rex)
                            {
                                log.Log    = rex.Message;
                                log.Status = MVDMonitorStepStatusTypes.Fail;
                            }
                            catch (Exception ex)
                            {
                                log.Log    = ex.Message;
                                log.Status = MVDMonitorStepStatusTypes.Error;
                            }
                            finally
                            {
                                log.EndDate = DateTime.UtcNow;
                            }

                            if (log.Status == MVDMonitorStepStatusTypes.Success)
                            {
                                step.Status = MVDMonitorStepStatusTypes.Success;
                            }
                            else if (log.Status == MVDMonitorStepStatusTypes.Error)
                            {
                                step.Status = MVDMonitorStepStatusTypes.Error;
                            }
                            else
                            {
                                step.Status = MVDMonitorStepStatusTypes.Fail;
                            }
                        }
                        step.LastCheckDate = DateTime.UtcNow;
                        await db.SaveChangesAsync(_token);
                    }
                }
                await Task.Delay(500, _token);
            }
        }
Exemplo n.º 2
0
        public async void DoWork()
        {
            while (true)
            {
                using (var scope = _services.CreateScope())
                {
                    var db = scope.ServiceProvider.GetRequiredService <MVDContext>();

                    // hiç işlem yapmadığım 20 adet step
                    var steps = await db.MonitorSteps
                                .Where(x => x.Type == MVDMonitorStepTypes.Request &&
                                       x.Status != MVDMonitorStepStatusType.Processing)
                                .Take(20)
                                .ToListAsync();

                    foreach (var step in steps)
                    {
                        var settings = step.SettingsAsRequest();
                        if (string.IsNullOrEmpty(settings.Url))

                        {
                            //yaptığım her site işlemi için log yaz.
                            var log = new MVDMonitorStepLog
                            {
                                MonitorId     = step.MonitorId,
                                MonitorStepId = step.Id,
                                StartDate     = DateTime.UtcNow,
                                Interval      = step.Interval,
                                Status        = MVDMonitorStepStatusType.Processing
                            };
                            db.Add(log);

                            // Bu servis cancel edildiği zaman, asenkron işlemlerin bildirilmesini sağlamak için, token parametresi geçtim.
                            await db.SaveChangesAsync(_token);

                            try
                            {
                                //requesti at.
                                var client = new HttpClient();
                                client.Timeout = TimeSpan.FromSeconds(15);
                                var result = await client.GetAsync(settings.Url, _token);

                                if (result.IsSuccessStatusCode)
                                {
                                    log.Status = MVDMonitorStepStatusType.Success;
                                }
                                else
                                {
                                    log.Status = MVDMonitorStepStatusType.Fail;
                                }
                            }
                            catch (Exception ex)
                            {
                                log.Log    = ex.Message;
                                log.Status = MVDMonitorStepStatusType.Error;
                            }
                            finally
                            {
                                log.EndDate = DateTime.UtcNow;
                            }

                            if (log.Status == MVDMonitorStepStatusType.Success)
                            {
                                step.Status = MVDMonitorStepStatusType.Success;
                            }
                            else if (log.Status == MVDMonitorStepStatusType.Error)
                            {
                                step.Status = MVDMonitorStepStatusType.Error;
                            }
                            else
                            {
                                step.Status = MVDMonitorStepStatusType.Fail;
                            }
                        }

                        step.LastCheckDate = DateTime.UtcNow;

                        await db.SaveChangesAsync(_token);
                    }
                }

                await Task.Delay(500, _token);
            }
        }