private void handleJobSubmitted(Packet p, JobPriority priority, bool background) { Guid jobid = Guid.NewGuid(); string jobhandle = String.Format("{0}:{1}", hostName, jobid); SubmitJob sj = (SubmitJob)p; var job = new Job() { TaskName = sj.taskname, JobHandle = jobhandle, Unique = sj.unique_id, When = sj.when, Priority = (int)priority, Data = sj.data, Dispatched = false, }; bool success = queue.storeJob(job, true); Console.WriteLine("{0} == {1}", job.JobHandle, job.Id); GearmanServer.Log.Info("Job was submitted!"); conn.sendPacket(new JobCreated(jobhandle)); if (sj.when <= DateTime.UtcNow && workers.ContainsKey(sj.taskname)) { foreach (Gearman.Connection c in workers[sj.taskname]) { c.sendPacket(new NoOp()); } } }
/// <summary> /// Add a task <--> delegate mapping to this worker. A task name is a short string that the worker registers /// with the manager, for example "reverse". The delegate is a callback that the worker calls when it receives a /// packet of work to do that has a task name matching the registered name. /// </summary> /// <param name="taskname"> /// A <see cref="System.String"/> that is the 'name' of the task being registered with the server /// </param> /// <param name="function"> /// A <see cref="taskdelegate"/> that is the actual callback function /// (must match the <see cref="taskdelegate"/> specification) /// </param> public void registerFunction(string taskname, taskdelegate function) { if (!methodMap.ContainsKey(taskname)) { c.sendPacket(new CanDo(taskname)); } methodMap[taskname] = function; }
/// <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); } }
/// <summary>Submit a job to the job server in the background, with a particular priority</summary> /// <example> /// <code> /// </code> /// </example> public string submitJobInBackground(string callback, byte[] data, JobPriority priority) { try { Connection c = null; string jobid = System.Guid.NewGuid().ToString(); SubmitJob p = new SubmitJob(callback, jobid, data, true, priority); Packet result; while (true) { // Simple round-robin submission for now c = managers[connectionIndex++ % managers.Count]; c.sendPacket(p); Log.DebugFormat("Sent background 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) { Log.DebugFormat("Created background job {0}, with priority {1}", ((JobCreated)result).jobhandle, priority.ToString()); return(((JobCreated)result).jobhandle); } } } catch (Exception e) { Log.DebugFormat("Error submitting job: {0}", e.ToString()); return(null); } }