예제 #1
0
        public static void AddPlayer(string PlayerName, Command cmd, PriorityEnum priority, SWGoH.Enums.QueueEnum.QueueType type, DateTime nextrundate)
        {
            try
            {
                MongoDBRepo    mongo = new MongoDBRepo();
                IMongoDatabase db    = mongo.Connect();
                if (db != null)
                {
                    IMongoCollection <QueueDto> collection = db.GetCollection <QueueDto>("Queue");
                    if (collection != null)
                    {
                        FilterDefinition <QueueDto> filter = Builders <QueueDto> .Filter.Eq("Name", PlayerName);

                        UpdateDefinition <QueueDto> update = Builders <QueueDto> .Update.Set("Name", PlayerName)
                                                             .Set("InsertedDate", DateTime.UtcNow.ToString("o"))
                                                             .Set("ProcessingStartDate", "")
                                                             .Set("NextRunDate", nextrundate.ToString("o"))
                                                             .Set("Status", SWGoH.Enums.QueueEnum.QueueStatus.PendingProcess)
                                                             .Set("Priority", priority)
                                                             .Set("Type", type)
                                                             .Set("Command", cmd)
                                                             .Set("ComputerName", "");

                        var opts = new FindOneAndUpdateOptions <QueueDto>()
                        {
                            IsUpsert       = true,
                            ReturnDocument = ReturnDocument.After,
                            Sort           = Builders <QueueDto> .Sort.Descending(r => r.Priority).Ascending(r => r.NextRunDate)
                        };
                        QueueDto found = collection.FindOneAndUpdate <QueueDto>(filter, update, opts);
                        if (found != null)
                        {
                            SWGoH.Log.ConsoleMessage("Added Player To Queu:" + PlayerName);
                        }
                    }
                }
                else
                {
                    using (HttpClient client = new HttpClient())
                    {
                        JObject data = new JObject(
                            new JProperty("Name", PlayerName),
                            new JProperty("InsertedDate", DateTime.UtcNow.ToString("o")),
                            new JProperty("ProcessingStartDate", ""),
                            new JProperty("NextRunDate", nextrundate.ToString("o")),
                            new JProperty("Status", SWGoH.Enums.QueueEnum.QueueStatus.PendingProcess),
                            new JProperty("Priority", priority),
                            new JProperty("Type", type),
                            new JProperty("Command", cmd),
                            new JProperty("ComputerName", ""));

                        var httpContent = new StringContent(data.ToString(), Encoding.UTF8, "application/json");
                        var requestUri  = string.Format(SWGoH.MongoDBRepo.BuildApiUrl("Queue", "", "", "", ""));
                        HttpResponseMessage response = client.PostAsync(requestUri, httpContent).Result;
                        if (response.IsSuccessStatusCode)
                        {
                            SWGoH.Log.ConsoleMessage("Added Player To Queu:" + PlayerName);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                SWGoH.Log.ConsoleMessage("Error Adding Player To Queu:" + e.Message);
            }
        }
예제 #2
0
        public static QueueDto GetQueu()
        {
            bool onlymanual = SWGoH.Settings.appSettings.UpdateOnlyManual;

            try
            {
                MongoDBRepo    mongo = new MongoDBRepo();
                IMongoDatabase db    = mongo.Connect();
                if (db != null)
                {
                    SWGoH.Log.ConsoleMessageNotInFile("Getting from Queu!! (mongo)");
                    IMongoCollection <QueueDto> collection = db.GetCollection <QueueDto>("Queue");
                    if (collection != null)
                    {
                        //FilterDefinition<QueueDto> filter2 = Builders<QueueDto>.Filter.Eq("Priority", 2);
                        //UpdateDefinition<QueueDto> update2 = Builders<QueueDto>.Update.Set("Priority", 1);
                        //UpdateOptions opts2 = new UpdateOptions();
                        //opts2.IsUpsert = false;
                        //DeleteResult res2 = collection.DeleteMany(filter2);

                        FilterDefinition <QueueDto> filter = Builders <QueueDto> .Filter.Eq("Status", 0);

                        UpdateDefinition <QueueDto> update = Builders <QueueDto> .Update.Set("Status", 1).Set("ProcessingStartDate", DateTime.UtcNow.ToString("o")).Set("ComputerName", SWGoH.Settings.appSettings.ComputerName);

                        var opts = new FindOneAndUpdateOptions <QueueDto>()
                        {
                            IsUpsert       = false,
                            ReturnDocument = ReturnDocument.After,
                            Sort           = Builders <QueueDto> .Sort.Descending(r => r.Priority).Ascending(r => r.NextRunDate)
                        };
                        QueueDto found = collection.FindOneAndUpdate <QueueDto>(filter, update, opts);
                        if (found != null)
                        {
                            DateTime nextrun = DateTime.Parse(found.NextRunDate).ToUniversalTime();
                            if (DateTime.UtcNow < nextrun || (onlymanual && found.Priority != PriorityEnum.ManualLoad))
                            {
                                found.Status = QueueStatus.PendingProcess;

                                FilterDefinition <QueueDto> filter1 = Builders <QueueDto> .Filter.Eq("_id", found.Id);

                                UpdateDefinition <QueueDto> update1 = Builders <QueueDto> .Update.Set("Status", 0).Set("ComputerName", "");

                                UpdateOptions opts1 = new UpdateOptions();
                                opts1.IsUpsert = false;

                                UpdateResult res = collection.UpdateOne(filter1, update1, opts1);
                                return(null);
                            }
                        }
                        return(found);
                    }
                }
                else
                {
                    SWGoH.Log.ConsoleMessageNotInFile("Getting from Queu!!");
                    using (HttpClient client = new HttpClient())
                    {
                        string url = SWGoH.MongoDBRepo.BuildApiUrl("Queue", "&q={\"Status\":0}", "&s={\"Priority\":-1,\"NextRunDate\":1}", "&l=1", "");

                        string response = client.GetStringAsync(url).Result;
                        if (response != "" && response != "[  ]")
                        {
                            List <BsonDocument> document = BsonSerializer.Deserialize <List <BsonDocument> >(response);
                            QueueDto            result1  = BsonSerializer.Deserialize <QueueDto>(document.FirstOrDefault());
                            if (result1 != null)
                            {
                                //check nextrundate
                                DateTime nextrun = DateTime.Parse(result1.NextRunDate).ToUniversalTime();
                                if (DateTime.UtcNow > nextrun)
                                {
                                    //UPDATE with Status = 1
                                    JObject data = new JObject(
                                        new JProperty("Name", result1.Name),
                                        new JProperty("InsertedDate", result1.InsertedDate),
                                        new JProperty("ProcessingStartDate", DateTime.UtcNow.ToString("o")),
                                        new JProperty("NextRunDate", result1.NextRunDate),
                                        new JProperty("Status", SWGoH.Enums.QueueEnum.QueueStatus.Processing),
                                        new JProperty("Priority", result1.Priority),
                                        new JProperty("Type", result1.Type),
                                        new JProperty("Command", result1.Command),
                                        new JProperty("ComputerName", SWGoH.Settings.appSettings.ComputerName));

                                    var httpContent = new StringContent(data.ToString(), Encoding.UTF8, "application/json");
                                    var requestUri  = SWGoH.MongoDBRepo.BuildApiUrlFromId("Queue", result1.Id.ToString());
                                    using (HttpClient client1 = new HttpClient())
                                    {
                                        HttpResponseMessage updateresult = client1.PutAsync(requestUri, httpContent).Result;
                                    }
                                    SWGoH.Log.ConsoleMessage("Got from Queu Player " + result1.Name);
                                    return(result1);
                                }
                                else
                                {
                                    return(null);
                                }
                            }
                            return(result1);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                SWGoH.Log.ConsoleMessage("Error getting from Queu!!" + e.Message);
                return(null);
            }
            return(null);
        }