【问题标题】:Deploying python script that uses Firebase to Heroku将使用 Firebase 的 python 脚本部署到 Heroku
【发布时间】:2021-01-12 17:58:10
【问题描述】:

我有一个 python 脚本,它使用 selenium 抓取网页并将数据发布到 Firebase 实时数据库。此脚本使用计划库每 X 小时运行一次。

在没有 Firebase 的情况下部署脚本,我没有遇到任何错误。使用 Firebase 代码部署脚本时,我在运行 heroku logs --tail 时收到以下消息:

2020-09-23T18:48:35.044206+00:00 heroku[web.1]:进程退出 状态 137 2020-09-23T18:48:35.091567+00:00 heroku[web.1]: 状态 从开始更改为崩溃 2020-09-24T00:26:53.135632+00:00 heroku[web.1]:状态从崩溃变为开始 2020-09-24T00:27:07.260119+00:00 heroku[web.1]:启动过程 命令python scraper.py 2020-09-24T00:28:07.677644+00:00 heroku[web.1]:错误 R10(启动超时)-> Web 进程绑定失败 在发射后 60 秒内到达 $PORT

在本地,该应用可与 Firebase 完美配合!

脚本:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
import schedule
import time
import os
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db

cred = credentials.Certificate('secret-file.json')

firebase_admin.initialize_app(cred, {
    'databaseURL' : 'https://a-database-url.com/'
})

# Use Chrome in silent mode
op = webdriver.ChromeOptions()
op.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
op.add_argument("--headless") 
op.add_argument("--no-sandbox") 
op.add_argument("--disable-dev-sh-usage") 


def job():
    PATH = "C:\Program Files (x86)\chromedriver.exe"

    # Heroku driver
    driver = webdriver.Chrome(executable_path= os.environ.get("CHROMEDRIVER_PATH"), chrome_options=op)

    driver.get("https://a-website.com")
    print("Opening the browser")

    try:
        # Magic..

        time.sleep(1)

        # Magic..

        time.sleep(3)

        # More Magic..

        driver.quit()
    except:
        driver.quit()



schedule.every(5).minutes.do(job)

while True:
    schedule.run_pending()
    time.sleep(1) # wait one minute

【问题讨论】:

  • “Web 进程未能在 60 秒内绑定到 $PORT”——您的脚本是否旨在侦听 HTTP / HTTPS 请求?
  • 嗨,克里斯,不是特别是,还是您的意思是 driver.get(..url..) ?我将添加我的脚本

标签: python firebase heroku


【解决方案1】:

您收到以下错误:

Web 进程未能在 60 秒内绑定到 $PORT

但听起来您不打算运行一个网络进程(一个监听 HTTP 请求的进程)。

Heroku 上的进程在名为Procfile 的文件中定义,该文件位于项目的顶级目录中。您可能只需要 worker 而不是 web 进程,例如

worker: python scraper.py

如果您已有Procfile,请将其从web: 更改为worker:,如上所示。如果您没有,请创建包含上述内容的文件。

【讨论】:

  • 更改了它,将其推送到 Heroku 并打开了 worker。没有奇怪的输出,也没有刮板运行的迹象
  • 我似乎得到了一些输出打印行。不是我所期望的,看起来硒部分没有按预期工作
  • @Rafael,这是一个单独的问题,应该单独处理。请考虑接受此答案,然后根据您看到的任何输出搜索现有问题,如果您找不到解决问题的现有答案,请提出另一个问题。确保包括你得到的实际输出;我们不能用“一些我没想到的输出”。
  • @Rafael,并确保使用heroku logs 检查您的运行时日志。如果刮板没有运行,那可能仍然在此处的范围内。但如果它正在运行并且没有按照您的预期运行,那就另当别论了。
  • 我会接受这个问题,谢谢。如果找到该元素,它将在本地打印,但在 Heroku 上,它不会像本地一样打印它应该打印的行。..
猜你喜欢
  • 2020-10-29
  • 1970-01-01
  • 2021-06-14
  • 1970-01-01
  • 1970-01-01
  • 2020-12-21
  • 1970-01-01
  • 1970-01-01
  • 2018-03-03
相关资源
最近更新 更多