コード例 #1
0
 public static void ExitProcessDelayed(TimeSpan delay, int exitCode = 0)
 {
     if (delay == TimeSpan.Zero)
     {
         Utils.TerminateSelf(exitCode);
         return;
     }
     TaskFactoryExtensions.StartNewDelayed(Task.Factory, (int)delay.TotalMilliseconds, delegate()
     {
         Utils.TerminateSelf(exitCode);
     });
 }
コード例 #2
0
        public Task PaxosMessageAsync(string sender, Message message)
        {
            Task result;

            try
            {
                if (this.StateMachine != null && this.StateMachine.Mesh != null)
                {
                    this.StateMachine.Mesh.Incoming.OnNext(Tuple.Create <string, Message>(sender, message));
                }
                else
                {
                    DxStoreInstance.Tracer.TraceError <string>(0L, "{0}: PaxosMessageAsync skipped since state machine is not configured yet.", this.GroupConfig.Identity);
                }
                result = TaskFactoryExtensions.FromResult <object>(Task.Factory, null);
            }
            catch (Exception ex)
            {
                result = TaskFactoryExtensions.FromException(Task.Factory, ex);
            }
            return(result);
        }
コード例 #3
0
        public async Task <IActionResult> AddTopicToCapability(string id, [FromBody] TopicInput input)
        {
            var capabilityId = Guid.Empty;

            Guid.TryParse(id, out capabilityId);

            if (capabilityId == Guid.Empty)
            {
                return(BadRequest(new { Message = $"the capability id: {id} is malformed" }));
            }

            var capability = await
                             _capabilityRepository.Get(capabilityId);

            IActionResult actionResult;

            try
            {
                var configurations = new Dictionary <string, object>();
                if (input.Configurations != null)
                {
                    foreach (var(key, value) in input.Configurations)
                    {
                        var jsonElement = (JsonElement)value;
                        configurations[key] = JsonObjectTools.GetValueFromJsonElement(jsonElement);
                    }
                }

                if (String.IsNullOrEmpty(input.KafkaClusterId))
                {
                    throw new ClusterNotSelectedException();
                }

                var topic = Topic.Create(
                    capabilityId,
                    Guid.Parse(input.KafkaClusterId),
                    capability.RootId,
                    input.Name,
                    input.Description,
                    input.Partitions,
                    input.Availability,
                    configurations
                    );

                await _topicDomainService.CreateTopic(
                    topic : topic,
                    dryRun : true
                    );

                var kafkaCluster = await _topicDomainService.GetClusterById(topic.KafkaClusterId);

                if (input.DryRun)
                {
                    return(Ok(DTOs.Topic.CreateFrom(topic)));
                }

                TaskFactoryExtensions.StartActionWithConsoleExceptions(async() =>
                {
                    await _kafkaJanitorRestClient.CreateTopic(topic, capability, kafkaCluster.ClusterId);

                    await _topicDomainService.CreateTopic(
                        topic: topic,
                        dryRun: input.DryRun
                        );
                });

                var topicDto = DTOs.Topic.CreateFrom(topic);
                actionResult = Ok(topicDto);
            }
            catch (Exception exception) when(ExceptionToStatusCode.CanConvert(exception, out actionResult))
            {
            }

            return(actionResult);
        }
コード例 #4
0
        private void UpdateCallback(object state)
        {
            if ((this.IsDisposed || !this.IsRunning ? 0 : (Interlocked.CompareExchange(ref this.m_currentThreadId, Thread.CurrentThread.ManagedThreadId, 0) == 0 ? 1 : 0)) == 0)
            {
                return;
            }
            DateTime now  = DateTime.Now;
            int      num1 = (int)(now - this.m_lastUpdateTime).TotalMilliseconds;
            long     num2 = 0L;
            long     num3 = 0L;
            int      num4 = 0;

            try
            {
                Stopwatch stopwatch1 = Stopwatch.StartNew();
                IMessage  message;
                while (this.m_messageQueue.TryDequeue(out message))
                {
                    try
                    {
                        message.Execute();
                    }
                    catch (Exception ex)
                    {
                        Area.logger.Error <Area, Exception>("Exception raised when processing Message in {0} : {1}.", this, ex);
                    }
                }
                stopwatch1.Stop();
                num2 = stopwatch1.ElapsedMilliseconds;
                this.m_isUpdating = true;
                foreach (TimedTimerEntry timedTimerEntry in Enumerable.Where <TimedTimerEntry>((IEnumerable <TimedTimerEntry>) this.m_pausedTimers, (Func <TimedTimerEntry, bool>)(timer => timer.Enabled)))
                {
                    this.m_timers.Push(timedTimerEntry);
                }
                Stopwatch       stopwatch2 = Stopwatch.StartNew();
                TimedTimerEntry timedTimerEntry1;
                while (((timedTimerEntry1 = this.m_timers.Peek()) == null ? 0 : (timedTimerEntry1.NextTick <= DateTime.Now ? 1 : 0)) != 0)
                {
                    TimedTimerEntry timedTimerEntry2 = this.m_timers.Pop();
                    if (!timedTimerEntry2.Enabled)
                    {
                        if (!timedTimerEntry2.IsDisposed)
                        {
                            this.m_pausedTimers.Add(timedTimerEntry2);
                        }
                    }
                    else
                    {
                        try
                        {
                            timedTimerEntry2.Trigger();
                            if (timedTimerEntry2.Enabled)
                            {
                                this.m_timers.Push(timedTimerEntry2);
                            }
                            ++num4;
                        }
                        catch (Exception ex)
                        {
                            Area.logger.Error <Area, Exception>("Exception raised when processing TimerEntry in {0} : {1}.", this, ex);
                        }
                    }
                }
                stopwatch2.Stop();
                num3 = stopwatch2.ElapsedMilliseconds;
            }
            finally
            {
                try
                {
                    this.m_lastUpdateTime = now;
                    ++this.TickCount;
                    this.m_isUpdating = false;
                    TimeSpan timeSpan = DateTime.Now - now;
                    this.AverageUpdateTime = (float)(((double)this.AverageUpdateTime * 9.0 + timeSpan.TotalMilliseconds) / 10.0);
                    Interlocked.Exchange(ref this.m_currentThreadId, 0);
                    int millisecondsDelay = (int)((double)this.m_updateDelay - timeSpan.TotalMilliseconds);
                    if (millisecondsDelay < 0)
                    {
                        millisecondsDelay = 0;
                        Area.logger.Debug("Area '{0}' update lagged ({1}ms) (msg:{2}ms, timers:{3}ms, timerProc:{4}/{5})", (object)this, (object)(int)timeSpan.TotalMilliseconds, (object)num2, (object)num3, (object)num4, (object)this.m_timers.Count);
                    }
                    if (!this.IsRunning)
                    {
                        this.m_stoppedAsync.Set();
                    }
                    TaskFactoryExtensions.StartNewDelayed(Task.Factory, millisecondsDelay, new Action <object>(this.UpdateCallback), (object)this);
                }
                catch (Exception ex)
                {
                    Area.logger.Error <Area, Exception>("Area {0}. Could not recall callback !! Exception {1}", this, ex);
                }
            }
        }