Health check on port 8080 has failed for fastAPI

I am getting this error on fly deployment.

Dockerfile:

# Use the official Python image from the Docker Hub

FROM python:3.9-slim

# Set the working directory in the container

WORKDIR /app

# Copy the current directory contents into the container at /app

COPY . /app

# Install any needed packages specified in requirements.txt

RUN pip install --no-cache-dir -r requirements.txt

ENV PORT=8080

# Make port 8080 available to the world outside this container

EXPOSE 8080

# Run flask app

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080"]

fly.staging.toml:

# fly.toml app configuration file generated for oddience-scripts on 2024-07-28T20:03:16+01:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = 'oddience-scripts'
primary_region = 'lhr'
kill_signal = "SIGINT"
kill_timeout = 60
processes = []

[deploy]
  strategy = "bluegreen"

[build]
  builder = 'paketobuildpacks/builder:base'
  buildpacks = ["gcr.io/paketo-buildpacks/python"]

[env]
  PORT = '8080'

[[services]]
  internal_port = 8080
  processes = ["app"]
  protocol = "tcp"
  script_checks = []
  auto_stop_machines = true
  auto_start_machines = true
  min_machines_running = 1
  
  [[services.http_checks]]
    path = "/"
    interval = "10s"
    timeout = "2s"

  [services.concurrency]
    hard_limit = 5000
    soft_limit = 2500
    type = "requests"

  [[services.ports]]
    force_https = true
    handlers = ["http"]
    port = 80

  [[services.ports]]
    handlers = ["tls", "http"]
    port = 443

  [[services.tcp_checks]]
    grace_period = "1s"
    interval = "15s"
    restart_limit = 0
    timeout = "2s"

main.py

from fastapi import FastAPI, HTTPException
from models import UserData
from data_processing import process_user_data
from apscheduler.schedulers.background import BackgroundScheduler
from config import UPDATE_INTERVALS
from table_update_script import update_table

app = FastAPI()

def job_function():
     print("Scheduler is alive!");
     update_table()

sched = BackgroundScheduler(daemon=True)
sched.add_job(job_function,'interval',hours=int(UPDATE_INTERVALS))
sched.start()

@app.post('/process_user')
def process_user(data: UserData):
    user_id = data.user_id
    social = data.social
    
    if not user_id or not social:
        raise HTTPException(status_code=400, detail="user_id and social are required")
    
    scored_df, post_metrics_df, pricing_df = process_user_data(user_id, social)
    if scored_df is None or post_metrics_df is None or pricing_df is None:
        raise HTTPException(status_code=500, detail="Failed to fetch user data")
    
    return {"message": "Data processed and appended successfully"}

if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app, host='0.0.0.0', port=8080)

Error:

It looks like you check is hitting the / route, but your app only exposes /process_user

According to the docs the healthcheck wants a 200 response, it’s likely getting a 404 and is therefore not satisfied :slight_smile:

I added this

@app.post('/healthcheck ')
def health_check(data: UserData):
    return {"message": "Status okay"}

and added this to my fly.staging.toml


  [[services.http_checks]]
    method = "get"
    path = "/healthcheck"

I am still getting same error @donatas

the route you’ve defined is an app.post, but the method in your services.http_checks section is a get :wink:

Same error. I wonder why I am still getting that error.

ANy suggestions @donatas

any chance you can share the repo? :slight_smile:

It is a private repo.

It is just three files , main file , fly.staging.toml and github yml @donatas

How come you have 2 checks (http & tcp) - drop tcp for now.
You’re forcing https, try changing the protocol to https in your http check, or set it to false to see if it passes.

so this is my fly.staging.toml

# fly.toml app configuration file generated for oddience-scripts on 2024-07-28T20:03:16+01:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = 'oddience-scripts'
primary_region = 'lhr'
kill_signal = "SIGINT"
kill_timeout = 60
processes = []

[deploy]
  strategy = "bluegreen"

[build]
  builder = 'paketobuildpacks/builder:base'
  buildpacks = ["gcr.io/paketo-buildpacks/python"]

[env]
  PORT = '8080'

[[services]]
  internal_port = 8080
  processes = ["app"]
  protocol = "https"
  script_checks = []
  auto_stop_machines = true
  auto_start_machines = true
  min_machines_running = 1
  
  [[services.http_checks]]
    method = "get"
    path = "/healthcheck"
    interval = "10s"
    timeout = "2s"

  [services.concurrency]
    hard_limit = 5000
    soft_limit = 2500
    type = "requests"

  [[services.ports]]
    force_https = true
    handlers = ["http"]
    port = 80

  [[services.ports]]
    handlers = ["tls", "http"]
    port = 443

any suggestion @khuezy

Did you read the suggestion in my first message?

I updated it. That is what I posted above. I am getting error Services [0] Protocol is not included in the list.

The protocol on the health_check, not on the services.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.