Skip to content

Commit

Permalink
docker: add controller readiness check
Browse files Browse the repository at this point in the history
  • Loading branch information
saltydk committed Mar 24, 2024
1 parent 4a7a6b3 commit 267d1ad
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
19 changes: 15 additions & 4 deletions roles/docker/tasks/subtasks/controller.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,22 @@
state: restarted
daemon_reload: true

- name: "Controller | Wait for 10 seconds"
ansible.builtin.wait_for:
timeout: "10"
- name: Controller | Wait until Controller is ready
ansible.builtin.uri:
url: "{{ docker_controller_url }}/ping"
method: GET
return_content: yes
status_code: 200,404
register: result
until: >
(result.status == 200 and (result.content | from_json).get('message') == "pong") or
(result.status == 404 and (result.content | from_json).get('detail') == "Not Found")
retries: 12
delay: 5
ignore_errors: yes


- name: Block Docker Controller
- name: Controller | Block Docker Controller
ansible.builtin.uri:
url: "{{ docker_controller_url }}/block/20"
method: POST
Expand Down
2 changes: 1 addition & 1 deletion roles/unionfs/tasks/subtasks/docker/daemon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
ansible.builtin.wait_for:
timeout: 30

- name: Unblock Docker Controller
- name: Docker | Daemon | Unblock Docker Controller
ansible.builtin.uri:
url: "{{ docker_controller_url }}/unblock"
method: POST
Expand Down
10 changes: 10 additions & 0 deletions scripts/saltbox_docker_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

# Global flag to indicate shutdown
shutdown_flag = False
app_ready = False

# Signal handler
def signal_handler(signum, frame):
Expand Down Expand Up @@ -313,6 +314,7 @@ def stop_containers_in_dependency_order(graph: DependencyGraph):
# FastAPI Application and API Endpoints
@asynccontextmanager
async def lifespan(app: FastAPI):
global app_ready
try:
# Initialization
client = docker.from_env()
Expand All @@ -322,6 +324,7 @@ async def lifespan(app: FastAPI):
# Initialize DependencyGraph
global graph # Declare graph as global if it's used elsewhere outside this context
graph = DependencyGraph()
app_ready = True # Indicate that the app is now ready

except Exception as e:
logging.error(f"An error occurred during application initialization: {e}")
Expand All @@ -335,6 +338,13 @@ async def lifespan(app: FastAPI):
unblock_task = None


@app.get("/ping")
async def ping():
if app_ready:
return {"message": "pong"}
raise HTTPException(status_code=503, detail="Application not ready")


@app.post("/start")
async def start_containers():
if is_blocked:
Expand Down

0 comments on commit 267d1ad

Please sign in to comment.