public ServerTask(TwitterAcc twitterRequest) { if (!twitterRequest.GetAuthObj(out twitterAuth)) { Log.Error("Auth failed"); } twitterName = twitterRequest.Name; }
private Queue <TwitterAcc> nonAddedRequests = new Queue <TwitterAcc>(); //Requests from the browser goes into here /// <summary> /// //The main function of the queue server /// </summary> public async void TaskQueue() { Queue <Task> taskQueue = new Queue <Task>(); //Queue of tasks that are not started yet List <Task> runningTasksList = new List <Task>(); //List of currently running tasks. while (true) //runs for as long as the server is up. { while (ThereIsNewTask()) // Adds all requests to the queue as tasks. { TwitterAcc input = nonAddedRequests.Peek(); Log.Debug("Task entered queue with ID " + input.RequestID); Task newTask = new Task(() => { ServerTask st = new ServerTask(input); st.Run(); }); taskQueue.Enqueue(newTask); nonAddedRequests.Dequeue(); } while (runningTasksList.Count < Constants.QUEUE_SERVER_TASK_LIMIT) //Starts tasks from the queue until there are none left or the limit is reached. { if (taskQueue.Count > 0) { runningTasksList.Add(taskQueue.Peek()); await RunTaskAsync(taskQueue.Dequeue()); } else { break; } } if (runningTasksList.Count > 0) // If any tasks are running, it removes the completed ones from the list. { List <Task> runningTasksListInstance = new List <Task>(); runningTasksListInstance.AddRange(runningTasksList); foreach (var task in runningTasksListInstance) { if (task.IsCompleted) { runningTasksList.Remove(task); } } } } }
//The method used by the server to add requests to the request queue. public bool AddTask(TwitterAcc tAcc) { bool wasSuccesful = true; try { //If the twitter account does not exist or if any of the two keys do not contain proper information, it can not be added. if (tAcc == null || (String.IsNullOrWhiteSpace(tAcc.Name) || String.IsNullOrWhiteSpace(tAcc.Token) || String.IsNullOrWhiteSpace(tAcc.Secret))) { return(false); } nonAddedRequests.Enqueue(tAcc); } catch (Exception) //It does not matter what the exception is. { wasSuccesful = false; } return(wasSuccesful); }