/// <summary>
        /// Delegate method for the watcher, copy file to raw folder
        /// after file and folder existence checking, and send the file
        /// to processor.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        public void OnCreated(object source, FileSystemEventArgs e)
        {
            var destinationFileFolder = _configuration["destination"];
            var sourceFileFolder      = _configuration["source"];

            // Check file existence before sent to process.
            if (!File.Exists(e.FullPath))
            {
                throw new Exception("There is no file been created.");
            }

            // Specify what is done when a file is created.
            _logger.LogInfoToConsole($"File: {e.FullPath}");

            if (!Directory.Exists(destinationFileFolder))
            {
                throw new Exception($"{destinationFileFolder} does not exist.");
            }

            var destinationFilePath = Path.Combine(destinationFileFolder, e.Name);

            if (File.Exists(destinationFilePath))
            {
                throw new Exception($"{destinationFilePath} has been processed before.");
            }

            File.Copy(Path.Combine(sourceFileFolder, e.Name), destinationFilePath);

            _eventProcessor.Process(e.FullPath, e.Name);
        }
示例#2
0
 protected override async Task ExecuteAsync(CancellationToken cancellationToken)
 {
     while (!cancellationToken.IsCancellationRequested)
     {
         logger.LogInformation("Worker processing event at: {time}", DateTimeOffset.Now);
         await eventProcessor.Process(cancellationToken);
     }
 }
示例#3
0
 /// <summary>
 /// Send an event
 /// </summary>
 /// <param name="eventData"></param>
 protected void SendEvent(IEvent eventData)
 {
     //we don't need to send the event if it is
     //a replay event
     if (!IsReplayEvent())
     {
         eventProcessor.Process(eventData, false);
     }
 }
示例#4
0
        public async Task <Guid> Handle(CreateExample request, CancellationToken cancellationToken)
        {
            Guid    id      = Guid.NewGuid();
            Example example = Example.Create(id, request.Content);

            await _exampleRepository.Add(example);

            await _eventProcessor.Process(example.Events);

            return(id);
        }
示例#5
0
        public void Process(GameSession session, IEvent e)
        {
            IEventProcessor processor = processors.GetValue(e.GetType());

            if (processor == null)
            {
                return;
            }

            processor.Process(session, e);
        }
        public void PublishEvent <T>(Event <T> e) where T : IEventData
        {
            if (e.TargetQueues.Count() == 0)
            {
                throw new ApplicationException("Event should target at least one queue");
            }

            if (string.IsNullOrEmpty(e.EventName))
            {
                throw new ApplicationException("Event name cannot be empty");
            }

            var client = new BackgroundJobClient();

            foreach (var queueName in e.TargetQueues)
            {
                var state = new EnqueuedState(queueName);

                client.Create(() => _eventProcessor.Process(e), state);
            }
        }
示例#7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="eventSaver">Where the event which will be replay are loaded</param>
        /// <param name="eventProcessor">The processor which trigger the event throughout
        /// the whole application</param>
        /// <param name="gateway"></param>
        public static void Replay(IEventSaver eventSaver,
                                  IEventProcessor eventProcessor, IControlGateway gateway)
        {
            IEvent[] eventList = eventSaver.LoadEvents();

            //replace with the event list from the file

            gateway.ReplaceEvents(eventList);

            //Reset all the state
            //We must reset all state before we reply the event because
            //we don't want them the state to be appended to each other
            eventProcessor.ResetState();

            foreach (IEvent eachEvent in eventList)
            {
                eventProcessor.Process(eachEvent, true);
            }
        }
示例#8
0
 void ProcessEvent(CommittedEventEnvelope envelope)
 {
     lock (lockObject)
     {
         _logger.Debug($"Processing {envelope.Version.ToString()} of {Key.Event.Id}");
         var previousVersion = LastVersionProcessed;
         try
         {
             SetCurrentVersion(envelope.Version);
             _processor.Process(envelope);
             _logger.Debug($"Processed {envelope.Version.ToString()} of {Key.Event.Id}");
         }
         catch (Exception ex)
         {
             _logger.Error($"Error Processing {envelope.Version.ToString()} of {Key.Event.Id}.  Resetting to previous version. {ex.Message}");
             SetCurrentVersion(previousVersion);
             throw;
         }
     }
     _logger.Debug($"Processing {envelope.Version.ToString()} of {Key.Event.Id}");
 }
示例#9
0
    void Update()
    {
        if (_initialised == false)
        {
            try
            {
                Init();
                _initialised = true;
            }
            catch
            {
                /* Not exactly production code but works for a sample */
            }
        }

        if (_triggerTutorial)
        {
            _processor.Process(new TutorialTriggerEvent());
            _triggerTutorial = false;
        }
    }
示例#10
0
            public Task ListInstanceEventsAsync(
                IEnumerable <string> projectIds,
                IEnumerable <string> zones,
                IEnumerable <ulong> instanceIds,
                DateTime startTime,
                IEventProcessor processor,
                CancellationToken cancellationToken)
            {
                this.CallCount++;

                var systemEventJson = @"
                 {
                   'protoPayload': {
                     '@type': 'type.googleapis.com/google.cloud.audit.AuditLog',
                     'authenticationInfo': {
                     },
                     'serviceName': 'compute.googleapis.com',
                     'methodName': 'NotifyInstanceLocation',
                     'request': {
                       '@type': 'type.googleapis.com/NotifyInstanceLocation'
                     },
                     'metadata': {
                       'serverId': '4aaaa7b32a208e7ccb4ee62acedee725',
                       'timestamp': '2020-05-04T01:50:10.917Z',
                       '@type': 'type.googleapis.com/google.cloud.audit.GceInstanceLocationMetadata'
                     }
                   },
                   'insertId': '-x0boqfe25xye',
                   'resource': {
                     'type': 'gce_instance',
                     'labels': {
                       'instance_id': '7045222222254025',
                       'project_id': 'project-1',
                       'zone': 'us-central1-a'
                     }
                   },
                   'timestamp': '2020-05-04T01:50:16.885Z',
                   'severity': 'INFO',
                   'logName': 'projects/project-1/logs/cloudaudit.googleapis.com%2Fsystem_event',
                   'receiveTimestamp': '2020-05-04T01:50:17.020301892Z'
                 } ";

                var lifecycleEventJson = @"
                {
                   'protoPayload': {
                     '@type': 'type.googleapis.com/google.cloud.audit.AuditLog',
                     'authenticationInfo': {
                     },
                     'requestMetadata': {
                       'callerIp': '1.2.3.4',
                       'callerSuppliedUserAgent': 'Mozilla'
                     },
                     'serviceName': 'compute.googleapis.com',
                     'methodName': 'v1.compute.instances.reset',
                     'resourceName': 'projects/project-1/zones/us-central1-a/instances/instance-1',
                     'request': {
                       '@type': 'type.googleapis.com/compute.instances.reset'
                     }
                   },
                   'insertId': 'yz07i2c',
                   'resource': {
                     'type': 'gce_instance',
                     'labels': {
                       'instance_id': '4894051111144103',
                       'project_id': 'project-1',
                       'zone': 'us-central1-a'
                     }
                   },
                   'timestamp': '2020-05-11T14:41:30.863Z',
                   'severity': 'NOTICE',
                   'logName': 'projects/project-1/logs/cloudaudit.googleapis.com%2Factivity',
                   'operation': {
                     'id': 'operation-1589208088486-5a5605796a1ac-2d2b0706-bf57b173',
                     'producer': 'compute.googleapis.com',
                     'last': true
                   },
                   'receiveTimestamp': '2020-05-11T14:41:31.096086630Z'
                 }";

                processor.Process(new NotifyInstanceLocationEvent(LogRecord.Deserialize(systemEventJson)));
                processor.Process(new ResetInstanceEvent(LogRecord.Deserialize(lifecycleEventJson)));
                return(Task.CompletedTask);
            }
示例#11
0
 private static IEnumerable <T> Process <T>(IEvent @event, IContext context, IEventProcessor processor, Microsoft.Extensions.Logging.ILogger log)
 {
     return(processor.Process <T, IEvent, IContext>(@event, context));
 }
        public async Task ProcessInstanceEventsAsync(
            string bucket,
            DateTime startTime,
            DateTime endTime,
            IEventProcessor processor,
            CancellationToken cancellationToken)
        {
            Debug.Assert(startTime.Kind == DateTimeKind.Utc);

            if (startTime.Date > DateTime.UtcNow.Date)
            {
                return;
            }

            using (TraceSources.IapDesktop.TraceMethod().WithParameters(bucket, startTime))
            {
                var severitiesWhitelist = processor.SupportedSeverities.ToHashSet();
                var methodsWhitelist    = processor.SupportedMethods.ToHashSet();

                var objectsByDay = await FindAuditLogExportObjectsGroupedByDay(
                    bucket,
                    startTime,
                    DateTime.UtcNow,
                    cancellationToken)
                                   .ConfigureAwait(false);

                var days = (processor.ExpectedOrder == EventOrder.OldestFirst)
                    ? DateRange.DayRange(startTime, endTime.Date, 1)
                    : DateRange.DayRange(endTime, startTime.Date, -1);

                foreach (var day in days)
                {
                    TraceSources.IapDesktop.TraceVerbose("Processing {0}", day);

                    //
                    // Grab the objects for this day (typically 2, one activity and one system event).
                    // Each object is (probably) sorted in ascending order, but we need a global,
                    // descending order. Therefore, download everything for that day, merge, and
                    // sort it before processing each event.
                    //

                    if (objectsByDay.TryGetValue(day, out IEnumerable <StorageObjectLocator> objectsForDay))
                    {
                        TraceSources.IapDesktop.TraceVerbose(
                            "Processing {1} export objects for {0}", day, objectsForDay.Count());

                        var eventsForDay = await ListInstanceEventsAsync(
                            objectsForDay,
                            cancellationToken)
                                           .ConfigureAwait(false);

                        // Merge and sort events.
                        var eventsForDayOrdered = (processor.ExpectedOrder == EventOrder.OldestFirst)
                            ? eventsForDay.OrderBy(e => e.Timestamp)
                            : eventsForDay.OrderByDescending(e => e.Timestamp);

                        foreach (var e in eventsForDayOrdered)
                        {
                            if (e.LogRecord?.ProtoPayload?.MethodName != null &&
                                methodsWhitelist.Contains(e.LogRecord.ProtoPayload.MethodName) &&
                                severitiesWhitelist.Contains(e.Severity))
                            {
                                processor.Process(e);
                            }
                        }
                    }
                    else
                    {
                        TraceSources.IapDesktop.TraceWarning("No export objects found for {0}", day);
                    }
                }
            }
        }
示例#13
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            int value = int.Parse(txtValue.Text);

            eventProcessor.Process(new AddValueEvent(value), false);
        }
示例#14
0
        /// <summary>
        /// Process the <see cref="StreamEvent" /> and get the new <see cref="IStreamProcessorState" />.
        /// </summary>
        /// <param name="event">The <see cref="StreamEvent" />.</param>
        /// <param name="currentState">The current <see cref="IStreamProcessorState" />.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken" />.</param>
        /// <returns>A <see cref="Task"/> that, when returned, returns the new <see cref="IStreamProcessorState" />.</returns>
        protected virtual async Task <IStreamProcessorState> ProcessEvent(StreamEvent @event, IStreamProcessorState currentState, CancellationToken cancellationToken)
        {
            var processingResult = await _processor.Process(@event.Event, @event.Partition, cancellationToken).ConfigureAwait(false);

            return(await HandleProcessingResult(processingResult, @event, currentState).ConfigureAwait(false));
        }