Пример #1
0
 static BlacklistCommands()
 {
     using (var uow = DbHandler.UnitOfWork())
     {
         BlacklistedItems = new ConcurrentHashSet<BlacklistItem>(uow.BotConfig.GetOrCreate().Blacklist);
     }
 }
Пример #2
0
 public static ConcurrentHashSet<string> FilteredWordsForServer(ulong guildId)
 {
     var words = new ConcurrentHashSet<string>();
     if(WordFilteringServers.Contains(guildId))
         ServerFilteredWords.TryGetValue(guildId, out words);
     return words;
 }
Пример #3
0
 public static ConcurrentHashSet<string> FilteredWordsForChannel(ulong channelId, ulong guildId)
 {
     ConcurrentHashSet<string> words = new ConcurrentHashSet<string>();
     if(WordFilteringChannels.Contains(channelId))
         ServerFilteredWords.TryGetValue(guildId, out words);
     return words;
 }
 public VoicePlusTextCommands()
 {
     using (var uow = DbHandler.UnitOfWork())
     {
         voicePlusTextCache = new ConcurrentHashSet<ulong>(NadekoBot.AllGuildConfigs.Where(g => g.VoicePlusTextEnabled).Select(g => g.GuildId));
     }
     NadekoBot.Client.UserVoiceStateUpdated += UserUpdatedEventHandler;
 }
Пример #5
0
 public TcpStreamBroadcaster(IHostSettings settings, IHostGlobalEvents globalEvents, ILogger logger)
 {
     this.globalEvents = globalEvents;
     this.logger = logger;
     localaddr = IPAddress.Parse(settings.TcpHostName);
     port = settings.TcpPort;
     activeClients = new ConcurrentHashSet<TcpStreamBroadcasterClientProcessor>();
 }
Пример #6
0
 static CustomReactions()
 {
     using (var uow = DbHandler.UnitOfWork())
     {
         var items = uow.CustomReactions.GetAll();
         GuildReactions = new ConcurrentDictionary<ulong, ConcurrentHashSet<CustomReaction>>(items.Where(g => g.GuildId != null && g.GuildId != 0).GroupBy(k => k.GuildId.Value).ToDictionary(g => g.Key, g => new ConcurrentHashSet<CustomReaction>(g)));
         GlobalReactions = new ConcurrentHashSet<CustomReaction>(items.Where(g => g.GuildId == null || g.GuildId == 0));
     }
 }
 public async Task Scsc(IUserMessage msg)
 {
     var channel = (ITextChannel)msg.Channel;
     var token = new NadekoRandom().Next();
     var set = new ConcurrentHashSet<ITextChannel>();
     if (Subscribers.TryAdd(token, set))
     {
         set.Add(channel);
         await ((IGuildUser)msg.Author).SendMessageAsync("This is your CSC token:" + token.ToString()).ConfigureAwait(false);
     }
 }
Пример #8
0
        public void Reload()
        {
            var arr = JArray.Parse(File.ReadAllText("data/questions.json"));

            foreach (var item in arr)
            {
                var tq = new TriviaQuestion(item["Question"].ToString().SanitizeMentions(), item["Answer"].ToString().SanitizeMentions(), item["Category"]?.ToString());
                pool.Add(tq);
            }
            var r = new NadekoRandom();
            pool = new ConcurrentHashSet<TriviaQuestion>(pool.OrderBy(x => r.Next()));
        }
            public PlantPickCommands()
            {
                _log = LogManager.GetCurrentClassLogger();
                NadekoBot.Client.MessageReceived += PotentialFlowerGeneration;
                rng = new NadekoRandom();

                using (var uow = DbHandler.UnitOfWork())
                {
                    var conf = uow.BotConfig.GetOrCreate();
                    var x =
                    generationChannels = new ConcurrentHashSet<ulong>(NadekoBot.AllGuildConfigs
                        .SelectMany(c => c.GenerateCurrencyChannelIds.Select(obj=>obj.ChannelId)));
                    chance = conf.CurrencyGenerationChance;
                    cooldown = conf.CurrencyGenerationCooldown;
                }
            }
Пример #10
0
        internal Compilation(int id, Action<Compilation> callback, CompilablePlugin[] plugins)
        {
            this.id = id;
            this.callback = callback;
            this.queuedPlugins = new ConcurrentHashSet<CompilablePlugin>(plugins);

            if (Current == null) Current = this;

            foreach (var plugin in plugins)
            {
                plugin.CompilerErrors = null;
                plugin.OnCompilationStarted();
            }

            includePath = Path.Combine(Interface.Oxide.PluginDirectory, "Include");
            extensionNames = Interface.Oxide.GetAllExtensions().Select(ext => ext.Name).ToArray();
            gameExtensionNamespace = Interface.Oxide.GetAllExtensions().SingleOrDefault(ext => ext.IsGameExtension)?.GetType().Namespace;
            newGameExtensionNamespace = gameExtensionNamespace != null && gameExtensionNamespace.Contains(".Game.");
        }
Пример #11
0
            static FilterCommands()
            {
                using (var uow = DbHandler.UnitOfWork())
                {
                    var guildConfigs = NadekoBot.AllGuildConfigs;

                    InviteFilteringServers = new ConcurrentHashSet<ulong>(guildConfigs.Where(gc => gc.FilterInvites).Select(gc => gc.GuildId));
                    InviteFilteringChannels = new ConcurrentHashSet<ulong>(guildConfigs.SelectMany(gc => gc.FilterInvitesChannelIds.Select(fci => fci.ChannelId)));

                    var dict = guildConfigs.ToDictionary(gc => gc.GuildId, gc => new ConcurrentHashSet<string>(gc.FilteredWords.Select(fw => fw.Word)));

                    ServerFilteredWords = new ConcurrentDictionary<ulong, ConcurrentHashSet<string>>(dict);

                    var serverFiltering = guildConfigs.Where(gc => gc.FilterWords);
                    WordFilteringServers = new ConcurrentHashSet<ulong>(serverFiltering.Select(gc => gc.GuildId));

                    WordFilteringChannels = new ConcurrentHashSet<ulong>(guildConfigs.SelectMany(gc => gc.FilterWordsChannelIds.Select(fwci => fwci.ChannelId)));

                }
            }
Пример #12
0
		private bool FarmHours(ConcurrentHashSet<Game> games) {
			if ((games == null) || (games.Count == 0)) {
				Logging.LogNullError(nameof(games), Bot.BotName);
				return false;
			}

			float maxHour = games.Max(game => game.HoursPlayed);
			if (maxHour < 0) {
				Logging.LogNullError(nameof(maxHour), Bot.BotName);
				return false;
			}

			if (maxHour >= 2) {
				Logging.LogGenericError("Received request for past-2h games!", Bot.BotName);
				return true;
			}

			Bot.ArchiHandler.PlayGames(games.Select(game => game.AppID), Bot.BotConfig.CustomGamePlayedWhileFarming);

			bool success = true;
			while (maxHour < 2) {
				Logging.LogGenericInfo("Still farming: " + string.Join(", ", games.Select(game => game.AppID)), Bot.BotName);

				DateTime startFarmingPeriod = DateTime.Now;
				if (FarmResetEvent.Wait(60 * 1000 * Program.GlobalConfig.FarmingDelay)) {
					FarmResetEvent.Reset();
					success = KeepFarming;
				}

				// Don't forget to update our GamesToFarm hours
				float timePlayed = (float) DateTime.Now.Subtract(startFarmingPeriod).TotalHours;
				foreach (Game game in games) {
					game.HoursPlayed += timePlayed;
				}

				if (!success) {
					break;
				}

				maxHour += timePlayed;
			}

			Logging.LogGenericInfo("Stopped farming: " + string.Join(", ", games.Select(game => game.AppID)), Bot.BotName);
			return success;
		}
        // Collections.synchronizedMap(new WeakHashMap<SegmentCoreReaders, bool?>());

        public virtual void RunTest(string testName)
        {
            Failed.Set(false);
            AddCount.Set(0);
            DelCount.Set(0);
            PackCount.Set(0);

            DateTime t0 = DateTime.UtcNow;

            Random random = new Random(Random().Next());
            LineFileDocs docs = new LineFileDocs(random, DefaultCodecSupportsDocValues());
            DirectoryInfo tempDir = CreateTempDir(testName);
            Dir = GetDirectory(NewMockFSDirectory(tempDir)); // some subclasses rely on this being MDW
            if (Dir is BaseDirectoryWrapper)
            {
                ((BaseDirectoryWrapper)Dir).CheckIndexOnClose = false; // don't double-checkIndex, we do it ourselves.
            }
            MockAnalyzer analyzer = new MockAnalyzer(Random());
            analyzer.MaxTokenLength = TestUtil.NextInt(Random(), 1, IndexWriter.MAX_TERM_LENGTH);
            IndexWriterConfig conf = NewIndexWriterConfig(TEST_VERSION_CURRENT, analyzer).SetInfoStream(new FailOnNonBulkMergesInfoStream());

            if (LuceneTestCase.TEST_NIGHTLY)
            {
                // newIWConfig makes smallish max seg size, which
                // results in tons and tons of segments for this test
                // when run nightly:
                MergePolicy mp = conf.MergePolicy;
                if (mp is TieredMergePolicy)
                {
                    ((TieredMergePolicy)mp).MaxMergedSegmentMB = 5000.0;
                }
                else if (mp is LogByteSizeMergePolicy)
                {
                    ((LogByteSizeMergePolicy)mp).MaxMergeMB = 1000.0;
                }
                else if (mp is LogMergePolicy)
                {
                    ((LogMergePolicy)mp).MaxMergeDocs = 100000;
                }
            }

            conf.SetMergedSegmentWarmer(new IndexReaderWarmerAnonymousInnerClassHelper(this));

            if (VERBOSE)
            {
                conf.InfoStream = new PrintStreamInfoStreamAnonymousInnerClassHelper(this, Console.Out);
            }
            Writer = new IndexWriter(Dir, conf);
            TestUtil.ReduceOpenFiles(Writer);

            TaskScheduler es = Random().NextBoolean() ? null : TaskScheduler.Default;

            DoAfterWriter(es);

            int NUM_INDEX_THREADS = TestUtil.NextInt(Random(), 2, 4);

            int RUN_TIME_SEC = LuceneTestCase.TEST_NIGHTLY ? 300 : RANDOM_MULTIPLIER;

            ISet<string> delIDs = new ConcurrentHashSet<string>(new HashSet<string>());
            ISet<string> delPackIDs = new ConcurrentHashSet<string>(new HashSet<string>());
            IList<SubDocs> allSubDocs = new SynchronizedCollection<SubDocs>();

            DateTime stopTime = DateTime.UtcNow.AddSeconds(RUN_TIME_SEC);

            ThreadClass[] indexThreads = LaunchIndexingThreads(docs, NUM_INDEX_THREADS, stopTime, delIDs, delPackIDs, allSubDocs);

            if (VERBOSE)
            {
                Console.WriteLine("TEST: DONE start " + NUM_INDEX_THREADS + " indexing threads [" + (DateTime.UtcNow - t0).TotalMilliseconds + " ms]");
            }

            // Let index build up a bit
            Thread.Sleep(100);

            DoSearching(es, stopTime);

            if (VERBOSE)
            {
                Console.WriteLine("TEST: all searching done [" + (DateTime.UtcNow - t0).TotalMilliseconds + " ms]");
            }

            for (int thread = 0; thread < indexThreads.Length; thread++)
            {
                indexThreads[thread].Join();
            }

            if (VERBOSE)
            {
                Console.WriteLine("TEST: done join indexing threads [" + (DateTime.UtcNow - t0).TotalMilliseconds + " ms]; addCount=" + AddCount + " delCount=" + DelCount);
            }

            IndexSearcher s = FinalSearcher;
            if (VERBOSE)
            {
                Console.WriteLine("TEST: finalSearcher=" + s);
            }

            Assert.IsFalse(Failed.Get());

            bool doFail = false;

            // Verify: make sure delIDs are in fact deleted:
            foreach (string id in delIDs)
            {
                TopDocs hits = s.Search(new TermQuery(new Term("docid", id)), 1);
                if (hits.TotalHits != 0)
                {
                    Console.WriteLine("doc id=" + id + " is supposed to be deleted, but got " + hits.TotalHits + " hits; first docID=" + hits.ScoreDocs[0].Doc);
                    doFail = true;
                }
            }

            // Verify: make sure delPackIDs are in fact deleted:
            foreach (string id in delPackIDs)
            {
                TopDocs hits = s.Search(new TermQuery(new Term("packID", id)), 1);
                if (hits.TotalHits != 0)
                {
                    Console.WriteLine("packID=" + id + " is supposed to be deleted, but got " + hits.TotalHits + " matches");
                    doFail = true;
                }
            }

            // Verify: make sure each group of sub-docs are still in docID order:
            foreach (SubDocs subDocs in allSubDocs.ToList())
            {
                TopDocs hits = s.Search(new TermQuery(new Term("packID", subDocs.PackID)), 20);
                if (!subDocs.Deleted)
                {
                    // We sort by relevance but the scores should be identical so sort falls back to by docID:
                    if (hits.TotalHits != subDocs.SubIDs.Count)
                    {
                        Console.WriteLine("packID=" + subDocs.PackID + ": expected " + subDocs.SubIDs.Count + " hits but got " + hits.TotalHits);
                        doFail = true;
                    }
                    else
                    {
                        int lastDocID = -1;
                        int startDocID = -1;
                        foreach (ScoreDoc scoreDoc in hits.ScoreDocs)
                        {
                            int docID = scoreDoc.Doc;
                            if (lastDocID != -1)
                            {
                                Assert.AreEqual(1 + lastDocID, docID);
                            }
                            else
                            {
                                startDocID = docID;
                            }
                            lastDocID = docID;
                            Document doc = s.Doc(docID);
                            Assert.AreEqual(subDocs.PackID, doc.Get("packID"));
                        }

                        lastDocID = startDocID - 1;
                        foreach (string subID in subDocs.SubIDs)
                        {
                            hits = s.Search(new TermQuery(new Term("docid", subID)), 1);
                            Assert.AreEqual(1, hits.TotalHits);
                            int docID = hits.ScoreDocs[0].Doc;
                            if (lastDocID != -1)
                            {
                                Assert.AreEqual(1 + lastDocID, docID);
                            }
                            lastDocID = docID;
                        }
                    }
                }
                else
                {
                    // Pack was deleted -- make sure its docs are
                    // deleted.  We can't verify packID is deleted
                    // because we can re-use packID for update:
                    foreach (string subID in subDocs.SubIDs)
                    {
                        Assert.AreEqual(0, s.Search(new TermQuery(new Term("docid", subID)), 1).TotalHits);
                    }
                }
            }

            // Verify: make sure all not-deleted docs are in fact
            // not deleted:
            int endID = Convert.ToInt32(docs.NextDoc().Get("docid"));
            docs.Dispose();

            for (int id = 0; id < endID; id++)
            {
                string stringID = "" + id;
                if (!delIDs.Contains(stringID))
                {
                    TopDocs hits = s.Search(new TermQuery(new Term("docid", stringID)), 1);
                    if (hits.TotalHits != 1)
                    {
                        Console.WriteLine("doc id=" + stringID + " is not supposed to be deleted, but got hitCount=" + hits.TotalHits + "; delIDs=" + string.Join(",",  delIDs.ToArray()));
                        doFail = true;
                    }
                }
            }
            Assert.IsFalse(doFail);

            Assert.AreEqual(AddCount.Get() - DelCount.Get(), s.IndexReader.NumDocs, "index=" + Writer.SegString() + " addCount=" + AddCount + " delCount=" + DelCount);
            ReleaseSearcher(s);

            Writer.Commit();

            Assert.AreEqual(AddCount.Get() - DelCount.Get(), Writer.NumDocs(), "index=" + Writer.SegString() + " addCount=" + AddCount + " delCount=" + DelCount);

            DoClose();
            Writer.Dispose(false);

            // Cannot shutdown until after writer is closed because
            // writer has merged segment warmer that uses IS to run
            // searches, and that IS may be using this es!
            /*if (es != null)
            {
              es.shutdown();
              es.awaitTermination(1, TimeUnit.SECONDS);
            }*/

            TestUtil.CheckIndex(Dir);
            Dir.Dispose();
            System.IO.Directory.Delete(tempDir.FullName, true);

            if (VERBOSE)
            {
                Console.WriteLine("TEST: done [" + (DateTime.UtcNow - t0).TotalMilliseconds + " ms]");
            }
        }
Пример #14
0
            private void MigrateServerSpecificConfigs0_9(IUnitOfWork uow)
            {
                const string specificConfigsPath = "data/ServerSpecificConfigs.json";

                if (!File.Exists(specificConfigsPath))
                {
                    _log.Warn($"No data from {specificConfigsPath} will be migrated.");
                    return;
                }

                var configs = new ConcurrentDictionary<ulong, ServerSpecificConfig>();
                try
                {
                    configs = JsonConvert
                        .DeserializeObject<ConcurrentDictionary<ulong, ServerSpecificConfig>>(
                            File.ReadAllText(specificConfigsPath), new JsonSerializerSettings()
                            {
                                Error = (s, e) =>
                                {
                                    if (e.ErrorContext.Member.ToString() == "GenerateCurrencyChannels")
                                    {
                                        e.ErrorContext.Handled = true;
                                    }
                                }
                            });
                }
                catch (Exception ex)
                {
                    _log.Warn(ex, "ServerSpecificConfig deserialization failed");
                    return;
                }
                var i = 0;
                var selfAssRoles = new ConcurrentHashSet<SelfAssignedRole>();
                configs
                    .Select(p => new { data = p.Value, gconfig = uow.GuildConfigs.For(p.Key) })
                    .AsParallel()
                    .ForAll(config =>
                    {
                        try
                        {
                            var guildConfig = config.gconfig;
                            var data = config.data;

                            guildConfig.AutoAssignRoleId = data.AutoAssignedRole;
                            guildConfig.DeleteMessageOnCommand = data.AutoDeleteMessagesOnCommand;
                            guildConfig.DefaultMusicVolume = data.DefaultMusicVolume;
                            guildConfig.ExclusiveSelfAssignedRoles = data.ExclusiveSelfAssignedRoles;
                            guildConfig.GenerateCurrencyChannelIds = new HashSet<GCChannelId>(data.GenerateCurrencyChannels.Select(gc => new GCChannelId() { ChannelId = gc.Key }));
                            selfAssRoles.AddRange(data.ListOfSelfAssignableRoles.Select(r => new SelfAssignedRole() { GuildId = guildConfig.GuildId, RoleId = r }).ToArray());
                            var logSetting = guildConfig.LogSetting;
                            guildConfig.LogSetting.IsLogging = data.LogChannel != null;
                            guildConfig.LogSetting.ChannelId = data.LogChannel ?? 0;
                            guildConfig.LogSetting.IgnoredChannels = new HashSet<IgnoredLogChannel>(data.LogserverIgnoreChannels.Select(id => new IgnoredLogChannel() { ChannelId = id }));

                            guildConfig.LogSetting.LogUserPresence = data.LogPresenceChannel != null;
                            guildConfig.LogSetting.UserPresenceChannelId = data.LogPresenceChannel ?? 0;


                            guildConfig.FollowedStreams = new HashSet<FollowedStream>(data.ObservingStreams.Select(x =>
                            {
                                FollowedStream.FollowedStreamType type = FollowedStream.FollowedStreamType.Twitch;
                                switch (x.Type)
                                {
                                    case StreamNotificationConfig0_9.StreamType.Twitch:
                                        type = FollowedStream.FollowedStreamType.Twitch;
                                        break;
                                    case StreamNotificationConfig0_9.StreamType.Beam:
                                        type = FollowedStream.FollowedStreamType.Beam;
                                        break;
                                    case StreamNotificationConfig0_9.StreamType.Hitbox:
                                        type = FollowedStream.FollowedStreamType.Hitbox;
                                        break;
                                    default:
                                        break;
                                }

                                return new FollowedStream()
                                {
                                    ChannelId = x.ChannelId,
                                    GuildId = guildConfig.GuildId,
                                    Username = x.Username.ToLowerInvariant(),
                                    Type = type
                                };
                            }));
                            guildConfig.VoicePlusTextEnabled = data.VoicePlusTextEnabled;
                            _log.Info("Migrating SpecificConfig for {0} done ({1})", guildConfig.GuildId, ++i);
                        }
                        catch (Exception ex)
                        {
                            _log.Error(ex);
                        }
                    });
                uow.SelfAssignedRoles.AddRange(selfAssRoles.ToArray());
                try { File.Move("data/ServerSpecificConfigs.json", "data/DELETE_ME_ServerSpecificCOnfigs.json"); } catch { }
            }
            public async Task Lsar(IUserMessage umsg)
            {
                var channel = (ITextChannel)umsg.Channel;

                var toRemove = new ConcurrentHashSet<SelfAssignedRole>();
                var removeMsg = new StringBuilder();
                var msg = new StringBuilder();
                using (var uow = DbHandler.UnitOfWork())
                {
                    var roleModels = uow.SelfAssignedRoles.GetFromGuild(channel.Guild.Id);
                    msg.AppendLine($"ℹ️ There are `{roleModels.Count()}` self assignable roles:");
                    
                    foreach (var roleModel in roleModels)
                    {
                        var role = channel.Guild.Roles.FirstOrDefault(r => r.Id == roleModel.RoleId);
                        if (role == null)
                        {
                            uow.SelfAssignedRoles.Remove(roleModel);
                        }
                        else
                        {
                            msg.Append($"**{role.Name}**, ");
                        }
                    }
                    foreach (var role in toRemove)
                    {
                        removeMsg.AppendLine($"`{role.RoleId} not found. Cleaned up.`");
                    }
                    await uow.CompleteAsync();
                }
                await channel.SendMessageAsync(msg.ToString() + "\n\n" + removeMsg.ToString()).ConfigureAwait(false);
            }
Пример #16
0
        /// <summary>
        ///     Remove nodes that are to be omitted due to an #if !SharpNative or the else of an #if SharpNative
        /// </summary>
        /// <param name="tree"></param>
        /// <returns></returns>
        public static IEnumerable<SyntaxNode> DoNotWrite(SyntaxTree tree)
        {
            var triviaProcessed = new ConcurrentHashSet<SyntaxTrivia>();

            var skipCount = 0;
            //set to 1 if we encounter a #if !SharpNative directive (while it's 0).  Incremented for each #if that's started inside of that, and decremented for each #endif
            var elseCount = 0;
            //set to 1 if we encounter an #if SharpNative directive (while it's 0).  Incremented for each #if that's started inside of that, and decremented for each #endif

            var ret = new List<SyntaxNode>();

            Action<SyntaxNodeOrToken> recurse = null;
            recurse = node =>
            {
                Action<SyntaxTrivia> doTrivia = trivia =>
                {
                    if (!triviaProcessed.Add(trivia))
                        return;

                    if (trivia.RawKind == (decimal)SyntaxKind.EndIfDirectiveTrivia)
                    {
                        if (skipCount > 0)
                            skipCount--;
                        if (elseCount > 0)
                            elseCount--;
                    }
                    else if (trivia.RawKind == (decimal)SyntaxKind.IfDirectiveTrivia)
                    {
                        if (skipCount > 0)
                            skipCount++;
                        if (elseCount > 0)
                            elseCount++;

                        var cond = GetConditions(trivia, "#if ");

                        if (cond.Contains("!SharpNative") && skipCount == 0)
                            skipCount = 1;
                        else if (cond.Contains("SharpNative") && elseCount == 0)
                            elseCount = 1;
                    }
                    else if (trivia.RawKind == (decimal)SyntaxKind.ElseDirectiveTrivia)
                    {
                        if (elseCount == 1)
                        {
                            skipCount = 1;
                            elseCount = 0;
                        }
                    }
                };

                foreach (var trivia in node.GetLeadingTrivia())
                    doTrivia(trivia);

                if (skipCount > 0 && node.IsNode)
                    ret.Add(node.AsNode());

                foreach (var child in node.ChildNodesAndTokens())
                    recurse(child);

                foreach (var trivia in node.GetTrailingTrivia())
                    doTrivia(trivia);
            };

            var root = tree.GetRoot();
            recurse(root);

            return ret;
        }
Пример #17
0
 private static void ListenQueue(string queueName, IJobActivator jobActivator, Logger logger, ConcurrentHashSet<InWorkMessage> inworkMessages, CancellationToken cancellationToken)
 {
     if (!MessageQueue.Exists(queueName))
     {
         throw new InvalidOperationException(string.Format("Очередь {0} отсутствует.", queueName));
     }
     var mq = new MessageQueue(queueName);
     while (true)
     {
         try
         {
             using (var messageQueueTransaction = new MessageQueueTransaction())
             {
                 messageQueueTransaction.Begin();
                 var mes = mq.Receive(messageQueueTransaction);
                 if (mes == null)
                 {
                     logger.Error("Получено пустое сообщение");
                     continue;
                 }
                 if (cancellationToken.IsCancellationRequested)
                 {
                     logger.Info("Операция отменена");
                     return;
                 }
                 mes.Formatter = new XmlMessageFormatter(new[] {typeof(MessageWrapper)});
                 var inWorkMessage = new InWorkMessage { Job = (MessageWrapper)mes.Body, QueueName = queueName, Label = mes.Label};
                 if(!inworkMessages.TryAdd(inWorkMessage))
                     continue;
                 if (inWorkMessage.Job.RetryCount==0)
                     logger.Info("Запущена задача {0}", inWorkMessage.Label);
                 else
                     logger.Info("Запущена задача {0}. Повторная попытка {1}", inWorkMessage.Label, inWorkMessage.Job.RetryCount);
                 var serializedjob = JobHelper.FromJson<SerializedJob>(inWorkMessage.Job.SerializedJob);
                 var job = serializedjob.Deserialize();
                 //Отправляем задачу в работу и добавляем обработчик который в случае ошибки или отмены задачи вернет сообщение в очередь.
                 //Если задача завершилась успешно, проставляем флаг об этом.
                 Task.Factory.StartNew(() => job.Perform(jobActivator, cancellationToken), cancellationToken)
                     .ContinueWith(t =>
                     {
                         if (t.Exception != null)
                         {
                             t.Exception.Handle(ex =>
                             {
                                 if (ex.GetType() == typeof (JobFailedException))
                                 {
                                     logger.Info("При выполнении задачи {0} возникла ошибка.", inWorkMessage.Label);
                                     logger.Error(ex.GetAllInnerExceptionMessagesAndTrace());
                                     Thread.Sleep(60000);
                                     ReturnMessageToQueue(inWorkMessage, inworkMessages, cancellationToken);
                                     return true;
                                 }
                                 logger.Fatal(ex.GetAllInnerExceptionMessagesAndTrace());
                                 ReturnMessageToQueue(inWorkMessage, inworkMessages, cancellationToken);
                                 return false;
                             });
                         }
                         else
                         {
                             logger.Info("Задача {0} завершилась успешно.", inWorkMessage.Label);
                             inWorkMessage.CompleteMessage();
                         }
                     }, TaskContinuationOptions.NotOnCanceled);
                 messageQueueTransaction.Commit();
             }
         }
         catch (Exception e)
         {
             logger.Fatal(e.GetAllInnerExceptionMessagesAndTrace());
             //inworkMessages.CompleteAdding();
             throw;
         }
     }
 }
Пример #18
0
 private static void ReturnMessageToQueue(InWorkMessage message, ConcurrentHashSet<InWorkMessage> inworkMessages, CancellationToken cancellationToken)
 {
     message.ReturnMessage();
     if (inworkMessages != null)
         inworkMessages.TryRemove(message);
     cancellationToken.ThrowIfCancellationRequested();
 }
Пример #19
0
        public void TestLockFreeColl2()
        {
            Action<ICollection<int>, int, int> collAdd = (coll, a, b) => Enumerable.Range(a,b).Reverse().ForEach(i =>  coll.Add(i));

            ConcurrentHashSet<int> testColl = new ConcurrentHashSet<int>();

            var jobs = new Action[] {
             () => { collAdd(testColl, 0, 500000);},
             () => { collAdd(testColl, 500000, 500000);},
             () => { collAdd(testColl, 1000000, 500000);},
             () => { collAdd(testColl, 1500000, 500000);},
             () => { collAdd(testColl, 2000000, 500000);},
             () => { collAdd(testColl, 2500000, 500000);},
             () => { collAdd(testColl, 3000000, 500000);},
             () => { collAdd(testColl, 3500000, 500000);}
            };

            //var jobs = new Action[] {
            // () => { collAdd(testColl, 0, 1000000);},
            // () => { collAdd(testColl, 1000000, 1000000);},
            // () => { collAdd(testColl, 2000000, 1000000);},
            // () => { collAdd(testColl, 3000000, 1000000);}
            //};

            //var jobs = new Action[] {
            // () => { collAdd(testColl, 0, 2000000);},
            // () => { collAdd(testColl, 2000000, 2000000);}
            //};

            //var jobs = new Action[] {
            // () => { collAdd(testColl, 0, 4000000);}
            //};

            TimeSpan serial = jobs.Execute();
        }
Пример #20
0
        public void TestLockFreeHashSet_Long()
        {
            ConcurrentHashSet<long> listTest = new ConcurrentHashSet<long>();

            listTest.Add(42);
            listTest.Add(22);
            listTest.Add(22);
            listTest.Add(64);
            listTest.Add(55);

            Assert.IsTrue(listTest.Count == 4);

            Assert.IsTrue(listTest.Contains(42));
            Assert.IsFalse(listTest.Contains(142));
            Assert.IsFalse(listTest.Contains(2));
            Assert.IsTrue(listTest.Contains(64));

            listTest.Remove(42);
            Assert.IsFalse(listTest.Contains(42));
            Assert.IsFalse(listTest.Remove(42));
            Assert.IsTrue(listTest.Count == 3);

            listTest.Add(42);
            listTest.Add(41);
            listTest.Add(0);

            Assert.IsTrue(listTest.Count == 6);

            listTest.Add(0);
            Assert.IsTrue(listTest.Count == 6);


            Assert.IsTrue(listTest.Contains(41));
            Assert.IsTrue(listTest.Contains(0));

            Assert.IsTrue(listTest.Remove(0));

            Assert.IsFalse(listTest.Contains(0));


            Assert.IsTrue(listTest.Remove(22));
            Assert.IsTrue(listTest.Remove(55));

            listTest.Add(1212);
            listTest.Add(323);
            listTest.Add(7567);
            listTest.Add(567);

            Assert.IsTrue(listTest.Count == 7);

        }
Пример #21
0
        public void TestLockFreeHashSet_Stream()
        {
            ICollection<Stream> listTest = new ConcurrentHashSet<Stream>();

            MemoryStream ms1 = new MemoryStream();
            MemoryStream ms2 = new MemoryStream();

            try
            {
                Enumerable.Range(0, 5000)
                    .Convert(i => new MemoryStream())
                    .InsertAtPosition(ms1, 455)
                    .ForEach(s => listTest.Add(s));
            }
            catch (Exception ex)
            {
                throw ex;
            }
            Assert.IsTrue(listTest.Count == 5001);
            Assert.IsTrue(listTest.Contains(ms1));
            Assert.IsFalse(listTest.Contains(ms2));
        }
Пример #22
0
        public void TestLockFreeHashSet_Guid()
        {
            ICollection<Guid> listTest = new ConcurrentHashSet<Guid>();

            Enumerable.Range(0, 50000)
                .ForEach(e => listTest.Add(Guid.NewGuid()));

        }
Пример #23
0
        static void Main(string[] args)
        {
            int startRaw = Environment.TickCount;
            HashSet<int> testRaw = new HashSet<int>();
            for (int i = 0; i < 4000000; i++)
                testRaw.Add(i);
            int timeRaw = Environment.TickCount - startRaw;
            for (int i = 0; i < 4000000; i++)
                testRaw.Contains(i);
            int timeRaw2 = Environment.TickCount - startRaw;

            Console.WriteLine(timeRaw.ToString());
            Console.WriteLine(timeRaw2.ToString());

            //Action<int, int> fnOutput = (total, start) =>
            //    Enumerable.Range(start, total).ForEach(i => Console.WriteLine(i));

            //for (int i = 0; i < 40; i++)
            //    Console.WriteLine(i);

            ////fnOutput(40, 0);

            //Action<int> fnOutput40 = fnOutput.Curry()(40);

            //fnOutput40(0);
            //fnOutput40(30);

            //ICollection<int> coll = new HashSet<int>(); 
            //int[] data = { 42, 46, 77, 345, -12 }; 
            //foreach (var local in data)
            //    coll.Add(local);

            //int test5 = TestFunctions.fnThunk(new HashSet<int>(), 1);
            //Console.Write("None\t");
            //Console.WriteLine(test5);

            //var test6 = TestFunctions.CalcP<LockFreeList<int>>();
            //Console.Write("LFLS\t");
            //Console.WriteLine(test6);

            //var test1 = TestFunctions.CalcP<CourseGrainedICollectionWrapper<int>, HashSet<int>>();
            //Console.Write("Lock\t");
            //Console.WriteLine(test1);
            //var test2 = TestFunctions.CalcP<ReadWriteICollectionWrapper<int>, HashSet<int>>();
            //Console.Write("RWLS\t");
            //Console.WriteLine(test2);
            //var test3 = TestFunctions.CalcP<InterlockedICollectionWrapper<int>, HashSet<int>>();
            //Console.Write("Intr\t");
            //Console.WriteLine(test3);

            CollectionWrapperCourseGrained<int> wrp = new CollectionWrapperCourseGrained<int>();
            wrp.Collection = new HashSet<int>();
            var resultn = TestFunctions.fnTest4MAdd(wrp, 4);
            Console.WriteLine(resultn.ToString());

            var resultnc = TestFunctions.fnTest4MContains(wrp, 4);
            Console.WriteLine(resultnc.ToString());

            var resultnr = TestFunctions.fnTest4MRemove(wrp, 4);
            Console.WriteLine(resultnr.ToString());

            int start = Environment.TickCount;

            //LockFreeList<int> list = new LockFreeList<int>();
            //ConcurrentHashSet<int> list = new ConcurrentHashSet<int>(4100000,true);
            ConcurrentHashSet<int> list = new ConcurrentHashSet<int>(4100000, true);

            var result2 = TestFunctions.fnTest4MAdd(list, 4);
            Console.Write("INSERTS = ");
            Console.WriteLine(result2.ToString());

            Console.Write("TICKS = ");
            Console.WriteLine(Environment.TickCount - start);

#if (DEBUG)
            Console.WriteLine(list.DebugDump);
            list.DebugReset();

            //list.DebugDataValidate();

            bool success = list.Contains(1000000);
#endif
            start = Environment.TickCount;

            var resultnc2 = TestFunctions.fnTest4MContains(list, 4);
            Console.Write("CONTAIN = ");
            Console.WriteLine(resultnc2.ToString());

            Console.Write("TICKS = ");
            Console.WriteLine(Environment.TickCount - start);

#if (DEBUG)
            Console.WriteLine(list.DebugDump);
#endif
            //start = Environment.TickCount;

            //var resultnr2 = TestFunctions.fnTest4MRemove(list, 4);
            //Console.Write("REMOVE = ");
            //Console.WriteLine(resultnr2.ToString());

            //Console.Write("TICKS = ");
            //Console.WriteLine(Environment.TickCount - start);

            //start = Environment.TickCount;

            list.Clear();
            Console.Write("CLEAR = ");
            Console.WriteLine(Environment.TickCount - start);

#if (DEBUG)
            //Console.WriteLine(list.DebugEmpty);
#endif
            start = Environment.TickCount;

            var result2b = TestFunctions.fnTest4MAdd(list, 4);
            Console.Write("INSERTS = ");
            Console.WriteLine(result2b.ToString());

            Console.Write("TICKS = ");
            Console.WriteLine(Environment.TickCount - start);
#if (DEBUG)
            Console.WriteLine(list.DebugDump);
#endif
            Console.ReadLine();
        }
Пример #24
0
		private bool FarmHours(float maxHour, ConcurrentHashSet<uint> appIDs) {
			if ((maxHour < 0) || (appIDs == null) || (appIDs.Count == 0)) {
				Logging.LogNullError(nameof(maxHour) + " || " + nameof(appIDs) + " || " + nameof(appIDs.Count), Bot.BotName);
				return false;
			}

			Bot.ArchiHandler.PlayGames(appIDs);

			bool success = true;
			while (maxHour < 2) {
				Logging.LogGenericInfo("Still farming: " + string.Join(", ", appIDs), Bot.BotName);

				DateTime startFarmingPeriod = DateTime.Now;
				if (FarmResetEvent.Wait(60 * 1000 * Program.GlobalConfig.FarmingDelay)) {
					FarmResetEvent.Reset();
					success = KeepFarming;
				}

				// Don't forget to update our GamesToFarm hours
				float timePlayed = (float) DateTime.Now.Subtract(startFarmingPeriod).TotalHours;
				foreach (uint appID in appIDs) {
					GamesToFarm[appID] += timePlayed;
				}

				if (!success) {
					break;
				}

				maxHour += timePlayed;
			}

			Logging.LogGenericInfo("Stopped farming: " + string.Join(", ", appIDs), Bot.BotName);
			return success;
		}
Пример #25
0
 public StoreInfo()
 {
     Name = "store";
     Tables = new ConcurrentHashSet<string>();
 }
Пример #26
0
 public StoreInfo()
 {
     _name = "store";
     _tableNames = new ConcurrentHashSet<string>();
 }
Пример #27
0
        public FileSystemCallbacks(
            GVFSContext context,
            GVFSGitObjects gitObjects,
            RepoMetadata repoMetadata,
            BlobSizes blobSizes,
            GitIndexProjection gitIndexProjection,
            BackgroundFileSystemTaskRunner backgroundFileSystemTaskRunner,
            FileSystemVirtualizer fileSystemVirtualizer,
            IPlaceholderCollection placeholderDatabase,
            ISparseCollection sparseCollection,
            GitStatusCache gitStatusCache = null)
        {
            this.logsHeadFileProperties = null;

            this.context = context;
            this.fileSystemVirtualizer = fileSystemVirtualizer;

            this.filePlaceHolderCreationCount   = new ConcurrentDictionary <string, PlaceHolderCreateCounter>(GVFSPlatform.Instance.Constants.PathComparer);
            this.folderPlaceHolderCreationCount = new ConcurrentDictionary <string, PlaceHolderCreateCounter>(GVFSPlatform.Instance.Constants.PathComparer);
            this.fileHydrationCount             = new ConcurrentDictionary <string, PlaceHolderCreateCounter>(GVFSPlatform.Instance.Constants.PathComparer);
            this.newlyCreatedFileAndFolderPaths = new ConcurrentHashSet <string>(GVFSPlatform.Instance.Constants.PathComparer);

            string error;

            if (!ModifiedPathsDatabase.TryLoadOrCreate(
                    this.context.Tracer,
                    Path.Combine(this.context.Enlistment.DotGVFSRoot, GVFSConstants.DotGVFS.Databases.ModifiedPaths),
                    this.context.FileSystem,
                    out this.modifiedPaths,
                    out error))
            {
                throw new InvalidRepoException(error);
            }

            this.BlobSizes = blobSizes ?? new BlobSizes(context.Enlistment.BlobSizesRoot, context.FileSystem, context.Tracer);
            this.BlobSizes.Initialize();

            this.placeholderDatabase = placeholderDatabase;
            this.GitIndexProjection  = gitIndexProjection ?? new GitIndexProjection(
                context,
                gitObjects,
                this.BlobSizes,
                repoMetadata,
                fileSystemVirtualizer,
                this.placeholderDatabase,
                sparseCollection,
                this.modifiedPaths);

            if (backgroundFileSystemTaskRunner != null)
            {
                this.backgroundFileSystemTaskRunner = backgroundFileSystemTaskRunner;
                this.backgroundFileSystemTaskRunner.SetCallbacks(
                    this.PreBackgroundOperation,
                    this.ExecuteBackgroundOperation,
                    this.PostBackgroundOperation);
            }
            else
            {
                this.backgroundFileSystemTaskRunner = new BackgroundFileSystemTaskRunner(
                    this.context,
                    this.PreBackgroundOperation,
                    this.ExecuteBackgroundOperation,
                    this.PostBackgroundOperation,
                    Path.Combine(context.Enlistment.DotGVFSRoot, GVFSConstants.DotGVFS.Databases.BackgroundFileSystemTasks));
            }

            this.enableGitStatusCache = gitStatusCache != null;

            // If the status cache is not enabled, create a dummy GitStatusCache that will never be initialized
            // This lets us from having to add null checks to callsites into GitStatusCache.
            this.gitStatusCache = gitStatusCache ?? new GitStatusCache(context, TimeSpan.Zero);

            this.logsHeadPath = Path.Combine(this.context.Enlistment.WorkingDirectoryBackingRoot, GVFSConstants.DotGit.Logs.Head);

            EventMetadata metadata = new EventMetadata();

            metadata.Add("placeholders.Count", this.placeholderDatabase.GetCount());
            metadata.Add("background.Count", this.backgroundFileSystemTaskRunner.Count);
            metadata.Add(TracingConstants.MessageKey.InfoMessage, $"{nameof(FileSystemCallbacks)} created");
            this.context.Tracer.RelatedEvent(EventLevel.Informational, $"{nameof(FileSystemCallbacks)}_Constructor", metadata);
        }