public FindAvailableWorkers(int nodeCount, IStartJob startJob)
 {
     NodeCount = nodeCount;
     StartJob = startJob;
 }
 private void BecomeReady()
 {
     JobToStart = null;
     Become(Ready);
     Stash.UnstashAll();
 }
        private void Ready()
        {
            Receive<IStartJob>(start =>
            {
                _logger.Info("{0} : StartJob", start.Job.JobInfo.Id);

                JobToStart = start;
                if (!CurrentJobs.ContainsKey(start.Job))
                {
                    CurrentJobs.TryAdd(start.Job,
                        new JobWorkerInfo(start.Job)
                        {
                            JobStatusUpdate = new JobStatusUpdate(start.Job) { Status = JobStatus.New }
                        });

                    // Start Search
                    JobCoordinatorRouter.Ask<Routees>(new GetRoutees()).ContinueWith(tr =>
                    {
                        if (tr.IsCanceled || tr.IsFaulted)
                        {
                            _logger.Error(tr.Exception, "{0} : JobCoordinatorRouter was {1} ", start.Job.JobInfo.Id);
                            return new FindAvailableWorkers(0, start);
                        }

                        return new FindAvailableWorkers(tr.Result.Members.Count(), start);
                    }, TaskContinuationOptions.AttachedToParent & TaskContinuationOptions.ExecuteSynchronously).PipeTo(Self);
                    Become(SearchingForJob);
                }
                else
                {
                    JobWorkerInfo workerInfo;
                    CurrentJobs.TryGetValue(start.Job, out workerInfo);

                    if (workerInfo.JobWorkerRef != null)
                    {
                        // If we have a JobWorkerRef and the job has started or is running then subscribe and check status.
                        if (workerInfo.JobStatusUpdate.Status == JobStatus.Starting ||
                            workerInfo.JobStatusUpdate.Status == JobStatus.Running ||
                            workerInfo.JobStatusUpdate.Status == JobStatus.GetItemToProcess ||
                            workerInfo.JobStatusUpdate.Status == JobStatus.ProcessingItem)
                        {
                            workerInfo.JobWorkerRef.Tell(new SubscribeToJob(start.Job, start.Requestor), Self);
                            workerInfo.JobWorkerRef.Tell(new JobWorker.CheckJobStatus(), Self);
                        }

                        // If we have a JobWorkerRef and the status is WorkerFound... 
                        // The actor must still exists becuase we are watching it and if something is wrong with it then it would timeout and be terminated.
                        // Possible reason is the JobWorker did not recieve the IStartJob message.  
                        if (workerInfo.JobStatusUpdate.Status == JobStatus.WorkerFound)
                        {
                            workerInfo.JobWorkerRef.Tell(start.Job);
                        }
                    }
                    else
                    {
                        // Start Search
                        workerInfo.PreviousJobStatusUpdate.Insert(0, workerInfo.JobStatusUpdate);
                        workerInfo.JobStatusUpdate = new JobStatusUpdate(start.Job) { Status = JobStatus.New };

                        if (workerInfo.PreviousJobStatusUpdate.Count == 11)
                        {
                            workerInfo.PreviousJobStatusUpdate.Remove(workerInfo.PreviousJobStatusUpdate.Last());
                        }
                        
                        CurrentJobs.AddOrUpdate(start.Job, workerInfo, (key, oldvalue) => workerInfo);

                        JobCoordinatorRouter.Ask<Routees>(new GetRoutees()).ContinueWith(tr =>
                        {
                            if (tr.IsFaulted)
                            {
                                _logger.Error(tr.Exception, "{0} : JobCoordinatorRouter was Faulted ", start.Job.JobInfo.Id);
                                return new FindAvailableWorkers(0, start);
                            }
                            if (tr.IsCanceled)
                            {
                                _logger.Error(tr.Exception, "{0} : JobCoordinatorRouter was Canceled ", start.Job.JobInfo.Id);
                                return new FindAvailableWorkers(0, start);
                            }

                            return new FindAvailableWorkers(tr.Result.Members.Count(), start);
                        }, TaskContinuationOptions.AttachedToParent & TaskContinuationOptions.ExecuteSynchronously).PipeTo(Self);
                        Become(SearchingForJob);
                    }
                }
            });
            
            Receive<TellJobSubscribersTheStatus>(ic =>
            {
                HandleTellJobSubscribersTheStatus();
            });

            Receive<JobWorker.ReceivedJobStatus>(ic =>
            {
                HandleReceivedJobStatus(ic);
            });

            Receive<Terminated>(terminated =>
            {
                RemoveTerminatedReferences(terminated);
            });

            Receive<JobStatusUpdate>(ic =>
            {
                JobWorkerInfo workerInfo;
                CurrentJobs.TryGetValue(ic.Job, out workerInfo);
                if (workerInfo != null)
                {
                    workerInfo.JobStatusUpdate = ic;
                    CurrentJobs.AddOrUpdate(ic.Job, workerInfo, (key, oldvalue) => workerInfo);
                }

            });

            Receive<JobWorker.JobComplete>(ic =>
            {
                HandleJobComplete(ic);
            });

            Receive<ISubscribeToAllJobs>(ic =>
            {
                _logger.Info("{0} : Subscribing to all jobs", Sender);
                HandleSubscribingToJobs(ic.Requestor);
            });

            Receive<IUnSubscribeToAllJobs>(ic =>
            {
                _logger.Info("{0} : UnSubscribing to all jobs", Sender);
                HandleUnSubscribingToJobs(ic.Requestor);
            });

            ReceiveAny(task =>
            {
                _logger.Error(" [x] Oh Snap! JobMaster.Ready.ReceiveAny: \r\n{0}", task);
            });
        }
 private void BecomeReady()
 {
     JobToStart = null;
     Become(Ready);
     Stash.UnstashAll();
 }
 public FindAvailableWorkers(int nodeCount, IStartJob startJob)
 {
     NodeCount = nodeCount;
     StartJob  = startJob;
 }
        private void Ready()
        {
            Receive <IStartJob>(start =>
            {
                _logger.Info("{0} : StartJob", start.Job.JobInfo.Id);

                JobToStart = start;
                if (!CurrentJobs.ContainsKey(start.Job))
                {
                    CurrentJobs.TryAdd(start.Job,
                                       new JobWorkerInfo(start.Job)
                    {
                        JobStatusUpdate = new JobStatusUpdate(start.Job)
                        {
                            Status = JobStatus.New
                        }
                    });

                    // Start Search
                    JobCoordinatorRouter.Ask <Routees>(new GetRoutees()).ContinueWith(tr =>
                    {
                        if (tr.IsCanceled || tr.IsFaulted)
                        {
                            _logger.Error(tr.Exception, "{0} : JobCoordinatorRouter was {1} ", start.Job.JobInfo.Id);
                            return(new FindAvailableWorkers(0, start));
                        }

                        return(new FindAvailableWorkers(tr.Result.Members.Count(), start));
                    }, TaskContinuationOptions.AttachedToParent & TaskContinuationOptions.ExecuteSynchronously).PipeTo(Self);
                    Become(SearchingForJob);
                }
                else
                {
                    JobWorkerInfo workerInfo;
                    CurrentJobs.TryGetValue(start.Job, out workerInfo);

                    if (workerInfo.JobWorkerRef != null)
                    {
                        // If we have a JobWorkerRef and the job has started or is running then subscribe and check status.
                        if (workerInfo.JobStatusUpdate.Status == JobStatus.Starting ||
                            workerInfo.JobStatusUpdate.Status == JobStatus.Running ||
                            workerInfo.JobStatusUpdate.Status == JobStatus.GetItemToProcess ||
                            workerInfo.JobStatusUpdate.Status == JobStatus.ProcessingItem)
                        {
                            workerInfo.JobWorkerRef.Tell(new SubscribeToJob(start.Job, start.Requestor), Self);
                            workerInfo.JobWorkerRef.Tell(new JobWorker.CheckJobStatus(), Self);
                        }

                        // If we have a JobWorkerRef and the status is WorkerFound...
                        // The actor must still exists becuase we are watching it and if something is wrong with it then it would timeout and be terminated.
                        // Possible reason is the JobWorker did not recieve the IStartJob message.
                        if (workerInfo.JobStatusUpdate.Status == JobStatus.WorkerFound)
                        {
                            workerInfo.JobWorkerRef.Tell(start.Job);
                        }
                    }
                    else
                    {
                        // Start Search
                        workerInfo.PreviousJobStatusUpdate.Insert(0, workerInfo.JobStatusUpdate);
                        workerInfo.JobStatusUpdate = new JobStatusUpdate(start.Job)
                        {
                            Status = JobStatus.New
                        };

                        if (workerInfo.PreviousJobStatusUpdate.Count == 11)
                        {
                            workerInfo.PreviousJobStatusUpdate.Remove(workerInfo.PreviousJobStatusUpdate.Last());
                        }

                        CurrentJobs.AddOrUpdate(start.Job, workerInfo, (key, oldvalue) => workerInfo);

                        JobCoordinatorRouter.Ask <Routees>(new GetRoutees()).ContinueWith(tr =>
                        {
                            if (tr.IsFaulted)
                            {
                                _logger.Error(tr.Exception, "{0} : JobCoordinatorRouter was Faulted ", start.Job.JobInfo.Id);
                                return(new FindAvailableWorkers(0, start));
                            }
                            if (tr.IsCanceled)
                            {
                                _logger.Error(tr.Exception, "{0} : JobCoordinatorRouter was Canceled ", start.Job.JobInfo.Id);
                                return(new FindAvailableWorkers(0, start));
                            }

                            return(new FindAvailableWorkers(tr.Result.Members.Count(), start));
                        }, TaskContinuationOptions.AttachedToParent & TaskContinuationOptions.ExecuteSynchronously).PipeTo(Self);
                        Become(SearchingForJob);
                    }
                }
            });

            Receive <TellJobSubscribersTheStatus>(ic =>
            {
                HandleTellJobSubscribersTheStatus();
            });

            Receive <JobWorker.ReceivedJobStatus>(ic =>
            {
                HandleReceivedJobStatus(ic);
            });

            Receive <Terminated>(terminated =>
            {
                RemoveTerminatedReferences(terminated);
            });

            Receive <JobStatusUpdate>(ic =>
            {
                JobWorkerInfo workerInfo;
                CurrentJobs.TryGetValue(ic.Job, out workerInfo);
                if (workerInfo != null)
                {
                    workerInfo.JobStatusUpdate = ic;
                    CurrentJobs.AddOrUpdate(ic.Job, workerInfo, (key, oldvalue) => workerInfo);
                }
            });

            Receive <JobWorker.JobComplete>(ic =>
            {
                HandleJobComplete(ic);
            });

            Receive <ISubscribeToAllJobs>(ic =>
            {
                _logger.Info("{0} : Subscribing to all jobs", Sender);
                HandleSubscribingToJobs(ic.Requestor);
            });

            Receive <IUnSubscribeToAllJobs>(ic =>
            {
                _logger.Info("{0} : UnSubscribing to all jobs", Sender);
                HandleUnSubscribingToJobs(ic.Requestor);
            });

            ReceiveAny(task =>
            {
                _logger.Error(" [x] Oh Snap! JobMaster.Ready.ReceiveAny: \r\n{0}", task);
            });
        }
Example #7
0
 private static void HandleMessage(IStartJob msg)
 {
     Console.Out.WriteLine(String.Format("got request {0}!", msg.CorrelationId));
     Thread.Sleep(10000);
     Console.Out.WriteLine("done with message" + msg.CorrelationId);
 }