示例#1
0
        private bool TryCaptureInternal(
            SynchronizationServiceConfiguration synchronizationService,
            string ownerId,
            out SynchronizationToken token)
        {
            var synchronizationObject = _synchronizationObjectRepository.GetLastCreated();

            // If the synchronization object isn't captured
            if (!synchronizationObject.ServiceId.HasValue)
            {
                synchronizationObject = _synchronizationObjectRepository.Capture(synchronizationObject.Id, ownerId, synchronizationService.Id);
                _synchronizationObjectRepository.SaveChanges();

                // Exception throwing means that synchronization object was updated (captured) by another service execution

                token = SynchronizationToken.Create(synchronizationObject, ownerId);

                return(true);
            }

            if (synchronizationObject.ServiceId == synchronizationService.Id)
            {
                if (synchronizationService.MaxConcurrencyLevel.HasValue &&
                    synchronizationObject.OwnerCount == synchronizationService.MaxConcurrencyLevel)
                {
                    token = SynchronizationToken.Empty;

                    return(false);
                }

                if (synchronizationObject.Owners.Any(oo => oo.OwnerId == ownerId && oo.IsActive))
                {
                    token = SynchronizationToken.Empty;

                    return(false);
                }

                synchronizationObject = _synchronizationObjectRepository.Capture(synchronizationObject.Id, ownerId);
                _synchronizationObjectRepository.SaveChanges();

                // Exception throwing means that synchronization object was updated by another run execution, there are only 3 cases:
                // 1. Another new incoming run incremented Count of Owners (Captured this object) of this sync object
                // 2. Some owner of this sync object decremented Count of Owners
                // 3. Some owner of this sync object decremented Count of Owners and in case if CountOfOwners == 0 released this object and added new entry to the database

                token = SynchronizationToken.Create(synchronizationObject, ownerId);

                return(true);
            }

            // If synchronization object is captured by another service

            token = SynchronizationToken.Empty;

            return(false);
        }
        public void AddService(int id, int?maxConcurrencyLevel = null)
        {
            var configuration = new SynchronizationServiceConfiguration(id, maxConcurrencyLevel);

            _configuration.Add(configuration);
        }
 public bool TryGet(int id, out SynchronizationServiceConfiguration service)
 {
     return(_services.TryGetValue(id, out service));
 }
        public SynchronizationServicesConfiguration Add(SynchronizationServiceConfiguration service)
        {
            _services.Add(service.Id, service);

            return(this);
        }