Ejemplo n.º 1
0
        /// <summary>
        ///     sends as much pending requests as there are waiting workers
        ///     of the specified service by
        ///     a) add the request to the pending requests within this service
        ///        if there is a message
        ///     b) remove all expired workers of this service
        ///     c) while there are waiting workers
        ///             get the next pending request
        ///             send to the worker
        /// </summary>
        private void ServiceDispatch(Service service, NetMQMessage message)
        {
            DebugLog($"Service [{service.Name}] dispatches -> {(message == null ? "PURGING" : "message = " + message)}");

            // if message is 'null' just send pending requests
            if (!ReferenceEquals(message, null))
            {
                service.AddRequest(message);
            }

            // remove all expired workers!
            Purge();

            // if there are requests pending and workers are waiting dispatch the requests
            // as long as there are workers and requests
            while (service.CanDispatchRequests())
            {
                var worker = service.GetNextWorker();

                if (service.PendingRequests.Count > 0)
                {
                    var request = service.GetNextRequest();

                    DebugLog($"Service Dispatch -> pending request {request} to {worker.Id}");

                    WorkerSend(worker, MDPCommand.Request, request);
                }
                else
                {
                    // no more requests -> we're done
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     sends as much pending requests as there are waiting workers 
        ///     of the specified service by
        ///     a) add the request to the pending requests within this service
        ///        if there is a message
        ///     b) remove all expired workers of this service
        ///     c) while there are waiting workers 
        ///             get the next pending request
        ///             send to the worker
        /// </summary>
        private void ServiceDispatch (Service service, NetMQMessage message)
        {
            DebugLog (string.Format ("Service [{0}] dispatches -> {1}",
                                     service.Name,
                                     message == null ? "PURGING" : "message = " + message));

            // if message is 'null' just send pending requests
            if (!ReferenceEquals (message, null))
                service.AddRequest (message);

            // remove all expired workers!
            Purge ();

            // if there are requests pending and workers are waiting dispatch the requests
            // as long as there are workers and requests
            while (service.CanDispatchRequests ())
            {
                var worker = service.GetNextWorker ();

                if (service.PendingRequests.Count > 0)
                {
                    var request = service.GetNextRequest ();

                    DebugLog (string.Format ("Service Dispatch -> pending request {0} to {1}", request, worker.Id));

                    WorkerSend (worker, MDPCommand.Request, request);
                }
                else
                    // no more requests -> we're done
                    break;
            }
        }
Ejemplo n.º 3
0
        public void GetNextWorker_SomeWaitingWorker_ShouldReturnOldestWorkerFirst()
        {
            Worker oldestWorker = null;
            var service = new Service("service");

            for (var i = 0; i < 10; i++)
            {
                var id = $"W0{i:N3}";
                var worker = new Worker(id, new NetMQFrame(id), service);

                if (i == 0)
                    oldestWorker = worker;

                service.AddWaitingWorker(worker);
            }

            Assert.That(service.GetNextWorker(), Is.EqualTo(oldestWorker));
            Assert.That(service.WaitingWorkers.Count(), Is.EqualTo(9));
        }
Ejemplo n.º 4
0
        public void GetNextWorker_GetAllWaitingWorkers_ShouldReturnEmptyWaitingAndLeaveKnownUnchanged()
        {
            var service = new Service("service");

            for (var i = 0; i < 10; i++)
            {
                var id = $"W0{i:N3}";
                var worker = new Worker(id, new NetMQFrame(id), service);
                service.AddWaitingWorker(worker);
                service.GetNextWorker();
            }

            Assert.That(service.WaitingWorkers, Is.Empty);
            Assert.That(service.DoWorkersExist(), Is.True);
        }