コード例 #1
0
        public async Task <string> HandleAsync(LogEntry log)
        {
            return(await Task.Run(() =>
            {
                if (!string.IsNullOrEmpty(log.CommandType) && log.CommandData != null)
                {
                    var command = _commandSerializer.Deserialize(log.CommandType, log.CommandData);
                    if (command != null && command is GenerateBusinessCodeCommand generateCommand)
                    {
                        var config = _businessCodeConfigManager.Get(generateCommand.ApplicationId, generateCommand.CodeType);
                        if (config == null)
                        {
                            return string.Empty;
                        }

                        var code = _businessCodeCache.Get(generateCommand.ApplicationId, generateCommand.CodeType);
                        code.Generate(generateCommand.CurrentTime ?? DateTime.Now);
                        var generatedCode = $"{config.CodePrefix}{code.Infix}{code.Sequence.ToString().PadLeft(config.SequenceMinLength, '0')}";
                        Console.WriteLine($"generatedCode={generatedCode}");
                        return generatedCode;
                    }
                }
                return string.Empty;
            }));
        }
コード例 #2
0
        public async Task ProcessCommandAsync(HttpContext context)
        {
            try
            {
                string json;
                using (var reader = new StreamReader(context.Request.Body))
                {
                    json = await reader.ReadToEndAsync();
                }

                if (JsonConvert.DeserializeObject(json, _jsonSerializerSettings) is GenerateBusinessCodeCommand command)
                {
                    _logger.LogDebug($"{context.Request.Path} called, my state is {_node.State.GetType().FullName}");
                    if (_node.Role == NodeRole.Leader || command.CurrentTime == null)
                    {
                        var config = _businessCodeConfigManager.Get(command.ApplicationId, command.CodeType);
                        if (config == null)
                        {
                            await context.Response.WriteAsync(JsonConvert.SerializeObject(new ExecuteCommandResponse(null, false, "unable to save business code to state machine")));

                            return;
                        }
                        var code = _businessCodeCache.Get(command.ApplicationId, command.CodeType);
                        command = new GenerateBusinessCodeCommand(command.ApplicationId, command.CodeType, DateTime.Now);
                    }
                    var commandResponse = await _node.ExecuteCommandAsync(command);

                    await context.Response.WriteAsync(JsonConvert.SerializeObject(commandResponse));
                }
                else
                {
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                    await context.Response.WriteAsync("Bad command request.");
                }
            }
            catch (Exception ex)
            {
                context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                await context.Response.WriteAsync(JsonConvert.SerializeObject(ex));

                _logger.LogError($"THERE WAS A PROBLEM ON NODE {_node.Id}", ex);
            }
        }