我正在嘗試制作一個腳本來檢查我的測功機是否空閑,如果是,那么它將停止測功機/webapp。唯一的問題是它似乎什么都不做,即使 post 請求會被接受:
from email import header
import requests
import dotenv
heroku_api_token = "Bearer ENTERAPITOKENHERE"
headers = {
'Accept': 'application/vnd.heroku json; version=3',
'Authorization': heroku_api_token
}
statecheck = requests.get("https://api.heroku.com/apps/rockosmodernapp/dynos/web.1",headers=headers).json()
print(statecheck)
if statecheck["state"] == "idle":
print("shutting down web app worker")
elif statecheck["state"] == "up":
print("app is still up, will check in 15 minutes to see if app is idle")
postheader = {
"Content-Type": "application/json",
"Accept": "application/vnd.heroku json; version=3",
'Authorization': heroku_api_token
}
stopdynoobject = {
}
stopdyno = requests.post("https://api.heroku.com/apps/rockosmodernapp/dynos/web.1/actions/stop",headers=postheader)
print(stopdyno.reason)
它看起來會停止,然后立即重新啟動:
2022-10-20T03:05:55.221896 00:00 heroku[web.1]: State changed from up to down
2022-10-20T03:05:56.368472 00:00 heroku[web.1]: Stopping all processes with SIGTERM
2022-10-20T03:05:56.673413 00:00 heroku[web.1]: Process exited with status 143
2022-10-20T03:06:13.858381 00:00 heroku[web.1]: State changed from down to starting
此外,儀表板仍然顯示正在運行的網路作業者:

uj5u.com熱心網友回復:
我想通了 - 使用 Heroku api 中的 FORMATION 端點將網路作業者的數量更改為 0。這將關閉 heroku 中的測功機,從而節省資金。
from email import header
import requests
import json
import dotenv
heroku_api_token = "Bearer ENTERHEROKUAPIKEYHERE"
headers = {
'Accept': 'application/vnd.heroku json; version=3',
'Authorization': heroku_api_token
}
statecheck = requests.get("https://api.heroku.com/apps/rockosmodernapp/dynos/web.1",headers=headers).json()
print(statecheck)
if statecheck["state"] == "idle":
print("shutting down web app worker")
elif statecheck["state"] == "up":
print("app is still up, will check in 15 minutes to see if app is idle")
postheader = {
"Content-Type": "application/json",
"Accept": "application/vnd.heroku json; version=3",
'Authorization': heroku_api_token
}
stopdynoobject = {
"updates" :[{"quantity":0,
"type": "web"}]
}
stopdynoobject_str = json.dumps(stopdynoobject)
turnoffdyno = requests.patch("https://api.heroku.com/apps/rockosmodernapp/formation",headers=headers,data=stopdynoobject_str).json()
print(turnoffdyno)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/519356.html
下一篇:添加列Heroku資料庫
