0

我正在使用 maven docker 插件启动 postgres 容器,然后在后面的步骤中使用数据库生成一些人工制品。

<plugin>
   …
    <artifactId>docker-maven-plugin</artifactId>
    …
    <configuration>
        <images>
            <image>
                ...
                <name>postgres:11</name>
                ...
            </image>
        </images>
    </configuration>

    <executions>
        <execution>
            <id>start-postgres-container</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>start</goal>
            </goals>
        </execution>
        <execution>
            <id>stop-postgres-container</id>
            <phase>process-sources</phase>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>

 </plugin>

我遇到的问题是,当两者之间的任何操作出现错误时startstopmaven 会离开正在运行的容器,并且连续的构建尝试将失败,因此必须先手动放下剩余的容器。

maven 中有没有办法/插件来指定一些finally动作/阶段?这样在构建脚本失败的情况下,仍然能够释放一些可能已经被保留的资源?

4

1 回答 1

0

可以将类似的解决方案应用于使用 Ryuk 的测试容器所做的事情:https ://github.com/testcontainers/moby-ryuk

要收获的图像应标记为,例如:

<autoRemove>true</autoRemove>
<labels>
   <killme>true</killme>
</labels>
<wait>
   <time>3000</time>
</wait>

上述延迟是为了给 Ryuk 设置一些时间。因为这个必须并行启动...

                        <image>
                            <alias>ryuk-summoned</alias>
                            <name>testcontainers/ryuk:0.3.0</name>
                            <run>
                                <ports>
                                    <port>ryuk.port:8080</port>
                                </ports>
                                <volumes>
                                    <bind>
                                        <volume>/var/run/docker.sock:/var/run/docker.sock</volume>
                                    </bind>
                                </volumes>
                                <autoRemove>true</autoRemove>
                            </run>
                        </image>

挑战在于必须通过包含死亡笔记的 TCP 套接字,每 10 秒至少有 1 次向 Ryuk 容器发送 hartbeat,例如。像这样:printf "label=killme" | nc localhost 8080

这很容易通过下面的 maven exec 插件和setup-ryuk.sh调用上述命令的简单脚本来实现。(注意:这仅提供单个死亡笔记,没有连续的心跳,因此 Ryuk 在接下来的 10 秒内自行死亡 - 届时它将收获所有收到笔记的物品)。

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>stop-postgres</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <target>
                        <exec executable="bash">
                            <arg value="setup-ryuk.sh"/>
                            <arg value="${ryuk.port}"/>
                        </exec>
                    </target>
                </configuration>
            </plugin>

为了使这个平台独立(因为上面对 Linux/Mac 有效)并让 Ryuk 存活更长时间,最好的方法似乎是想出自己的 maven 插件将消息发送到 TCP 套接字。

于 2020-11-19T10:27:37.623 回答