Esempio n. 1
0
        public void CallCreateTest()
        {
            ExecutorFactory factory = new ExecutorFactory(new IocContainer());

            Assert.IsType <Simple1Executor>(factory.Create(new Simple1Command()).Executor);
            Assert.IsType <Simple2Executor>(factory.Create(new Simple2Command()).Executor);
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void initShouldCreateThreadPool() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void InitShouldCreateThreadPool()
        {
            ExecutorFactory mockExecutorFactory = mock(typeof(ExecutorFactory));

            when(mockExecutorFactory.Create(anyInt(), anyInt(), any(), anyInt(), anyBoolean(), any())).thenReturn(Executors.newCachedThreadPool());
            ExecutorBoltScheduler scheduler = new ExecutorBoltScheduler(CONNECTOR_KEY, mockExecutorFactory, _jobScheduler, _logService, 0, 10, Duration.ofMinutes(1), 0, ForkJoinPool.commonPool());

            scheduler.Start();

            verify(_jobScheduler).threadFactory(Group.BOLT_WORKER);
            verify(mockExecutorFactory, times(1)).create(anyInt(), anyInt(), any(typeof(Duration)), anyInt(), anyBoolean(), any(typeof(ThreadFactory)));
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shutdownShouldTerminateThreadPool() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShutdownShouldTerminateThreadPool()
        {
            ExecutorService cachedThreadPool    = Executors.newCachedThreadPool();
            ExecutorFactory mockExecutorFactory = mock(typeof(ExecutorFactory));

            when(mockExecutorFactory.Create(anyInt(), anyInt(), any(), anyInt(), anyBoolean(), any())).thenReturn(cachedThreadPool);
            ExecutorBoltScheduler scheduler = new ExecutorBoltScheduler(CONNECTOR_KEY, mockExecutorFactory, _jobScheduler, _logService, 0, 10, Duration.ofMinutes(1), 0, ForkJoinPool.commonPool());

            scheduler.Start();
            scheduler.Stop();

            assertTrue(cachedThreadPool.Shutdown);
        }
Esempio n. 4
0
        public void CallCreateTest()
        {
            ExecutorFactory factory = new ExecutorFactory(new IocContainer());

            //- 普通
            Assert.IsType <Simple1Executor>(factory.Create(new Simple1Command()).Executor);
            Assert.IsType <Simple2Executor>(factory.Create(new Simple2Command()).Executor);
            Assert.IsType <Simple3Executor>(factory.Create(new Simple3()).Executor);
            //- 泛型
            Assert.IsType <Simple4Executor <int, string> >(factory.Create(new Simple4 <int, string>()).Executor);

            Assert.Throws <NotSupportedException>(() => factory.Create(new Simple4_Error <int, string>()));

            //- 嵌套
            Assert.IsType(Simple5 <int, string> .ExecutorType, factory.Create(new Simple5 <int, string>()).Executor);

            Assert.Throws <NotSupportedException>(() => factory.Create(new Simple5_Error <int, string>()));

            //- 特性
            Assert.IsType <TestSimple6>(factory.Create(new Simple6()).Executor);
            //- 特性泛型
            Assert.IsType <TestSimple7 <string, double> >(factory.Create(new Simple7 <string, double>()).Executor);
        }
Esempio n. 5
0
        private void AddListener(HubConnection connection, CancellationToken token)
        {
            connection.On <JobContext>("Trigger", async context =>
            {
                if (context == null)
                {
                    _logger.LogError("Receive null context.");
                    return;
                }

                var delay = (context.FireTimeUtc - DateTime.UtcNow).TotalSeconds;
                if (delay > 10)
                {
                    _logger.LogError($"Trigger job [{context.Name}, {context.Group}, {context.TraceId}, {context.CurrentSharding}] timeout: {delay}.");
                    await connection.SendAsync("StateChanged", new JobState
                    {
                        JobId    = context.JobId,
                        TraceId  = context.TraceId,
                        Sharding = context.CurrentSharding,
                        State    = State.Exit,
                        Client   = Name,
                        Msg      = "Timeout"
                    }
                                               , token);
                    return;
                }

                try
                {
                    _logger.LogInformation($"Try execute job [{context.Name}, {context.Group}, {context.TraceId}, {context.CurrentSharding}]");

                    await connection.SendAsync("StateChanged", new JobState
                    {
                        JobId    = context.JobId,
                        TraceId  = context.TraceId,
                        Sharding = context.CurrentSharding,
                        Client   = Name,
                        State    = State.Running
                    }, token);

                    var exitCode = await ExecutorFactory.Create(context.Executor).Execute(context,
                                                                                          async(jobId, traceId, msg) =>
                    {
                        await connection.SendAsync("OnLog", new Log {
                            JobId = jobId, TraceId = traceId, Msg = msg
                        },
                                                   token);
                    });

                    await connection.SendAsync("StateChanged", new JobState
                    {
                        JobId    = context.JobId,
                        TraceId  = context.TraceId,
                        Sharding = context.CurrentSharding,
                        Client   = Name,
                        State    = State.Exit,
                        Msg      = $"Exit: {exitCode}"
                    }, token);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, $"Execute job [{context.Name}, {context.Group}] failed.");
                    await connection.SendAsync("StateChanged", new JobState
                    {
                        JobId    = context.JobId,
                        TraceId  = context.TraceId,
                        Sharding = context.CurrentSharding,
                        Client   = Name,
                        State    = State.Exit,
                        Msg      = $"Failed: {ex.Message}"
                    }, token);
                }
            });

            connection.On("Exit", () =>
            {
                Stop();
                _logger.LogInformation("Exit by server.");
                Environment.Exit(0);
            });

            connection.On <string>("Kill", jobId =>
            {
                var keys = ProcessExecutor.Processes.Keys.Where(k => k.JobId == jobId).ToList();

                foreach (var key in keys)
                {
                    ProcessExecutor.Processes.TryGetValue(key, out var proc);
                    if (proc == null)
                    {
                        continue;
                    }
                    try
                    {
                        proc.Kill();
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError($"Kill PID {proc.Id} Job {jobId} failed: {ex.Message}.");
                    }
                }

                foreach (var key in keys)
                {
                    ProcessExecutor.Processes.TryRemove(key, out _);
                }
            });

            connection.On <string, string, int>("Kill", (jobId, traceId, sharding) =>
            {
                var key = new ProcessExecutor.ProcessKey(jobId, traceId, sharding);
                if (!ProcessExecutor.Processes.ContainsKey(key))
                {
                    return;
                }
                ProcessExecutor.Processes.TryGetValue(key, out var proc);
                if (proc == null)
                {
                    return;
                }
                try
                {
                    proc.Kill();
                }
                catch (Exception ex)
                {
                    _logger.LogError($"Kill PID {proc.Id} Job {jobId} failed: {ex.Message}.");
                }
            });
        }