在 Neo4j 容器啟動后,我正在嘗試執行一些用戶操作(更改管理員用戶)。但是我的后臺腳本不會等到 Neo4j 出現并在 Neo4j 上線之前死掉。
entrypoint.sh 類似于
if [some condition]
my_function &
fi
if [${cmd}" == "neo4j" ]; then
exec neo4j console
fi
helper_file.sh 有 my_function
function my_function {
echo "Checking to see if Neo4j has started at http://${DB_HOST}:${DB_PORT}..."
curl --retry-connrefused --retry 5 --retry-max-time 300 http://${DB_HOST}:${DB_PORT}
if [ $? -ne 0 ]; then
echo "Curl failed with error $?. Exiting.."
return 1
fi
migrate_users <--- another function
}
我面臨的問題是 Neo4j 在 curl 進行重試之前不會啟動。
Tue Sep 20 12:46:35 UTC 2022 Checking to see if Neo4j has started at http://localhost:7474...
Tue Sep 20 12:46:35 UTC 2022 % Total % Received % Xferd Average Speed Time Time Time Current
Tue Sep 20 12:46:35 UTC 2022 Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
Tue Sep 20 12:46:35 UTC 2022 curl: (7) Failed to connect to localhost port 7474: Connection refused
Tue Sep 20 12:46:35 UTC 2022 Curl failed with error 0. Exiting..
user: vmanage; command: neo4j
Directories in use:
如何確保在 Neo4j 完全上線后呼叫 migrate_users 函式?
編輯:
感謝您提供建議。
如果我使用后臺行程方法,我會看到 Neo4j 無法啟動,直到 curl 查詢完成
Tue Sep 20 18:57:34 UTC 2022 Checking to see if Neo4j has started
at http://localhost:7474...
Tue Sep 20 18:57:34 UTC 2022 Neo4j not ready
Tue Sep 20 18:57:34 UTC 2022 Connection refused
Tue Sep 20 18:57:34 UTC 2022 config-db is not up, try to setup password again
user: vmanage; command: neo4j
Directories in use:
home: /var/lib/neo4j
config: /var/lib/neo4j/conf
logs: /log
plugins: /var/lib/neo4j/plugins
import: /var/lib/neo4j
data: /data
certificates: /var/lib/neo4j/certificates
licenses: /var/lib/neo4j/licenses
run: /var/lib/neo4j/run
Starting Neo4j.
打算試試這個:https ://github.com/neo4j/docker-neo4j/issues/166#issuecomment-486890785
uj5u.com熱心網友回復:
您可以在腳本中添加一個回圈來檢查 neo4j 容器的運行狀況。如果健康檢查通過僅在您的腳本中繼續進行,否則回圈直到它通過。
uj5u.com熱心網友回復:
您可以使用docker-compose depends_on來condition執行此操作。甚至 docker-compose 檔案也建議實作某種腳本以等待服務啟動。查看以下鏈接docker-compose和stackoverflow
但它可能是這樣的:
version: "2"
services:
neo4j-admin:
build: .
depends_on:
- "neo4j"
command: ["./wait-for-it.sh","--", "sh", "change_admin_passwd.sh"]
neo4j:
image: neo4j
uj5u.com熱心網友回復:
您命名的函式my_function可以使用 until 繼續等待 neo4j 啟動,例如:
function my_function {
let RETRIES=0
declare SUCCESS=0
until [[ $SUCCESS -eq 1 ]] || [[ $RETRIES -eq 50 ]]; do
echo "Checking to see if Neo4j has started at
http://${DB_HOST}:${DB_PORT}..."
STATUS_CODE=$(curl -w %{http_code} -o /dev/null -s http://${DB_HOST}:${DB_PORT})
if [[ $STATUS_CODE -eq 200 ]]; then
echo "Neo4j is up and running" && SUCCESS=1 && exit 0
else
echo "Neo4j not ready" && let RETRIES =1 && sleep 10
fi
done
migrate_users
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/512924.html
