コード例 #1
0
        public string CreateJob(string jsondata)
        {
            Console.WriteLine("ManagerJobController: Creating job: json=" + jsondata);

            var jr = new JsonReader(jsondata);
            var jx = jr.Deserialize();
            var jo = jx as Dictionary<string, object>;
            if (jo == null)
                return "";

            var newjob = new ManagerJob();
            newjob.Controller = this;
            newjob.ID = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 10);

            int numworkers = 2;
            if (jo.ContainsKey("instances"))
                numworkers = int.Parse(jo["instances"].ToString());

            if (jo.ContainsKey("callback"))
                newjob.CallbackUrl = jo["callback"].ToString();

            if (jo.ContainsKey("jobtype"))
                newjob.JobType = jo["jobtype"].ToString();

            newjob.JobArgs = new DefaultJobArgs();
            if (jo.ContainsKey("jobargs"))
                Utilities.FillArgsFromJson(newjob.JobArgs, jo["jobargs"]);

            Console.WriteLine("ManagerJobController: Trying to get " + numworkers + " instances.");

            var idleworkerlist = ProvisionWorkers(numworkers);
            foreach (var workerurl in idleworkerlist)
            {
                // create job on workers

                Console.WriteLine("ManagerJobController: Telling worker at " + workerurl + " to create a job...");
                var winfo = GetWorkerByUrl(workerurl);
                winfo.RemoteJobId = "";
                var wc = new WebClient();
                var jobinfo = BuildCreateJobData(newjob);
                try
                {
                    winfo.RemoteJobId = wc.UploadString(Utilities.CombineURL(winfo.URL, "/createjob"), jobinfo);
                }
                catch (Exception z)
                {
                    Console.WriteLine(z);
                }
                Console.WriteLine("Created remote job id: " + winfo.RemoteJobId);
                if (winfo.RemoteJobId != "")
                    newjob.Workers.Add(winfo);
            }

            Console.WriteLine("ManagerJobController: Got " + newjob.Workers.Count + " workers.");

            //	newjob.CallbackUrl = jo["managerurl"];

            Console.WriteLine("ManagerJobController: Created job " + newjob.ID);//+ " with callback url " + newjob.CallbackUrl);

            m_jobs.Add(newjob);
            return newjob.ID;
        }
コード例 #2
0
 private string BuildCreateJobData(ManagerJob newjob)
 {
     var jobinfo = new StringBuilder();
     jobinfo.Append("{\"jobtype\":\"").Append(Utilities.EscapeJson(newjob.JobType)).Append("\",");
     var argjson = Utilities.BuildJsonFromArgs(newjob.JobArgs);
     if (!string.IsNullOrEmpty(argjson))
         jobinfo.Append("\"jobargs\":" + argjson + ",");
     var callbackurl = "http://" + _manager.Config.Hostname + ":" + _manager.Config.Port + "/job/" + newjob.ID + "/worker-callback";
     jobinfo.Append("\"callback\":\"").Append(Utilities.EscapeJson(callbackurl)).Append("\"}");
     return jobinfo.ToString();
 }