/// <summary> /// Submit a job to a job server for processing. The callback string is used as the /// task for the manager to hand off the job to a worker. /// </summary> /// <param name="callback"> /// A string containing the name of the operation to ask the manager to find a worker for /// </param> /// <param name="data"> /// A byte array containing the data to be worked on. This data is passed to the worker who can /// work on a job of the requested type. /// </param> /// <returns> /// A byte array containing the response from the worker that completed the task /// </returns> /// <example> /// <code> /// Client c = new Client("localhost"); /// byte[] data = new ASCIIEncoding().GetBytes("foo\nbar\nquiddle\n"); /// byte[] result = c.submitJob("wc", data); /// </code> /// </example> public byte[] submitJob(string callback, byte[] data) { try { Packet result = null; bool submitted = false; Connection c = null; string jobid = System.Guid.NewGuid().ToString(); SubmitJob p = new SubmitJob(callback, jobid, data, false); while (!submitted) { // Simple round-robin submission for now c = managers[connectionIndex++ % managers.Count]; c.sendPacket(p); Log.DebugFormat("Sent job request to {0}...", c); // We need to get back a JOB_CREATED packet result = c.getNextPacket(); // If we get back a JOB_CREATED packet, we can continue // otherwise try the next job manager if (result.Type == PacketType.JOB_CREATED) { submitted = true; Log.DebugFormat("Created job {0}", ((JobCreated)result).jobhandle); } } // This method handles synchronous requests, so we wait // until we get a work complete packet while (true) { result = c.getNextPacket(); if (result.Type == PacketType.WORK_COMPLETE) { WorkComplete wc = (WorkComplete)result; Log.DebugFormat("Completed job {0}", wc.jobhandle); return(wc.data); } } } catch (Exception e) { Log.DebugFormat("Error submitting job: {0}", e.ToString()); return(null); } }
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (Complete) { MainForm.Invoke((SimpleDelegate)(() => WorkComplete.Raise(this))); } else { MainForm.Invoke((SimpleDelegate)(() => WorkAborted.Raise(this))); } CloseBusyDisplay(); workTimer.Stop(); }
public void DoWork(int hours, WorkType type) { for (int i = 0; i < hours; i++) { if (this.WorkPerformed != null) { this.WorkPerformed.Invoke(i + 1, type); } } if (this.WorkComplete != null) { WorkComplete.Invoke(this, new WorkCompletedEventArgs(hours, type)); } }
public static void SubscribeToWorkReceived(Guid workerId) { Action<string, string> onWorkReceived = (channel, msg) => { var work = JsonSerializer.DeserializeFromString<Work>(msg); Console.WriteLine("[{0}] Performing work: {1}", DateTime.Now.ToShortTimeString(), work.Message); Thread.Sleep(10000); var workComplete = new WorkComplete { MessageId = Guid.NewGuid(), CorrelationId = work.MessageId }; Publish(Channels.WorkComplete, workComplete.ToString()); var workerAvailable = new WorkerAvailable { MessageId = Guid.NewGuid(), WorkerId = workerId }; Publish(Channels.WorkerAvailable, workerAvailable.ToString()); }; Task.Factory.StartNew(() => Subscribe(onWorkReceived, workerId.ToString())); }
protected virtual void OnWorkComplete(object sender, EventArgs e) { WorkComplete?.Invoke(sender, e); }
private void handleJobCompletion(Packet p) { WorkComplete wc = ((WorkComplete)p); queue.finishedJob(wc.jobhandle); }
/// <summary> /// Interprocess messege recived handler /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnIpcCommunicationMessageRecived(object sender, WorkResult e) { m_WorkCommandResults[e.Guid] = e; WorkComplete?.Invoke(this, new WorkCompleteEventArgs(e.Guid)); }