示例#1
0
        internal async Task ProcessFile(Guid uploadHistoryKey)
        {
            UploadHistory history = null;

            try
            {
                history = await historyStore.Find(uploadHistoryKey);

                var processor = ProcessorResolver.Resolve(history.Filename);
                if (processor == null)
                {
                    throw new Exception("The file format is not supported.");
                }

                var data = await historyStore.FindFileData(uploadHistoryKey);

                var parseResult = processor.Parse(data);
                var movies      = parseResult.Movies.Where(e => e.FilmingLocations.Any());

                foreach (var movie in movies)
                {
                    foreach (var location in movie.FilmingLocations)
                    {
                        var geocode = await geocodingService.Geocode(location.FormattedAddress);

                        if (geocode == null)
                        {
                            continue;
                        }

                        location.AddressKey       = geocode.Key;
                        location.FormattedAddress = geocode.FormattedAddress;
                        location.Latitude         = geocode.Latitude;
                        location.Longitude        = geocode.Longitude;
                    }

                    // Remove locations, that geocode service couldn't resolve.
                    movie.FilmingLocations = movie.FilmingLocations.Where(e => e.AddressKey != default(Guid)).ToList();

                    await movieService.Merge(movie);
                }

                history.Status = UploadStatus.Done;
                history.Errors = parseResult.Errors.ToArray();
                await historyStore.Update(history);
            }
            catch (Exception ex)
            {
                if (history != null)
                {
                    history.Status = UploadStatus.Done;
                    history.Errors = new[] { ex.Message };
                    await historyStore.Update(history);
                }
            }
        }
示例#2
0
        public async Task <OperationResult <UploadHistory> > ScheduleForProcessing(Abstraction.Service.File file)
        {
            var processor = ProcessorResolver.Resolve(file.Filename);

            if (processor == null)
            {
                return(OperationResult <UploadHistory> .Failed("The file format is not supported."));
            }

            var history = new UploadHistory
            {
                Key       = Guid.NewGuid(),
                Filename  = file.Filename,
                Status    = UploadStatus.Pending,
                Timestamp = DateTimeOffset.UtcNow,
                Errors    = new string[0]
            };
            await historyStore.Create(history, file.Data);

            backgroundJobClient.Enqueue <FileJob>(job => job.ProcessFile(history.Key));

            return(OperationResult <UploadHistory> .Success(history));
        }