Ejemplo n.º 1
0
        public async Task <ReportActivityResult> ExecuteAsync(ReportActivity command, CancellationToken cancellationToken)
        {
            await repository.SetServiceLastActiveAsync(command.LastActiveUtc, cancellationToken);

            TimeSpan expirationQuanta = await repository.GetExpirationQuantaAsync(cancellationToken);

            return(new ReportActivityResult(TimeSpan.FromMilliseconds(expirationQuanta.TotalMilliseconds / 3)));
        }
        public async Task <OccupyResponse> OccupyAsync(OccupyRequest request)
        {
            var result = await _context.OccupyAsync(new OccupyInstance(this.GetActorId().GetGuidId(), request.PartitionId, request.ServiceInstanceName), _cancellation);

            //calculate a seed value to the nearest 100ms to stagger the due time of the different actors.
            //this prevents from all actors occupied within milliseconds of each other from all vacating at exactly the same time
            //which will cause a deadlock on the pool actor.
            var expirationQuanta = await _repository.GetExpirationQuantaAsync(_cancellation);

            var intervalMs = (int)Math.Round(expirationQuanta.TotalMilliseconds / 5);
            var dueMs      = ((int)Math.Round((GetInstanceId().GetHashCode() % 1000) / 100.0) * 100) + intervalMs;

            await RegisterReminderAsync("expiration-quanta", null, TimeSpan.FromMilliseconds(dueMs), TimeSpan.FromMilliseconds(intervalMs));

            return(new OccupyResponse(result.ServiceName));
        }
Ejemplo n.º 3
0
        public async Task ExecuteAsync(CheckForExpiration command, CancellationToken cancellationToken)
        {
            var expirationQuanta = await repository.GetExpirationQuantaAsync(cancellationToken);

            var lastActive = await repository.GetServiceLastActiveAsync(cancellationToken);

            var inactivityPeriod = command.AsOfDate.Subtract(lastActive);

            if (inactivityPeriod > expirationQuanta)
            {
                string instanceName = await repository.TryGetServiceInstanceNameAsync(cancellationToken);

                string partitionId = await repository.GetPartitionIdAsync(cancellationToken);

                string serviceTypeUri = await repository.GetServiceTypeUriAsync(cancellationToken);

                await partitions.VacateInstanceAsync(partitionId, serviceTypeUri, instanceName, command.InstanceId);
            }
        }