public async Task <IActionResult> Search()
        {
            IActionResult result = null;

            try
            {
                using ILifetimeScope scope = _container.BeginLifetimeScope();
                SettingsFactory     settingsFactory = scope.Resolve <SettingsFactory>();
                CoreSettings        settings        = settingsFactory.CreateCore(_settings.Value);
                IPurgeWorkerFactory factory         = scope.Resolve <IPurgeWorkerFactory>();
                IMapper             mapper          = MapperConfigurationFactory.CreateMapper();
                result = Ok(
                    (await factory.GetAll(settings))
                    .Select <IPurgeWorker, PurgeWorker>(innerPurgeWorker => mapper.Map <PurgeWorker>(innerPurgeWorker))
                    );
            }
            catch (System.Exception ex)
            {
                using (ILifetimeScope scope = _container.BeginLifetimeScope())
                {
                    await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value);
                }
                result = StatusCode(StatusCodes.Status500InternalServerError);
            }
            return(result);
        }
Beispiel #2
0
        private static async Task StartPurge()
        {
            using ILifetimeScope scope = _container.BeginLifetimeScope();
            SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>();
            ITraceService   traceService    = scope.Resolve <ITraceService>();

            await WriteTrace(traceService, settingsFactory, "Start Purge");
            await InitializeWorkers();

            CoreSettings        settings      = settingsFactory.CreateCore(_settings);
            IPurgeWorkerFactory workerFactory = scope.Resolve <IPurgeWorkerFactory>();
            IPurgeWorkerSaver   workerSaver   = scope.Resolve <IPurgeWorkerSaver>();
            Guid?workerId = await workerFactory.Claim(settings);

            while (workerId.HasValue && !workerId.Value.Equals(Guid.Empty))
            {
                await WriteTrace(traceService, settingsFactory, $"Claimed worker {workerId.Value}");

                IPurgeWorker purgeWorker = await workerFactory.Get(settings, workerId.Value);

                try
                {
                    await Purge(purgeWorker);
                }
                catch (System.Exception ex)
                {
                    purgeWorker.Status = PurgeWorkerStatus.Error;
                    try
                    {
                        await scope.Resolve <IExceptionService>().Create(settingsFactory.CreateLog(_settings), _settings.ExceptionLoggingDomainId, ex);
                    }
                    catch { }
                    throw;
                }
                finally
                {
                    await workerSaver.Update(settings, purgeWorker);
                    await WriteTrace(traceService, settingsFactory, $"Updated Worker {workerId.Value} Status {purgeWorker.Status}");
                }
                workerId = await workerFactory.Claim(settings);
            }
            try
            {
                await WriteTrace(traceService, settingsFactory, "Purging meta data");
                await PurgMetaData(settingsFactory, scope.Resolve <IPurgeSaver>());
            }
            catch (System.Exception ex)
            {
                Console.Error.WriteLine(ex.ToString());
                try
                {
                    await scope.Resolve <IExceptionService>().Create(settingsFactory.CreateLog(_settings), _settings.ExceptionLoggingDomainId, ex);
                }
                catch { }
            }
        }
        public async Task <IActionResult> UpdateStatus([FromRoute] Guid?id, [FromBody] Dictionary <string, object> patch)
        {
            IActionResult result = null;

            try
            {
                if (result == null && (!id.HasValue || id.Value.Equals(Guid.Empty)))
                {
                    result = BadRequest("Missing id parameter value");
                }
                if (result == null && patch == null)
                {
                    result = BadRequest("Missing patch data");
                }
                if (result == null)
                {
                    using ILifetimeScope scope = _container.BeginLifetimeScope();
                    SettingsFactory     settingsFactory  = scope.Resolve <SettingsFactory>();
                    CoreSettings        settings         = settingsFactory.CreateCore(_settings.Value);
                    IPurgeWorkerFactory factory          = scope.Resolve <IPurgeWorkerFactory>();
                    IPurgeWorker        innerPurgeWorker = await factory.Get(settings, id.Value);

                    if (innerPurgeWorker == null)
                    {
                        result = NotFound();
                    }
                    else
                    {
                        if (patch.ContainsKey("Status"))
                        {
                            innerPurgeWorker.Status = (PurgeWorkerStatus)Convert.ChangeType(patch["Status"], typeof(short));
                        }
                        IPurgeWorkerSaver saver = scope.Resolve <IPurgeWorkerSaver>();
                        await saver.Update(settings, innerPurgeWorker);

                        IMapper mapper = MapperConfigurationFactory.CreateMapper();
                        result = Ok(mapper.Map <PurgeWorker>(innerPurgeWorker));
                    }
                }
            }
            catch (System.Exception ex)
            {
                using (ILifetimeScope scope = _container.BeginLifetimeScope())
                {
                    await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value);
                }
                result = StatusCode(StatusCodes.Status500InternalServerError);
            }
            return(result);
        }
        public async Task <IActionResult> Get([FromRoute] Guid?id)
        {
            IActionResult result = null;

            try
            {
                if (result == null && (!id.HasValue || id.Value.Equals(Guid.Empty)))
                {
                    result = BadRequest("Missing id parameter value");
                }
                if (result == null)
                {
                    using ILifetimeScope scope = _container.BeginLifetimeScope();
                    SettingsFactory     settingsFactory  = scope.Resolve <SettingsFactory>();
                    CoreSettings        settings         = settingsFactory.CreateCore(_settings.Value);
                    IPurgeWorkerFactory factory          = scope.Resolve <IPurgeWorkerFactory>();
                    IPurgeWorker        innerPurgeWorker = await factory.Get(settings, id.Value);

                    if (innerPurgeWorker == null)
                    {
                        result = NotFound();
                    }
                    else
                    {
                        IMapper mapper = MapperConfigurationFactory.CreateMapper();
                        result = Ok(mapper.Map <PurgeWorker>(innerPurgeWorker));
                    }
                }
            }
            catch (System.Exception ex)
            {
                using (ILifetimeScope scope = _container.BeginLifetimeScope())
                {
                    await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value);
                }
                result = StatusCode(StatusCodes.Status500InternalServerError);
            }
            return(result);
        }