AdonisJs 4.1:如何创建一个迁移数据库并启动服务器的启动脚本



我有一个码头化的AdonisJs应用程序,我正试图在ECS(AWS(上部署它。我设法部署了映像,但现在我不知道如何在部署时运行迁移。

通过参加乌代米的课程,我发现有人必须做同样的事情,但要用拉拉威尔。在Dockerfile中,他没有运行CMD ['artisan','serve'],而是创建了一个start.sh脚本,启动应用程序,将其放在后台,运行迁移,然后将应用程序放回后台。这是脚本:

#!/bin/sh
# turn on bash's job control
set -m
# Start the primary process and put it in the background
php-fpm &
# Start the helper process
php artisan migrate
# now we bring the primary process back into the foreground
# and leave it there
fg %1

我试着对阿多尼斯做同样的事情,这是我的剧本(许多版本之一(:

#!/bin/sh
# turn on bash's job control
set -m
# Start the primary process and put it in the background
adonis serve &
# Start the helper process
adonis migration:run
# now we bring the primary process back into the foreground
# and leave it there
fg %1

但我总是犯错误。例如:

  1. 服务器启动,但迁移不会运行,因为adonis无法连接到数据库。我不知道如何调试它,因为如果我正常启动应用程序,Adonis可以完美地连接到数据库
  2. (我只在本地尝试过(服务器启动,迁移运行,但服务器进程没有出现在前台,所以应用程序没有真正启动(curl localhost,它给了我curl: (7) Failed to connect to localhost port 80: Connection refused(,我也不能用ctrl+c停止服务器,我必须找到docker容器并停止容器。以下是控制台显示的内容:
SERVER STARTED 
info: serving app on http://0.0.0.0:80
Nothing to migrate

你能帮我创建一个这样做的脚本吗?

第1版:我注意到,即使我创建了一个只有";adonis服务"它仍然不起作用,所以也许这不是通过脚本启动服务器的正确方式?

我需要解决的第一个问题是没有终止的迁移命令。原因是我是如何启动adonis-scheduler程序包的。我是从start/kernel.js文件开始的。现在,我创建了一个scheduler.js(内部启动(,其中包含:

'use strict'
/*
|--------------------------------------------------------------------------
| Run Scheduler
|--------------------------------------------------------------------------
|
| Run the scheduler on boot of the web sever.
|
*/
const Scheduler = use('Adonis/Addons/Scheduler')
Scheduler.run()

并在server.js:中添加了这个

new Ignitor(require('@adonisjs/fold'))
.appRoot(__dirname)
.preLoad('start/scheduler') //code added
.fireHttpServer()
.catch(console.error)

通过这样做,我的迁移总是终止。

我面临的第二个问题是"#/bin/bash";不受支持,所以我需要在"#/bin/sh";总体安排不幸的是,这种格式不支持将作业放在后台,稍后再将它们移到前台,所以我只运行迁移,然后启动服务器。这是文件:

#!/bin/sh
# Start the helper process
adonis key:generate
adonis migration:run --force
# Start the primary process
adonis serve

最新更新