private async Task TaskDispatcher(System.Threading.CancellationToken token) { try { //we need to distribute work from the queue while (true) { while (taskQueue.Count > 0 && !token.IsCancellationRequested) { //get number of available tasks (this can only increase from another thread, so should be fine) int taskQueueSize = taskQueue.Count; //get first available worker GolemWorker worker = null; if (workerPool.TryDequeue(out worker)) { System.Threading.Interlocked.Increment(ref runningWorkers); Logger.LogMessage("Worker " + worker.Peer.NodeId + " started"); worker.ClearTasks(); List <string> compilersUsed = new List <string>(); //get the number of tasks to process int taskCount = Math.Min(worker.TaskCapacity, taskQueueSize); for (int i = 0; i < taskCount; ++i) { CompilationTask task = null; if (taskQueue.TryDequeue(out task)) { if (!compilersUsed.Contains(task.Compiler)) { compilersUsed.Add(task.Compiler); } worker.AddTask(task); } } string hash = GolemCache.GetCompilerPackageHash(compilersUsed); DeploymentSpecImage specImg = new DeploymentSpecImage("SHA1:" + hash, "http://" + myIP + ":" + ServerPort + "/requestID/compiler/" + hash); //create deployment DeploymentSpec spec = new DeploymentSpec(EnvType.Hd, specImg, "Compiler", new List <string>() { }); worker.Dispatch(golemApi, spec, () => { workerPool.Enqueue(worker); Logger.LogMessage("Worker " + worker.Peer.NodeId + " finished"); System.Threading.Interlocked.Decrement(ref runningWorkers); }); } } await Task.Delay(1000, token); } } catch (TaskCanceledException) { } }
public void AddTask(CompilationTask task) { taskList.Add(task); }
public void AddTask(CompilationTask task) { taskQueue.Enqueue(task); }