Exemplo n.º 1
0
        public static PostInfo GetEventPostAndSetActive(this IFileStorage storage, string path)
        {
            PostInfo eventPostInfo = null;

            try {
                eventPostInfo = storage.GetObject <PostInfo>(path);
                if (eventPostInfo == null)
                {
                    return(null);
                }

                if (!storage.Exists(path + ".x") && !storage.SaveFile(path + ".x", String.Empty))
                {
                    return(null);
                }
            } catch (Exception ex) {
                Log.Error().Exception(ex).Message("Error retrieving event post data \"{0}\".", path).Write();
                return(null);
            }

            return(eventPostInfo);
        }
Exemplo n.º 2
0
        public static async Task <PostInfo> GetEventPostAndSetActiveAsync(this IFileStorage storage, string path, ILogger logger = null)
        {
            PostInfo eventPostInfo = null;

            try {
                eventPostInfo = await storage.GetObjectAsync <PostInfo>(path);

                if (eventPostInfo == null)
                {
                    return(null);
                }

                if (!await storage.ExistsAsync(path + ".x") && !await storage.SaveFileAsync(path + ".x", String.Empty))
                {
                    return(null);
                }
            } catch (Exception ex) {
                logger.Error(ex, "Error retrieving event post data \"{0}\".", path);
                return(null);
            }

            return(eventPostInfo);
        }
        public virtual async Task CanConcurrentlyManageFiles() {
            await ResetAsync();

            IFileStorage storage = GetStorage();
            if (storage == null)
                return;

            using (storage) {
                const string queueFolder = "q";
                var queueItems = new BlockingCollection<int>();

                var info = await storage.GetFileInfoAsync("nope");
                Assert.Null(info);

                await Run.InParallel(10, async i => {
                    var ev = new PostInfo {
                        ApiVersion = 2,
                        CharSet = "utf8",
                        ContentEncoding = "application/json",
                        Data = Encoding.UTF8.GetBytes("{}"),
                        IpAddress = "127.0.0.1",
                        MediaType = "gzip",
                        ProjectId = i.ToString(),
                        UserAgent = "test"
                    };

                    await storage.SaveObjectAsync(Path.Combine(queueFolder, i + ".json"), ev);
                    queueItems.Add(i);
                });
                
                Assert.Equal(10, (await storage.GetFileListAsync()).Count());

                await Run.InParallel(10, async i => {
                    string path = Path.Combine(queueFolder, queueItems.Random() + ".json");
                    var eventPost = await storage.GetEventPostAndSetActiveAsync(Path.Combine(queueFolder, RandomData.GetInt(0, 25) + ".json"));
                    if (eventPost == null)
                        return;

                    if (RandomData.GetBool()) {
                        await storage.CompleteEventPost(path, eventPost.ProjectId, DateTime.UtcNow, true);
                    } else
                        await storage.SetNotActiveAsync(path);
                });
            }
        }