예제 #1
0
        protected override Task OnReceiveAsync(IContext context)
        {
            switch (context.Message)
            {
            case Started _:
            {
                var props = Actor.FromProducer(() => new DemoChildActor());
                context.SpawnNamed(props, "Child1");
                context.SpawnNamed(props, "Child2");

                _scheduler.ScheduleTellRepeatedly(
                    TimeSpan.FromSeconds(5),
                    TimeSpan.FromSeconds(10),
                    context.Self,
                    PingMessage.Instance,
                    out _cts
                    );
                break;
            }

            case PingMessage p:
            {
                Logger.LogInformation("Another ping in the wall");
                break;
            }

            case Stopping _:
            {
                _cts.Cancel();
                break;
            }
            }

            return(Actor.Done);
        }
예제 #2
0
        public Task ReceiveAsync(IContext context)
        {
            if (context.Message is Started)
            {
                var pid = context.Spawn(Actor.FromProducer(() => new ScheduleGreetActor()));
                scheduler.ScheduleTellOnce(TimeSpan.FromMilliseconds(100), context.Self, new SimpleMessage("test 1"))
                .ScheduleTellOnce(TimeSpan.FromMilliseconds(200), context.Self, new SimpleMessage("test 2"))
                .ScheduleTellOnce(TimeSpan.FromMilliseconds(300), context.Self, new SimpleMessage("test 3"))
                .ScheduleTellOnce(TimeSpan.FromMilliseconds(400), context.Self, new SimpleMessage("test 4"))
                .ScheduleTellOnce(TimeSpan.FromMilliseconds(500), context.Self, new SimpleMessage("test 5"))
                .ScheduleRequestOnce(TimeSpan.FromSeconds(1), context.Self, pid, new Greet("Daniel"))
                .ScheduleTellOnce(TimeSpan.FromSeconds(5), context.Self, new Hello());
            }
            else if (context.Message is Hello)
            {
                var hl = context.Message as Hello;
                Console.WriteLine($"Hello Once,let's give you a hick");
                scheduler.ScheduleTellRepeatedly(TimeSpan.FromSeconds(3), TimeSpan.FromMilliseconds(500), context.Self, new HickUp(), out timer);
            }
            else if (context.Message is HickUp)
            {
                var hu = context.Message as HickUp;
                counter++;
                Console.WriteLine("Hello!");

                if (counter == 5)
                {
                    timer.Cancel();
                    context.Self.Tell(new AbortHickUp());
                }
            }
            else if (context.Message is AbortHickUp)
            {
                Console.WriteLine($"Aborted hickup after {counter} times");
                Console.WriteLine($"All this was scheduled calls, have fun!");
            }
            else if (context.Message is Greet)
            {
                var msg = context.Message as Greet;
                Console.WriteLine($"Thanks {msg.Who}");
            }
            else if (context.Message is SimpleMessage)
            {
                var sm = context.Message as SimpleMessage;
                Console.WriteLine(sm.Msg);
            }
            return(Actor.Done);
        }
예제 #3
0
        public Task ReceiveAsync(IContext context)
        {
            switch (context.Message)
            {
            case Started _:
            {
                _scheduler.ScheduleTellRepeatedly(
                    TimeSpan.FromSeconds(5),
                    TimeSpan.FromSeconds(5),
                    context.Self,
                    Flush.Instance,
                    out _cts
                    );
                break;
            }

            case Stopping _:
            {
                _cts.Cancel();
                break;
            }

            case TrackedMessage tracked:
            {
                if (_buffer == null)
                {
                    _buffer = new List <TrackedMessage>(1000);
                }

                _buffer.Add(tracked);

                break;
            }

            case Flush _ when _buffer != null:
            {
                _client.PushAsync(_buffer);
                _buffer = null;
                break;
            }
            }
            return(Actor.Done);
        }
예제 #4
0
        public async Task ReceiveAsync(IContext context)
        {
            switch (context.Message)
            {
            case Started _:
            {
                _scheduler.ScheduleTellRepeatedly(
                    TimeSpan.FromSeconds(1),
                    TimeSpan.FromSeconds(2),
                    context.Self,
                    new DeleteCommandBatch(),
                    out _timer);
            }
            break;

            case DeleteCommand delete:
            {
                AddToBatch(delete);
            }
            break;

            case DeleteCommandBatch batch:
            {
                if (_receiptHandles.Count > 0)
                {
                    var successful = new List <string>();

                    foreach (var chunk in _receiptHandles.ChunkBy(10))
                    {
                        var response = await _sqsClient.DeleteCommandBatchAsync(chunk);

                        successful.AddRange(response.Successful);
                    }

                    RemoveFromBatch(successful);
                }
            }
            break;
            }
        }
예제 #5
0
        public Task ReceiveAsync(IContext context)
        {
            switch (context.Message)
            {
            case Started _:
            {
                _simpleScheduler.ScheduleTellRepeatedly(
                    TimeSpan.FromSeconds(1),
                    TimeSpan.FromSeconds(_flushInterval),
                    context.Self,
                    Flush.Instance,
                    out _cts
                    );
                break;
            }

            case Stopping _:
            {
                _cts.Cancel();
                BackgroundFlush();

                break;
            }

            case Flush _:
            {
                BackgroundFlush();
                break;
            }

            case LogEvent logEvent:
            {
                BufferEvent(logEvent);
                break;
            }
            }

            return(Actor.Done);
        }
예제 #6
0
        public Task ReceiveAsync(IContext context)
        {
            var msg = context.Message;

            if (msg is ScheduleWorkCmd)
            {
                try
                {
                    var cmd = (ScheduleWorkCmd)msg;

                    var pid = Actor.Spawn(Actor.FromProducer(() => new WorkPerformerActor()));
                    pid.Tell(new WorkerIntiationCmd(cmd));
                    CancellationTokenSource tokenSource;

                    _scheduler.ScheduleTellRepeatedly(
                        TimeSpan.Zero,
                        TimeSpan.FromMinutes(cmd.Interval),
                        pid,
                        new PerformWorkCmd(),
                        out tokenSource
                        );

                    if (cmd.SaveToDisk)
                    {
                        StaticActors.SaveToDiskActor
                        .Tell(new SaveWorkItemToDiskCmd(cmd));
                    }

                    _scheduledWork.Add(cmd.Id, new WorkDescription()
                    {
                        ActorPID    = pid,
                        TokenSource = tokenSource
                    });
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error: {0}", e.Message);
                }
            }
            else if (msg is CancelScheduledWorkCmd)
            {
                var cmd = (CancelScheduledWorkCmd)msg;

                try
                {
                    var description = _scheduledWork[cmd.Id];

                    if (description != null)
                    {
                        description.TokenSource.Cancel();
                        description.ActorPID.Stop();

                        StaticActors.SaveToDiskActor.Tell(new RemoveWorkItemFromDiskCmd(cmd.Id));
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error: {0}", e.Message);
                }
            }

            return(Actor.Done);
        }
예제 #7
0
        public async Task ReceiveAsync(IContext context)
        {
            switch (context.Message)
            {
            case Started _:
            {
                SherlockInspectionActor.Pid = context.Self;
                _scheduler.ScheduleTellRepeatedly(
                    TimeSpan.FromSeconds(5),
                    TimeSpan.FromSeconds(5),
                    context.Self,
                    Inspect.Instance,
                    out _cts
                    );
                break;
            }

            case Stopping _:
            {
                _cts.Cancel();
                break;
            }

            case AddToInspection add:
            {
                _targets.Add(add.Actor);
                break;
            }

            case Inspect i:
            {
                if (!_targets.Any())
                {
                    _logger.LogWarning("No targets configued");
                    return;
                }

                _logger.LogDebug("Sending inspection reports");
                // send reports
                try
                {
                    await _client.PushAsync(_reports.Reports.Values).ConfigureAwait(false);

                    _logger.LogDebug("Reports sent");
                }
                catch (RpcException ex)
                {
                    _logger.LogDebug("Sherlock server connection error: {message}", ex.Message);
                    if (ex.Status.StatusCode != StatusCode.Unavailable)
                    {
                        throw;
                    }
                }

                // refresh reports
                _logger.LogDebug("Creating new inspection reports");
                foreach (var t in _targets)
                {
                    t.Request(Inspect.Instance, context.Self);
                }

                break;
            }

            case InspectionReportRequest req:
            {
                context.Sender.Tell(_reports);
                break;
            }

            case InspectionReport report:
            {
                _reports.Reports[report.Pid.ToString()] = report;
                break;
            }
            }
        }
예제 #8
0
        public Task ReceiveAsync(IContext context)
        {
            switch (context.Message)
            {
            case Started _:

                var pid = context.Spawn(Props.FromProducer(() => new ScheduleGreetActor()));

                _scheduler
                .ScheduleTellOnce(TimeSpan.FromMilliseconds(100), context.Self, new SimpleMessage("test 1"))
                .ScheduleTellOnce(TimeSpan.FromMilliseconds(200), context.Self, new SimpleMessage("test 2"))
                .ScheduleTellOnce(TimeSpan.FromMilliseconds(300), context.Self, new SimpleMessage("test 3"))
                .ScheduleTellOnce(TimeSpan.FromMilliseconds(400), context.Self, new SimpleMessage("test 4"))
                .ScheduleTellOnce(TimeSpan.FromMilliseconds(500), context.Self, new SimpleMessage("test 5"))
                .ScheduleRequestOnce(TimeSpan.FromSeconds(1), context.Self, pid, new Greet("Daniel"))
                .ScheduleTellOnce(TimeSpan.FromSeconds(5), context.Self, new Hello());

                break;

            case Hello _:

                Console.WriteLine($"Hello Once, let's give you a hickup every 0.5 second starting in 3 seconds!");

                _scheduler.ScheduleTellRepeatedly(TimeSpan.FromSeconds(3), TimeSpan.FromMilliseconds(500), context.Self, new HickUp(), out _timer);

                break;

            case HickUp _:

                _counter++;

                Console.WriteLine("Hello!");

                if (_counter == 5)
                {
                    _timer.Cancel();

                    context.Send(context.Self, new AbortHickUp());
                }

                break;

            case AbortHickUp _:

                Console.WriteLine($"Aborted hickup after {_counter} times");

                Console.WriteLine("All this was scheduled calls, have fun!");

                break;

            case Greet msg:

                Console.WriteLine($"Thanks {msg.Who}");

                break;

            case SimpleMessage sm:

                Console.WriteLine(sm.Msg);

                break;
            }

            return(Actor.Done);
        }