Beispiel #1
0
        public JobPoolControllerActor(IActorRef commanderActor)
        {
            _commander = commanderActor;
            Receive <ProcessFileMessage>(msg => InitializeJobs(msg.FileName));
            Receive <ProcessJob>(job => HandleProcessJob());

            Receive <JobValidationSucceedMessage>(msg =>
            {
                _commander.Tell(new CanAcceptJobMessage(_currentJobMsg.Description, _currentJobMsg.ID));
            });

            Receive <JobValidationFailedMessage>(msg =>
            {
                ColorConsole.WriteLineRed("Invalid Task : {0} {1}", _currentJobMsg.ID, _currentJobMsg.Description);
            });

            Receive <AbleToAcceptJobMessage>(job =>
            {
                ColorConsole.WriteLineGreen("Commander {0} is able to accept job {1}. {2}", _commander.Path.Name, job.ID, job.Description);
                _commander.Tell(new BeginJobMessage(job.Description, job.ID));
            });

            Receive <UnableToAcceptJobMessage>(job =>
            {
                ColorConsole.WriteLineGreen("Commander {0} is unable to accept job {1}. {2}", _commander.Path.Name, job.ID, job.Description);
            });

            Receive <JobCompletedMessage>(job => HandleJobCompleted(job));
            Receive <JobFailedMessage>(job => HandleJobFailed(job));
            Receive <UpdateJobStatastics>(message => HandleUpdateJobStatastics());
        }
Beispiel #2
0
        // Represents the 'stopped' behavior
        private void Stopped()
        {
            Receive <PlayMovieMessage>(message => StartPlayingMovie(message.Title));
            Receive <StopMovieMessage>(message => ColorConsole.WriteLineRed($"ERROR: User:{_userId} Cannot stop if nothing is playing."));

            ColorConsole.WriteLine($"UserActor:{_userId} has now become 'Stopped'.", ConsoleColor.DarkCyan);
        }
        private void Playing()
        {
            Receive <PlayMovieMessage>(message => ColorConsole.WriteLineRed("Error: cannot start playing another movie before stopping existing one."));
            Receive <StopMovieMessage>(message => StopPlayingCurrentMovie());

            ColorConsole.WriteLineYellow("UserActor {0} has now become Playing", _userId);
        }
Beispiel #4
0
 /// <summary>
 /// Perform acknowledgement message from the sender.
 /// </summary>
 /// <param name="message"></param>
 private void HandleAcknowldgement(AcknowledgementMessage message)
 {
     if (message.Receipt == AcknowledgementReceipt.CANCELED)
     {
         throw new JobCanceledException();
     }
     else if (message.Receipt == AcknowledgementReceipt.INVALID_TASK)
     {
         Context.Parent.Tell(new JobFailedMessage(message.Description, message.ID, JobStatus.Failed));
         ColorConsole.WriteLineRed($"Task {message.ID}. {message.Description} is invalid.");
     }
     else if (message.Receipt == AcknowledgementReceipt.FAILED)
     {
         Context.Parent.Tell(new JobFailedMessage(message.Description, message.ID, JobStatus.Failed));
         ColorConsole.WriteLineRed($"Task {message.ID}. { message.Description} is failed due to unhandled exeption.");
     }
     else if (message.Receipt == AcknowledgementReceipt.TIMEOUT)
     {
         Context.Parent.Tell(new JobFailedMessage(message.Description, message.ID, JobStatus.Timeout));
         ColorConsole.WriteLineRed("Task ID: {0} is cancelled due to time out error.", message.ID);
     }
     else
     {
         if (message.CompletionTime == 0)
         {
             ColorConsole.WriteLineCyan($"Task ID: {message.ID} failed to execute by external application.");
             Context.Parent.Tell(new JobFailedMessage(message.Description, message.ID, JobStatus.Cancelled));
         }
         else
         {
             ColorConsole.WriteLineCyan("Task ID: {0} completed successfully by worker.", message.ID);
             Context.Parent.Tell(new JobCompletedMessage(message.Description, message.ID, message.CompletionTime));
         }
     }
 }
Beispiel #5
0
        private void ParseTxtFile(string fileName)
        {
            bool fileFound = File.Exists(fileName);

            if (!fileFound)
            {
                fileName  = Path.Combine(Environment.CurrentDirectory, fileName);
                fileFound = File.Exists(fileName);
                if (!fileFound)
                {
                    ColorConsole.WriteLineRed("Task file not found.");
                    return;
                }
            }

            var fileLines = File.ReadAllLines(fileName);
            int jobId     = 0;

            foreach (var line in fileLines)
            {
                jobId++;
                var job = new ProcessJobMessage(line, jobId);
                _jobsToProcessed.Add(job);
            }
        }
        private void Stopped()
        {
            Receive <PlayMovieMessage>(message => StartPlayingMovie(message.MovieTitle));
            Receive <StopMovieMessage>(message => ColorConsole.WriteLineRed("Error: cannot stop if nothing is playing"));

            ColorConsole.WriteLineYellow("UserActor has now become Stopped", _userId);
        }
Beispiel #7
0
        /// <summary>
        /// Supervisor strategy for coordinator
        /// </summary>
        /// <returns>action taken by the coordinator</returns>
        protected override SupervisorStrategy SupervisorStrategy()
        {
            return(new OneForOneStrategy(
                       exception =>
            {
                if (exception is JobCanceledException)
                {
                    ColorConsole.WriteLineRed("TaskCanceledException caught for Coordinator {0}, Restarting the task...", Self.Path.Name);
                    return Directive.Restart;
                }
                else if (exception is UnHandledException)
                {
                    ColorConsole.WriteLineRed("Unhandled exception caught for Coordinator {0}, Resuming Task...", Self.Path.Name);
                    return Directive.Resume;
                }
                else if (exception is JobTimeOutException)
                {
                    BecomeWaiting();
                    ColorConsole.WriteLineRed("Task Time out exception caught for Coordinator {0}, Stopping Task...", Self.Path.Name);
                    return Directive.Stop;
                }

                ColorConsole.WriteLineRed("Unknown exception caught for Coordinator {0}, Restarting the task...", Self.Path.Name);
                return Directive.Restart;
            }));
        }
Beispiel #8
0
        private void Stopped()
        {
            Receive <PlayMovieMessage>(message => StartPlayingMovie(message.MovieTitle));
            Receive <StopMovieMessage>(
                message => ColorConsole.WriteLineRed("cannot stop if nothing is playing"));

            ColorConsole.WriteLineYellow($"useractor {_userId} has become stopped");
        }
Beispiel #9
0
        private void Stopped()
        {
            Receive <PlayMovieMessage>(message => StartPlayingMovie(message.MovieTitle));
            string messageText = $"UserActor {_userId} Error: cannot stop if nothing is playing";

            Receive <StopMovieMessage>(message => ColorConsole.WriteLineRed(messageText));
            ColorConsole.WriteLineYellow($"UserActor {_userId} has now become Stopped");
        }
Beispiel #10
0
        private void Playing()
        {
            string messageText = $"UserActor {_userId} Error: cannot start playing another movie before stopping existing one";

            Receive <PlayMovieMessage>(message => ColorConsole.WriteLineRed(messageText));
            Receive <StopMovieMessage>(message => StopPlayingCurrentMovie());
            ColorConsole.WriteLineYellow($"UserActor {_userId} has now become Playing");
        }
Beispiel #11
0
        private void Stopped()
        {
            ColorConsole.WriteLineYellow(string.Format("UserActor {0} has now become Stopped", this.userId));

            this.Receive <PlayMovieMessage>(message => StartPlayingMovie(message.MovieTitle));
            this.Receive <StopMovieMessage>(
                message => ColorConsole.WriteLineRed(
                    string.Format("UserActor {0} Error: cannot stop if nothing is playing", this.userId)));
        }
Beispiel #12
0
        // Represents the 'playing' behavior
        private void Playing()
        {
            Receive <PlayMovieMessage>(message =>
                                       ColorConsole.WriteLineRed(
                                           $"ERROR: User:{_userId} Cannot start playing another movie ({message}) before stopping the existing one."));
            Receive <StopMovieMessage>(message => StopPlayingMovie());

            ColorConsole.WriteLine($"UserActor:{_userId} has now become 'Playing'.", ConsoleColor.DarkCyan);
        }
        protected override void PostStop()
        {
            if (null != _taskTimer)
            {
                _taskTimer.Cancel();
            }

            ColorConsole.WriteLineRed("WorkerActor for Coordinator {0} stopped.", Context.Parent.Path.Name);
        }
 protected override SupervisorStrategy SupervisorStrategy()
 {
     return(new OneForOneStrategy(
                exception =>
     {
         ColorConsole.WriteLineRed("Unknown exception caugh for commander, restarting the task...");
         return Directive.Restart;
     }));
 }
Beispiel #15
0
        private void Playing()
        {
            Receive <PlayMovieMessage>(message =>
                                       ColorConsole.WriteLineRed("Error: A movie is already playing"));
            Receive <StopMovieMessage>
                (message => StopPlayingCurrentMovie());

            ColorConsole.WriteLineYellow("UserActor has now become playing");
        }
Beispiel #16
0
        private void Playing()
        {
            ColorConsole.WriteLineYellow(string.Format("UserActor {0} has now become Playing", this.userId));

            this.Receive <StopMovieMessage>(message => StopPlayingCurrentMovie());
            this.Receive <PlayMovieMessage>(
                message => ColorConsole.WriteLineRed(
                    string.Format("UserActor {0} Error: cannot start playing another movie before stopping existing one", this.userId)));
        }
        private void HandleStopMovieMessage(StopMovieMessage message)
        {
            if (currentlyWatching == null)
            {
                ColorConsole.WriteLineRed("Error: cannot stop if nothing is playing");
                return;
            }

            StopPlayingCurrentMovie();
        }
Beispiel #18
0
        private void Playing()
        {
            Receive <PlayMovieMessage>(message => ColorConsole.WriteLineRed(
                                           "cannot start playing another movie"));

            Receive <StopMovieMessage>(
                message => StopPlayingCurrentMovie());

            ColorConsole.WriteLineYellow($"useractor {_userId} has become playing");
        }
        private void HandlePlayMovieMessage(PlayMovieMessage message)
        {
            if (currentlyWatching != null)
            {
                ColorConsole.WriteLineRed("Error: cannot start playing another movie before stopping existing one");
                return;
            }

            StartPlayingMovie(message.MovieTitle);
        }
Beispiel #20
0
        private void Playing()
        {
            // Defines what happens when we receive a PlayMovieMessage while we are in a Playing state.
            Receive <PlayMovieMessage>(message => ColorConsole.WriteLineRed("Error: cannot start playing another movie before stopping existing one"));

            // Defines what happens when we receive a StopMovieMessage while we are in a Playing state.
            Receive <StopMovieMessage>(message => StopPlayingCurrentMovie());

            ColorConsole.WriteLineYellow("UserActor {0} has now become Playing", UserID.ToString());
        }
Beispiel #21
0
        private void Stopped()
        {
            // Defines what happens when we receive a PlayMovieMessage while we are in a Stopped state.
            Receive <PlayMovieMessage>(message => StartPlayingMovie(message.MovieTitle));

            // Defines what happens when we receive a StopMovieMessage while we are in a Stopped state.
            Receive <StopMovieMessage>(message =>
                                       ColorConsole.WriteLineRed("Error: cannot stop if nothing is playing")
                                       );

            ColorConsole.WriteLineYellow("UserActor {0} has now become Stopped", UserID);
        }
        public JobPoolControllerActor(IActorRef commanderActor)
        {
            _commander = commanderActor;
            Receive <ProcessFileMessage>(msg => InitializeJobs(msg.FileName));
            Receive <UnableToAcceptJobMessage>(job =>
            {
                ColorConsole.WriteLineRed($"Commander {Sender.Path} is unable to perform Task : {job.Description}");
                if (!_jobsToProcessed.ContainsKey(job.ID))
                {
                    if (_taskList[job.ID].Status == JobStatus.NotStarted)
                    {
                        _jobsToProcessed.Add(job.ID, new ProcessJobMessage(job.Description, job.ID, Self));
                    }
                }
            });

            Receive <JobStartedMessage>(job =>
            {
                ColorConsole.WriteLineGreen($"Task {job.ID}. {job.Description} has started by {Sender.Path} at {job.ProcessedTime}");
                var task       = _taskList[job.ID];
                task.StartTime = job.ProcessedTime;
                task.Status    = JobStatus.Started;
                if (null == _stopWatch)
                {
                    _stopWatch = new Stopwatch();
                    _stopWatch.Start();
                }
                // update UI
            });

            Receive <JobCompletedMessage>(job =>
            {
                ColorConsole.WriteLineYellow($"Task {job.ID}. {job.Description} has completed succesfully at {job.ProcessedTime} by {Sender.Path} in {job.Duration} ms.");
                var task      = _taskList[job.ID];
                task.EndTime  = job.ProcessedTime;
                task.Duration = job.Duration;
                task.Status   = JobStatus.Completed;
                // update UI
            });

            Receive <JobFailedMessage>(job =>
            {
                ColorConsole.WriteLineRed($"Commander {Sender.Path} is unable to perform Task : {job.Description} because {job.Status.ToString()}");
                _taskList[job.ID].Status = job.Status;
                // update UI
            });

            Receive <ProcessUnfinishedJobs>(msg => HandleProcessUnFinishedJobs(msg));
        }
Beispiel #23
0
        static void Main(string[] args)
        {
            try
            {
                ApplicationLogger = NLog.LogManager.GetCurrentClassLogger();
            }
            catch (Exception ex)
            {
                ColorConsole.WriteLineRed("There seems to be a problem initializing the logger. Error message:{0} Inner Exception:{1}", ex.Message, ex.InnerException.Message ?? "");
                return;
            }
            string assembly = "C# Build: " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

            ApplicationLogger.Info("Version:" + assembly);

            //initialize the ClusterHelper
            //ClusterHelper.Initialize("couchbaseClients/couchbase");


            // Instantiate the actor system
            SSAActorSystem.Create();

            ApplicationLogger.Info("Host Actor System running on {0}", System.AppDomain.CurrentDomain.FriendlyName);
            string command = "";


            while (command != "exit")
            {
                Console.WriteLine("Enter a command:");
                command = Console.ReadLine(); // holds the app from closing

                if (command == "PrintSupervisorList")
                {
                    SSAActorSystem.ActorReferences.SupervisorRegistry.Tell("PrintSupervisorList", ActorRefs.Nobody);
                }

                ApplicationLogger.Info("User entered command line command:{0}", command);
            }


            ApplicationLogger.Info("Host Actor System on {0} is shutting down.", System.AppDomain.CurrentDomain.FriendlyName);
            // Shutdown the actor system
            SSAActorSystem.Shutdown();
            ApplicationLogger.Info("Host Actor System on {0} is shut down.", System.AppDomain.CurrentDomain.FriendlyName);

            // Close the Cluster Helper
            //ClusterHelper.Close();
        }
Beispiel #24
0
        private Task Stopped(IContext context)
        {
            switch (context.Message)
            {
            case PlayMovieMessage msg:
                _currentlyWatching = msg.MovieTitle;
                ColorConsole.WriteLineYellow($"User is currently watching '{_currentlyWatching}'");
                _behavior.Become(Playing);
                break;

            case StopMovieMessage msg:
                ColorConsole.WriteLineRed("Error: cannot stop if nothing is playing");
                break;
            }
            ColorConsole.WriteLineCyan("UserActor has now become Stopped");

            return(Actor.Done);
        }
Beispiel #25
0
        private Task Playing(IContext context)
        {
            switch (context.Message)
            {
            case PlayMovieMessage msg:
                ColorConsole.WriteLineRed("Error: cannot start playing another movie before stopping existing one");
                break;

            case StopMovieMessage msg:
                ColorConsole.WriteLineYellow($"User has stopped watching '{_currentlyWatching}'");
                _currentlyWatching = null;
                _behavior.Become(Stopped);
                break;
            }
            ColorConsole.WriteLineCyan("UserActor has now become Playing");

            return(Actor.Done);
        }
Beispiel #26
0
 protected override void PostStop()
 {
     _logger.Error("Cordinator{0}'s PostStop called.", Self.Path.Name);
     ColorConsole.WriteLineRed("Cordinator{0}'s PostStop called.", Self.Path.Name);
 }
 protected override void PostStop()
 {
     ColorConsole.WriteLineRed("WorkerActor for Coordinator {0} called PostStop.", Context.Parent.Path.Name);
     _logger.Error("WorkerActor for Coordinator {0} called PostStop.", Context.Parent.Path.Name);
 }
 private void Stopped()
 {
     Receive <PlayMovieMessage>(message => StartPlayingMovie(message.MovieTitle));
     Receive <StopMovieMessage>(message => ColorConsole.WriteLineRed("Error: cannot start playing another movie before stopping existing one"));
     ColorConsole.WriteLineCyan("User has now become Stopped");
 }
Beispiel #29
0
 private void HandleJobFailed(JobFailedMessage job)
 {
     _failedJobs.Add(job);
     ColorConsole.WriteLineRed("Task {0}. {1} failed ", job.ID, job.Description);
     Self.Tell(new UpdateJobStatastics());
 }
Beispiel #30
0
 private void Playing()
 {
     Receive <StopMessage>(m => StopPlayingMusic());
     Receive <PlayMessage>(m => ColorConsole.WriteLineRed("Error: Cannot play another track while playing the current one."));
 }