public IJobHandle f_createNew(IJob job) { IJobHandle handle = null; JOB_TYPE type = job.Type; switch (type) { case JOB_TYPE.NONE: case JOB_TYPE.REQUEST_URL: // factory IJobFactory fac; if (this.JobFactories.ContainsKey(type)) { fac = this.JobFactories[type]; } else { fac = new JobFactory(type); this.JobFactories.Add(type, fac); } handle = fac.f_createNew(job); break; default: // singleton if (!this.JobSingletons.ContainsKey(type)) { handle = new JobHandle(job); this.JobSingletons.Add(type, handle); } break; } return(handle); }
public JobMonitor() { var m = new JobMessage(this); this.MessageContext = m; this.MessageHandle = f_createNew(m); //f_createNew(new JobLink(this)); }
public JobMonitor() { var m = new JobMessage(this); this.MessageContext = m; this.MessageHandle = f_createNew(m); this.f_createNew(new JobFileHttp(this)); }
private static void AddJob(IJobHandle jobHandle) { jobsLock.EnterMoveLock(); //------------EnterLock JobsQueue.Enqueue(jobHandle); jobsLock.ExitMoveLock(); //------------ExitLock jobHandle.JobChange += OnJobChange; queueNonEmpty.Set(); }
public void f_runLoop(IJobHandle handle) { // Create the token source. //CancellationTokenSource cts = new CancellationTokenSource(); /* 4: stop */ if (this.Status == 4) { f_sleepAfterLoop(handle); } /* 1: init */ if (this.Status == 1) { System.Tracer.WriteLine("J{0}_{1} JobBaseUrl -> INIT", this.Id, this.Type); this.Handle = handle; this.f_INIT(); this.Status = 2; /* 2: running */ f_runLoop(handle); } /* 2: running */ if (this.Status == 2) { Message m = null; if (this.Handle.Factory != null) { m = this.Handle.Factory.f_getMessage(null); } // WAITING TO RECEIVED MESSAGE ... if (m == null) { f_sleepAfterLoop(handle); } else { //Tracer.WriteLine("J{0} PROCESSING ...", this.Id); // PROCESSING MESSAGE string htm = string.Empty, url = "http://localhost:3456/"; m.Input = url; var request = CreateWebRequest(url); IAsyncResult asyncRes = request.BeginGetResponse(null, request); /// CHECK TIMEOUT = Poll IAsyncResult.IsCompleted int counter = 1000, timeExpire = m.f_getTimeOutMillisecond(); timeout = false; while (asyncRes.IsCompleted == false) { Thread.Sleep(1000); // emulate that method is busy counter += 1000; if (timeExpire > 0 && timeExpire <= counter) { timeout = true; request.Abort(); break; } } if (timeout) { // TIMEOUT: REQUEST FAILD m.f_setErrorTimeOut(); } else { // REQUEST SUCCESS HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncRes); using (var stream = response.GetResponseStream()) using (var reader = new StreamReader(stream, Encoding.UTF8)) htm = reader.ReadToEnd(); response.Close(); m.Output.Ok = true; m.Output.SetData(htm); } if (this.Handle.Factory != null) { this.Handle.Factory.f_sendResponseEvent(m); } } } /// end function /////////////////////// }
void f_sleepAfterLoop(IJobHandle handle) { //Tracer.WriteLine("J{0} WAITING ...", this.Id); Thread.Sleep(JOB_CONST.JOB_TIMEOUT_RUN); f_runLoop(handle); }
public void f_runLoop(IJobHandle handle) { // Create the token source. CancellationTokenSource cts = new CancellationTokenSource(); /* 4: stop */ if (this.Status == 4) { f_sleepAfterLoop(handle); } /* 1: init */ if (this.Status == 1) { System.Tracer.WriteLine("J{0} BASE: INITED ...", this.Id); this.Handle = handle; this.f_init(); this.Status = 2; /* 2: running */ f_runLoop(handle); } /* 2: running */ if (this.Status == 2) { Message m = null; if (this.Handle.Factory == null) { m = this.Messages.Dequeue(null); } else { m = this.Handle.Factory.f_getMessage(null); } if (m == null) { //Tracer.WriteLine("J{0} BASE: WAITING ...", this.Id); // WAITING TO RECEIVED MESSAGE ... f_sleepAfterLoop(handle); } else { //Tracer.WriteLine("J{0} BASE: PROCESSING ...", this.Id); // PROCESSING MESSAGE ProcessMessage fun = this.f_processMessage; IAsyncResult asyncRes = fun.BeginInvoke(m, new AsyncCallback(f_callbackProcessMessage), null); ///// check timeout ... //IAsyncResult asyncRes = fun.BeginInvoke(m, null, null); //// Poll IAsyncResult.IsCompleted //while (asyncRes.IsCompleted == false) //{ // Console.WriteLine("Square Number still processing"); // Thread.Sleep(1000); // emulate that method is busy //} //Console.WriteLine("Square Number processing completed"); //Guid res = fun.EndInvoke(asyncRes); } } /// end function /////////////////////// }
void f_sleepAfterLoop(IJobHandle handle) { Thread.Sleep(JOB_CONST.JOB_TIMEOUT_RUN); f_runLoop(handle); }