public JobInfo StartNew(JobSpecification spec) { var info = new JobInfo(spec); info = this._jobs.GetOrAdd(info.Id, id => info); //Try create a simulator to see if the spec contains any errors, to report them in the creating thread spec.CreateSimulator(); var batchSize = (int)Math.Floor(info.Specification.VisitorCount / (double)this.Threads); for (var i = 0; i < this.Threads; i++) { var count = i == this.Threads - 1 ? info.Specification.VisitorCount - i * batchSize : batchSize; if (count > 0) { var segment = new JobSegment(info) { TargetVisitors = count }; info.Segments.Add(segment); this.StartJob(info, segment); if (i == 0) { // Fix for concurrent creating of Contacts. Thread.Sleep(this.WarmUpInterval); } } } return(info); }
protected void Process(JobSegment job) { try { if (job.JobStatus <= JobStatus.Paused) { job.Started = DateTime.Now; if (job.JobStatus == JobStatus.Pending) { job.JobStatus = JobStatus.Running; } Randomness.Seed((job.Id.GetHashCode() + DateTime.Now.Ticks).GetHashCode()); var simulator = job.Specification.CreateSimulator(); var visitorCount = simulator.GetVisitors(job.TargetVisitors); var partitioner = Partitioner.Create(visitorCount, EnumerablePartitionerOptions.NoBuffering); Parallel.ForEach(partitioner, ParallelOptions, (visitor, loopState) => { while (job.JobStatus == JobStatus.Paused) { Thread.Sleep(1000); } if (job.JobStatus > JobStatus.Paused) { loopState.Break(); } try { foreach (var visit in visitor.Commit()) { ++job.CompletedVisits; } } catch (Exception ex) { ++job.Exceptions; job.LastException = ex.ToString(); } ++job.CompletedVisitors; }); } job.Ended = DateTime.Now; job.JobStatus = job.CompletedVisitors < job.TargetVisitors ? JobStatus.Cancelled : JobStatus.Complete; } catch (Exception ex) { job.JobStatus = JobStatus.Failed; job.Ended = DateTime.Now; job.LastException = ex.ToString(); } }
protected void Process(JobSegment job) { try { if (job.JobStatus <= JobStatus.Paused) { job.Started = DateTime.Now; if (job.JobStatus == JobStatus.Pending) { job.JobStatus = JobStatus.Running; } Randomness.Seed((job.Id.GetHashCode() + DateTime.Now.Ticks).GetHashCode()); var sim = job.Specification.CreateSimulator(); foreach (var visitor in sim.NextVisitors(job.TargetVisitors, false)) { while (job.JobStatus == JobStatus.Paused) { Thread.Sleep(100); } if (job.JobStatus > JobStatus.Paused) { break; } try { foreach (var visit in visitor.Commit()) { ++job.CompletedVisits; } } catch (Exception ex) { ++job.Exceptions; job.LastException = ex.ToString(); } ++job.CompletedVisitors; } } job.Ended = DateTime.Now; job.JobStatus = job.CompletedVisitors < job.TargetVisitors ? JobStatus.Cancelled : JobStatus.Complete; } catch (Exception ex) { job.JobStatus = JobStatus.Failed; job.Ended = DateTime.Now; job.LastException = ex.ToString(); } }
public JobInfo StartNew(JobSpecification spec) { var info = new JobInfo(spec); info = _jobs.GetOrAdd(info.Id, id => info); //Try create a simulator to see if the spec contains any errors, to report them in the creating thread spec.CreateSimulator(); var segment = new JobSegment(info) { TargetVisitors = info.Specification.VisitorCount }; info.Segments.Add(segment); StartJob(info, segment); return(info); }
protected abstract void StartJob(JobInfo info, JobSegment job);