Пример #1
0
        public async Task <IActionResult> PutBatch(int id, Batch batch)
        {
            if (id != batch.RequestId)
            {
                return(BadRequest());
            }

            _context.Entry(batch).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BatchExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        User GetUser(BatchContext context, string userName)
        {
            var user = context.Users.Where(u => u.Name.Equals(userName)).FirstOrDefault();

            if (user == null)
            {
                lock ("User-adding")
                {
                    user = context.Users.Where(u => u.Name.Equals(userName)).FirstOrDefault();
                    if (user == null)
                    {
                        user = new Models.User()
                        {
                            Name = userName
                        };
                        context.Users.Add(user);
                        context.SaveChanges();

                        user = context.Users.Where(u => u.Name.Equals(userName)).FirstOrDefault();
                        context.Entry <User>(user).Collection(u => u.Batches).Load();
                    }
                }
            }
            return(user);
        }
Пример #3
0
        public override BatchQueueItem[] Process(BatchQueueItem[] items)
        {
            var ret = new List <BatchQueueItem>();

            foreach (var item in items)
            {
                using (var context = new BatchContext())
                {
                    var batch = context.Batches.Find(item.BatchId);

                    if (batch == null)
                    {
                        // This happens if the batch is deleted before extraction
                        ret.Add(item);
                    }
                    else
                    {
                        try
                        {
                            var parser = batch.CreateParser();
                            // Delete existing lines if needed
                            var oldLines = context.BatchLines.Where(bl => bl.Batch_BatchId == batch.BatchId);
                            context.BatchLines.RemoveRange(oldLines);
                            context.SaveChanges();

                            context.Entry <Batch>(batch).Reload();

                            var lines = parser.ToArray();
                            if (batch.Lines == null)
                            {
                                batch.Lines = new List <BatchLine>();
                            }

                            Array.ForEach <BatchLine>(lines, bl => batch.Lines.Add(bl));

                            batch.EnqueueAllAfterExtraction(context);
                            ret.Add(item);
                        }
                        catch (Exception ex)
                        {
                            Admin.LogException(ex);

                            batch.Status = BatchStatus.Error;
                            context.SaveChanges();
                        }
                        if (item.Impl.AttemptCount >= this.Impl.MaxRetry - 1)
                        {
                            // Max retry reached, clean up queueItem
                            ret.Add(item);
                        }
                    }
                }
            }
            return(ret.ToArray());
        }
Пример #4
0
        public override LineQueueItem[] Process(LineQueueItem[] items)
        {
            var ret = new List <LineQueueItem>();

            using (var context = new BatchContext())
            {
                foreach (var item in items)
                {
                    bool itemSucceeded = false;

                    var batchLine = context.BatchLines.Find(item.BatchLineId);

                    if (batchLine == null)
                    {
                        if (item.Impl.AttemptCount >= this.Impl.MaxRetry - 1)
                        {
                            // Max attempts reached - signal and remove anyway
                            ret.Add(item);
                        }
                        continue;
                    }

                    var partManager = new PartManager();
                    var soegObject  = batchLine.ToSoegObject();


                    if (soegObject != null)
                    {
                        // Try to search locally first
                        // Since Mass PNR Lookup does some name-matching itself first, we need the local search.
                        // There could be a local person found by CPR Broker, that Mass PNR Lookup does not match, therefore using "SourceUsageOrder.LocalThenExternal" is not enough
                        if (search(partManager, soegObject, batchLine, SourceUsageOrder.LocalOnly))
                        {
                            itemSucceeded = true;
                        }
                        else
                        {
                            // If no local person was found, search Externally
                            if (search(partManager, soegObject, batchLine, SourceUsageOrder.ExternalOnly))
                            {
                                itemSucceeded = true;
                            }
                        }
                    }
                    else
                    {
                        batchLine.Error = "Invalid address";
                    }

                    lock ("BatchCounts")
                    {
                        context.Entry <Batch>(batchLine.Batch).Reload(); // Reload to avoid overwriting the counts

                        if (itemSucceeded)
                        {
                            batchLine.Batch.SucceededLines++;
                        }


                        // Queue management
                        if (itemSucceeded)
                        {
                            ret.Add(item);
                            // Decrement the wait count on the semaphore
                            batchLine.Batch.GenerationSemaphore().Signal();
                        }
                        else if (item.Impl.AttemptCount >= this.Impl.MaxRetry - 1)
                        {
                            // Max attempts reached - signal and remove anyway
                            ret.Add(item);
                            batchLine.Batch.FailedLines++;

                            batchLine.Batch.GenerationSemaphore().Signal();
                        }
                        // Save the result at this point
                        context.SaveChanges();
                    }
                }
            }
            return(ret.ToArray());
        }
Пример #5
0
        public void Run()
        {
            string fileName = Guid.NewGuid().ToString();


            var userToken = "";
            var appToken  = CprBroker.Utilities.Constants.BaseApplicationToken.ToString();

            CprBroker.Engine.BrokerContext.Initialize(appToken, userToken);

            using (var myContext = new BatchContext())
            {
                Batch        _batch = null;
                Func <Batch> batch  = () =>
                {
                    if (_batch == null)
                    {
                        _batch = myContext.Batches.Where(b => b.FileName == fileName).SingleOrDefault();
                    }
                    else
                    {
                        myContext.Entry(_batch).Reload();
                    }
                    return(_batch);
                };

                var controller = new FilesController();
                var bytes      = Encoding.UTF8.GetBytes(Properties.Resources.Test_Opslag);

                controller.EnqueueFile(new MemoryStream(bytes), fileName, bytes.Length, "dummyUser");

                Assert.IsNotNull(batch());

                // Extract
                var extractQueue     = Queue.GetQueues <ExtractionQueue>().Single();
                var extractQueueItem = extractQueue.GetNext(1000).Where(qi0 => qi0.BatchId == batch().BatchId).SingleOrDefault();
                Assert.IsNotNull(extractQueueItem);

                var extractResult = extractQueue.Process(new BatchQueueItem[] { extractQueueItem });
                Assert.AreEqual(1, extractResult.Length);
                extractQueue.Remove(extractResult);

                // Search
                var searchQueue = Queue.GetQueues <SearchQueue>().Single();
                searchQueue.SourceUsageOrder = CprBroker.Schemas.SourceUsageOrder.LocalOnly;
                searchQueue.Impl.MaxRetry    = 1;

                var searchQueueItems = searchQueue.GetNext(1000).Where(qi => batch().Lines.Select(l => l.BatchElementId).Contains(qi.BatchLineId)).ToArray();
                Assert.AreEqual(batch().Lines.Count, searchQueueItems.Length);

                var searchResult = searchQueue.Process(searchQueueItems);
                //Assert.AreEqual(batch().Lines.Count, searchResult.Length);
                searchQueue.Remove(searchResult);

                // Output generation
                var outputGenerationQueue     = Queue.GetQueues <OutputGenerationQueue>().Single();
                var outputGenerationQueueItem = outputGenerationQueue.GetNext(1000).Where(qi => qi.BatchId == batch().BatchId).SingleOrDefault();
                Assert.IsNotNull(outputGenerationQueueItem);
                var outputGenerationResult = outputGenerationQueue.Process(new BatchQueueItem[] { outputGenerationQueueItem });
                Assert.AreEqual(1, outputGenerationResult.Length);
                var generated = Encoding.UTF8.GetString(batch().GeneratedContents);
                Assert.AreEqual(Properties.Resources.Test_Opslag_output, generated);

                // Notification
                var notificationQueue     = Queue.GetQueues <UserNotificationQueue>().Single();
                var notificationQueueItem = notificationQueue.GetNext(1000).Where(qi => qi.BatchId == batch().BatchId).SingleOrDefault();
                Assert.IsNotNull(notificationQueueItem);
                var notificationResult = notificationQueue.Process(new BatchQueueItem[] { notificationQueueItem });
                Assert.AreEqual(1, notificationResult.Length);
            }
        }