midwayjs / midway

🍔 A Node.js Serverless Framework for front-end/full-stack developers. Build the application for next decade. Works on AWS, Alibaba Cloud, Tencent Cloud and traditional VM/Container. Super easy integrate with React and Vue. 🌈
https://www.midwayjs.org/
MIT License
7.4k stars 576 forks source link

[BUG]:midway-bin package 构建 egg 项目,发布之后运行失败 #912

Closed mowatermelon closed 3 years ago

mowatermelon commented 3 years ago

基础信息

项目问题

技术栈说明

middway + typescript + egg + typeorm + swagger

基础描述

egg 项目,发布到线上之后,函数调用异常。

按照官网说法eggjs & mw2migrate_egg,在 @Controller('/test')注入之后,使用默认的 f.yml,就会自动生成 function 信息,但是并没有。

复现步骤

期望结果

image

四个接口能被正常读取,同时正常可访问。


更多内容说明

https://github.com/mowatermelon/mwegg/blob/main/BUG.md

czy88840616 commented 3 years ago

在根目录做 deploy 操作,不需要进入 .serverless 目录,这个是部署的临时目录。

czy88840616 commented 3 years ago

由于代码里写的数据库是 127,启动连不上,我注释了之后的效果。

http://45557715-125087.test.functioncompute.com/swagger-ui/index.html

mowatermelon commented 3 years ago

在根目录做 deploy 操作,不需要进入 .serverless 目录,这个是部署的临时目录。

不知道方便说明一下,你说的 deploy 是通过 fun deploy 还是 midway-bin deploy???

我都是先执行了npm run build --> midway-bin build -c && midway-bin package --skipZip

midway-bin deploy

Collecting your services information, in order to caculate devlopment changes...

Resources Changes(Beta version! Only FC resources changes will be displayed):

┌───────────┬──────────────────────────────┬────────┬──────────┐
│ Resource  │ ResourceType                 │ Action │ Property │
├───────────┼──────────────────────────────┼────────┼──────────┤
│ app_index │ Aliyun::Serverless::Function │ Modify │ CodeUri  │
└───────────┴──────────────────────────────┴────────┴──────────┘

? Please confirm to continue. Yes
Waiting for service midway-egg to be deployed...
        Waiting for function app_index to be deployed...
                Waiting for packaging function app_index code...
                The function app_index has been packaged. A total of 11336 files were compressed and the final size was 21.36 MB
                Waiting for HTTP trigger http-app_index to be deployed...
                triggerName: http-app_index
                methods: [ 'GET', 'PUT', 'POST', 'DELETE', 'HEAD' ]
                trigger http-app_index deploy success
        function app_index deploy success
service midway-egg deploy success

Detect 'DomainName:Auto' of custom domain 'midway_auto_domain'
Request a new temporary domain ...
The assigned temporary domain is http://45601818-1716548898343343.test.functioncompute.com,expired at 2021-03-23 10:16:58, limited by 1000 per day.
Waiting for custom domain midway_auto_domain to be deployed...
        retry 1 times
custom domain midway_auto_domain deploy success

Deploy success

发现构建的 f.yml 还是如下内容,没有看到我的 test 相关的函数信息

service:
  name: midway-egg
provider:
  name: aliyun
  initTimeout: 10
deployType: egg
package:
  include:
    - dist
functions:
  app_index:
    handler: index.handler
    events:
      - http:
          path: /*
globalDependencies:
  '@midwayjs/simple-lock': '*'
layers:
  eggLayer:
    path: 'npm:@midwayjs/egg-layer'

然后我访问地址

http://45601818-1716548898343343.test.functioncompute.com

{
errorMessage: "Handler 'initializer' is missing on module 'index'",
errorType: "Error",
stackTrace: [
"Error: Handler 'initializer' is missing on module 'index'"
]
}

fun deploy

我在下项目根目录做 通过 fun deploy ,提示说这个不是一个 fun project,然后我做了一下尝试。

Fun detected that your function mwegg/mwegg sizes exceed 100M. It is recommended that using the nas service to manage your function dependencies.
? Do you want to let fun to help you automate the configuration? Yes
? We recommend using the 'NasConfig: Auto' configuration to manage your function dependencies. Yes

Fun automatically backups the original template.yml file to /xx/mwegg/.template.yml.backup
Fun add 'NasConfig: Auto' configuration to /xx/mwegg/template.yml
Fun add node_modules to /xx/mwegg/.nas.yml
Fun add environment variables to 'mwegg/mwegg' in /xx/mwegg/template.yml

Fun will execute the ‘fun package’ again.

/xx/mwegg has been uploaded to OSS. objectName: xxxxxxxxxxxx.
A total of 188 files files were compressed and the final size was 183.53 KB

/xx/mwegg/.fun/nas/auto-default/mwegg is empty directory, skiping.

/xx/mwegg/node_modules has been uploaded to OSS. objectName: xxxxxxxx. A total of 37294 files files were compressed and the final size was 141.75 MB

/xx/mwegg/node_modules/@alicloud/fun/lib/utils/fun-nas-server/dist/fun-nas-server.zip has been uploaded to OSS. objectName: xxxxxx.

Package success
Collecting your services information, in order to caculate devlopment changes...

Resources Changes(Beta version! Only FC resources changes will be displayed):

┌───────────┬──────────────────────────────┬────────┬──────────────────────┐
│ Resource  │ ResourceType                 │ Action │ Property             │
├───────────┼──────────────────────────────┼────────┼──────────────────────┤
│           │                              │        │ Description          │
│           │                              │        ├──────────────────────┤
│ mwegg     │ Aliyun::Serverless::Service  │ Add    │ NasConfig            │
│           │                              │        ├──────────────────────┤
│           │                              │        │ VpcConfig            │
├───────────┼──────────────────────────────┼────────┼──────────────────────┤
│           │                              │        │ Handler              │
│           │                              │        ├──────────────────────┤
│           │                              │        │ Runtime              │
│ mwegg     │ Aliyun::Serverless::Function │ Add    ├──────────────────────┤
│           │                              │        │ CodeUri              │
│           │                              │        ├──────────────────────┤
│           │                              │        │ EnvironmentVariables │
├───────────┼──────────────────────────────┼────────┼──────────────────────┤
│           │                              │        │ Description          │
│           │                              │        ├──────────────────────┤
│           │                              │        │ Policies             │
│ Nas       │ Aliyun::Serverless::Service  │ Add    ├──────────────────────┤
│           │                              │        │ VpcConfig            │
│           │                              │        ├──────────────────────┤
│           │                              │        │ NasConfig            │
├───────────┼──────────────────────────────┼────────┼──────────────────────┤
│           │                              │        │ Handler              │
│           │                              │        ├──────────────────────┤
│           │                              │        │ Runtime              │
│           │                              │        ├──────────────────────┤
│ NasCpFunc │ Aliyun::Serverless::Function │ Add    │ CodeUri              │
│           │                              │        ├──────────────────────┤
│           │                              │        │ MemorySize           │
│           │                              │        ├──────────────────────┤
│           │                              │        │ Timeout              │
└───────────┴──────────────────────────────┴────────┴──────────────────────┘

Waiting for service mwegg to be deployed...
        make sure role 'aliyunfcgeneratedrole-cn-shanghai-mwegg' is exist
        role 'aliyunfcgeneratedrole-cn-shanghai-mwegg' is already exist
        attaching police 'AliyunECSNetworkInterfaceManagementAccess' to role: aliyunfcgeneratedrole-cn-shanghai-mwegg
        attached police 'AliyunECSNetworkInterfaceManagementAccess' to role: aliyunfcgeneratedrole-cn-shanghai-mwegg
        retry 1 times
        retry 2 times
        retry 3 times
        retry 4 times
        retry 5 times
        retry 6 times
        retry 7 times
        retry 8 times
        retry 9 times
czy88840616 commented 3 years ago

直接使用midway-bin deploy,内部已经包装了fun,不需要其他操作

image

mowatermelon commented 3 years ago

直接使用midway-bin deploy,内部已经包装了fun,不需要其他操作

我使用了 midway-bin build && midway-bin package & midway-bin deploy 为何 部署的项目还是没有你的那个效果?

czy88840616 commented 3 years ago

deploy包含了build和package,不需要单独执行。

文档:https://www.yuque.com/midwayjs/midway_v2/migrate_egg

附上文件。

mwegg.zip

mowatermelon commented 3 years ago
import { Configuration } from '@midwayjs/decorator';
import * as orm from '@midwayjs/orm';
import * as swagger from '@midwayjs/swagger';

@Configuration({
  imports: [
    orm, // 加载 orm 组件
    {
      component: swagger, // 加载 swagger 组件,
      enabledEnvironment: ['local'],
    },
  ],
})
export class ContainerConfiguratin {}

发布包含 typeorm 的项目,已经配置了在线数据库地址,尝试最小重现demo ,但是小 demo 中一直都是成功的,不太清楚为何实际项目发布会提示这个

 [34m[egg:logger] init all loggers with options: {"dir":"/tmp","encoding":"utf8","env":"prod","level":"INFO","consoleLevel":"INFO","disableConsoleAfterReady":false,"outputJSON":false,"buffer":true,"appLogName":"midway-web.log","coreLogName":"midway-core.log","agentLogName":"midway-agent.log","errorLogName":"common-error.log","coreLogger":{},"allowDebugAtProd":false,"type":"application"}
2021-05-21 09:37:30,407 INFO 9 [egg:core] dump config after load, 7ms
2021-05-21 09:37:30,448 INFO 9 [egg-multipart] stream mode enable
2021-05-21 09:37:30,527 INFO 9 Use coreMiddleware order: []
2021-05-21 09:37:30,527 INFO 9 Use appMiddleware order: []
2021-05-21 09:37:30,527 INFO 9 [egg:loader] Loaded middleware from ["/code/node_modules/_egg-session@3.3.0@egg-session/app/middleware","/code/node_modules/_egg-security@2.9.0@egg-security/app/middleware","/code/node_modules/_egg-jsonp@2.0.0@egg-jsonp/app/middleware","/code/node_modules/_egg-onerror@2.1.0@egg-onerror/app/middleware","/code/node_modules/_egg-multipart@2.11.1@egg-multipart/app/middleware","/code/node_modules/_egg-view@2.1.3@egg-view/app/middleware","/code/node_modules/_midway-schedule@2.10.14@midway-schedule/app/middleware","/code/node_modules/_egg-cors@2.2.3@egg-cors/app/middleware","/code/node_modules/_egg@2.29.4@egg/app/middleware","/code/node_modules/_@midwayjs_web@2.10.14@@midwayjs/web/dist/app/middleware","/code/node_modules/_@midwayjs_web@2.10.14@@midwayjs/web/app/middleware","/code/node_modules/_@midwayjs_egg-layer@2.10.14@@midwayjs/egg-layer/framework/app/middleware","/code/dist/app/middleware"]
2021-05-21 09:37:30,528 INFO 9 [egg:loader] Controller loaded: /code/dist/app/controller
2021-05-21 09:37:30,529 INFO 9 [egg:core:ready_stat] end ready task /code/node_modules/_@midwayjs_web@2.10.14@@midwayjs/web/dist/base.js:118:26, remain ["/code/node_modules/_@midwayjs_web@2.10.14@@midwayjs/web/app.js:didLoad"]
2021-05-21 09:37:30,529 INFO 9 [egg:core:ready_stat] end ready task /code/node_modules/_@midwayjs_web@2.10.14@@midwayjs/web/app.js:didLoad, remain []
2021-05-21 09:37:30,713 ERROR 9 [-/127.0.0.1/-/0ms GET /] nodejs.SyntaxError: Unexpected token {
(function (exports, require, module, __filename, __dirname) { import { MigrationInterface, QueryRunner } from 'typeorm'
                                                                     ^

SyntaxError: Unexpected token {
    at new Script (vm.js:80:7)
    at createScript (vm.js:274:10)
    at Object.runInThisContext (vm.js:326:10)
    at Module._compile (internal/modules/cjs/loader.js:664:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
mowatermelon commented 3 years ago

核心问题是 entities: ['src/db/entity/index.ts'], 设置没有生效,没有作用

问题已经解决,将 entity 文件夹 移到 src 下, 同时删除了 run && logs 文件,就好了

请一定要注意,config.prod.ts 中不能配置 migrations 和 cli 属性

czy88840616 commented 3 years ago

@mowatermelon 如果使用 @midwayjs/orm ,并不需要 entities 字段,midway 会自动扫描。其次,由于你写了 entities,里面指定了 ts 文件,导致在部署后只有 js 环境的场景下,typeorm 依旧会去加载 ts 文件,所以出现了你的错误。