public async Task IncrementAsync(string counterName, string id, long value)
        {
            var key    = string.Format("____COUNTER____{0}-{1}", counterName, id);
            var token  = new LockToken(key);
            var result = await _lockStore.TryLockAsync(token);

            if (!result)
            {
                throw new TimeoutException("Could not lock the resource");
            }
            var blob = await GetBlobAsync(key);

            var text = await blob.DownloadTextAsync();

            var current  = long.Parse(string.IsNullOrEmpty(text) ? "0" : text);
            var newValue = current + value;

            if (newValue < 0)
            {
                throw new InvalidOperationException("Value cannot get below zero: " + newValue);
            }

            await blob.UploadTextAsync(newValue.ToString());

            await _lockStore.ReleaseLockAsync(token);
        }
Exemple #2
0
        public async Task <Tuple <IEnumerable <Event>, bool> > TryScheduleAsync(DiagnosticsSource source)
        {
            // if Stop offset has been reached
            if (!string.IsNullOrEmpty(source.StopOffsetPoint) && source.LastOffsetPoint != null && source.LastOffsetPoint.CompareTo(source.StopOffsetPoint) >= 0)
            {
                return(new Tuple <IEnumerable <Event>, bool>(Enumerable.Empty <Event>(), false));
            }

            var lockToken = new LockToken(source.ToTypeKey());
            int seconds   =
                Convert.ToInt32(_configurationValueProvider.GetValue(ConfigurationKeys.ClusterLockDurationSeconds));

            if (!(await _lockStore.TryLockAsync(lockToken, 2, 1000, seconds * 1000)))
            {
                TheTrace.TraceInformation("I could NOT be master for {0}", source.ToTypeKey());
                return(new Tuple <IEnumerable <Event>, bool>(Enumerable.Empty <Event>(), false));
            }
            try
            {
                var events = await DoSchedule(source);

                return(new Tuple <IEnumerable <Event>, bool>(events, true));
            }
            finally
            {
                Task.Run(() => _lockStore.ReleaseLockAsync(lockToken)).Wait();
            }
        }
Exemple #3
0
        public async Task ScheduleSourcesAsync()
        {
            var token = new LockToken(_clustername);

            if (await _lockStore.TryLockAsync(token, tries: 1, timeoutMilliseconds: 30000))
            {
                foreach (var source in _table.ExecuteQuery(new TableQuery <PeckSource>()))
                {
                    if (source.IsActive && DateTimeOffset.Now > source.LastOffset.AddMinutes(source.IntervalMinutes))
                    {
                        await DoScheduleAsync(source);
                    }
                }
                await _lockStore.ReleaseLockAsync(token);
            }
        }
        public async Task ScheduleSourcesAsync()
        {
            if (!await ShouldScheduleAsync())
            {
                return;
            }

            var seconds =
                Convert.ToInt32(_configurationValueProvider.GetValue(ConfigurationKeys.ClusterLockDurationSeconds));
            var sources = _sourceConfiguration.GetSources();

            foreach (var source in sources)
            {
                try
                {
                    var lockToken = new LockToken(source.ToTypeKey());

                    if (!(await _lockStore.TryLockAsync(lockToken, tries: 0, aquireTimeoutMilliseconds: 100, timeoutMilliseconds: seconds * 1000))) // if tries < 1 it puts to 1 in beehive
                    {
                        TheTrace.TraceInformation("I could NOT be master for {0}", source.ToTypeKey());
                        continue;
                    }

                    var resultSource = await TryScheduleSourceAsync(source);

                    if (resultSource != null)
                    {
                        _sourceConfiguration.UpdateSource(resultSource);
                        TheTrace.TraceInformation("MasterScheduler - Updated {0}", resultSource.ToTypeKey());
                    }

                    await _lockStore.ReleaseLockAsync(lockToken);
                }
                catch (Exception e)
                {
                    TheTrace.TraceError(e.ToString());
                }
            }
        }