Ejemplo n.º 1
0
        private void PackageService(ServiceProject service, Assembly assembly, string tempRoot, string serviceEntryAssembly, string httpEntryAssembly, string artifactsFolder)
        {
            var plugins = PluginDiscoverer.Discover(assembly);

            if (!plugins.Any())
            {
                return;
            }

            var serviceFolder   = RunPublish(tempRoot, service.ProjectPath, service.Name, false);
            var serviceAssembly = Path.Combine(serviceFolder.Path, Path.GetFileName(assembly.Location));

            var builder = new FunctionBuilder.Builder();

            if (plugins.Any(x => x.ServiceType == ServiceType.MessageHandler))
            {
                using (var buildTargetFolder = new TempDirectory(tempRoot, service.Name + "-service"))
                {
                    var buildContext = new FunctionBuilder.BuildContext(ServiceType.MessageHandler, serviceEntryAssembly, serviceAssembly, buildTargetFolder.Path);
                    builder.Build(buildContext);

                    var packagePath = Path.Combine(artifactsFolder, $"{service.Name}-service.zip");
                    CreateZip(buildTargetFolder, packagePath);
                }
            }

            if (plugins.Any(x => x.ServiceType == ServiceType.Http))
            {
                using (var buildTargetFolder = new TempDirectory(tempRoot, service.Name + "-http"))
                {
                    var buildContext = new FunctionBuilder.BuildContext(ServiceType.Http, httpEntryAssembly, serviceAssembly, buildTargetFolder.Path);
                    builder.Build(buildContext);

                    var packagePath = Path.Combine(artifactsFolder, $"{service.Name}-http.zip");
                    CreateZip(buildTargetFolder, packagePath);
                }
            }
        }
        private void AddService(YamlMappingNode functions, YamlMappingNode resources, ServiceProject service, PluginMetadata plugin)
        {
            var functionName = _namingConventions.GetLambdaName(plugin);

            var env = new YamlMappingNode
            {
                { "ROUTER_QUEUE_NAME", new YamlMappingNode {
                      { "Fn::GetAtt", new YamlSequenceNode("RouterQueue", "QueueName") }
                  } },
                { "ROUTER_FUNCTION_NAME", new YamlMappingNode {
                      { "Ref", "RouterLambdaFunction" }
                  } },
            };

            switch (plugin.ServiceType)
            {
            case ServiceType.MessageHandler:
                var routes = RouteDiscoverer.Discover(plugin);
                if (routes.Any(x => !RouteHandlerFactory.IsRpc(x)))
                {
                    var queueName    = _namingConventions.GetSqsName(plugin);
                    var queueRefName = ToTitleCase(plugin.ServiceKey.Replace('.', '-'));

                    functions.Add(functionName + "-queue", new YamlMappingNode
                    {
                        { "name", functionName + "-queue" },
                        { "handler", "Toxon.Micro.RabbitBlog.Serverless.ServiceEntry::Toxon.Micro.RabbitBlog.Serverless.ServiceEntry.FunctionImpl::HandleQueueAsync" },
                        { "environment", env },
                        { "memorySize", "512" },
                        { "timeout", "60" },
                        { "events", new YamlSequenceNode(BuildSqsEvent(queueRefName)) },
                        {
                            "package", new YamlMappingNode
                            {
                                { "artifact", $"artifacts/{service.Name}-service.zip" },
                            }
                        },
                    });
                    resources.Add(queueRefName, BuildQueue(queueName));
                }

                if (routes.Any(RouteHandlerFactory.IsRpc))
                {
                    functions.Add(functionName, new YamlMappingNode
                    {
                        { "name", functionName },
                        { "handler", "Toxon.Micro.RabbitBlog.Serverless.ServiceEntry::Toxon.Micro.RabbitBlog.Serverless.ServiceEntry.FunctionImpl::HandleDirectAsync" },
                        { "environment", env },
                        { "memorySize", "512" },
                        { "timeout", "60" },
                        {
                            "package", new YamlMappingNode
                            {
                                { "artifact", $"artifacts/{service.Name}-service.zip" },
                            }
                        },
                    });
                }
                break;

            case ServiceType.Http:
                functions.Add(functionName, new YamlMappingNode
                {
                    { "name", functionName },
                    { "handler", "Toxon.Micro.RabbitBlog.Serverless.HttpEntry::Toxon.Micro.RabbitBlog.Serverless.HttpEntry.FunctionImpl::FunctionHandlerAsync" },
                    { "environment", env },
                    { "memorySize", "512" },
                    { "timeout", "60" },
                    { "events", new YamlSequenceNode(BuildHttpEvent()) },
                    {
                        "package", new YamlMappingNode
                        {
                            { "artifact", $"artifacts/{service.Name}-http.zip" },
                        }
                    },
                });
                break;

            default: throw new ArgumentOutOfRangeException();
            }
        }