How to build a maintenance mode page in Python

Mock a service maintenance status page that computes remaining downtime and lists affected features from a simple class.

Easy Python 3.9+ Aug 9, 2026 Production deployment patterns 13 views 0 copies

Python code

38 lines
Python 3.9+
from datetime import datetime

class MaintenanceMode:
    """Mock a maintenance mode status page for a service."""
    
    def __init__(self, service_name: str, scheduled_end: str):
        self.service_name = service_name
        self.scheduled_end = datetime.fromisoformat(scheduled_end)
        self.affected_features = []
    
    def add_affected_feature(self, feature: str):
        self.affected_features.append(feature)
    
    def render_page(self) -> str:
        now = datetime.now()
        remaining = self.scheduled_end - now
        status = "restoring service" if remaining.total_seconds() > 0 else "service available"
        
        header = f"=== {self.service_name.upper()} — {status} ==="
        body = [
            header,
            f"Scheduled maintenance until: {self.scheduled_end:%Y-%m-%d %H:%M}",
            f"Estimated time remaining: {max(0, remaining.total_seconds() // 3600)}h {max(0, (remaining.total_seconds() // 60) % 60)}m",
            self._render_features(),
            "We appreciate your patience while we improve reliability.",
        ]
        return "\n".join(body)
    
    def _render_features(self) -> str:
        if not self.affected_features:
            return "All features available."
        return "Temporarily unavailable:\n- " + "\n- ".join(self.affected_features)

if __name__ == "__main__":
    page = MaintenanceMode("Weather API", "2024-12-31T23:59:00")
    page.add_affected_feature("Real-time forecasts")
    page.add_affected_feature("Historical data retrieval")
    print(page.render_page())

Output

stdout
=== WEATHER API — restoring service ===
Scheduled maintenance until: 2024-12-31 23:59
Estimated time remaining: 138h 12m
Temporarily unavailable:
- Real-time forecasts
- Historical data retrieval
We appreciate your patience while we improve reliability.

How it works

The MaintenanceMode class stores service metadata and computes downtime with datetime arithmetic. datetime.fromisoformat parses the ISO end time, and datetime.now gives the current timestamp. The remaining time is calculated by subtraction, then formatted into hours and minutes with integer division and modulo. The render_page method returns a consistent status string that flips between 'restoring service' and 'service available' based on whether maintenance has ended.

Common mistakes

  • Forgetting that `fromisoformat` requires a strict ISO 8601 string without offsets
  • Using naive datetime comparisons that mix timezone-aware and naive objects
  • Letting negative remaining time show as negative hours or minutes
  • Hardcoding the status instead of deriving it from remaining seconds

Variations

  1. Use `zoneinfo.ZoneInfo` to handle timezones explicitly with `datetime.now(tz)`
  2. Return an HTML string instead of plain text by wrapping the fields in `<div>` tags

Real-world use cases

  • Displaying a temporary status banner on a web app during a scheduled deployment window.
  • Powering an internal uptime dashboard that shows when a microservice will return to service.
  • Feeding a CLI health-check script that blocks releases until maintenance is complete.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Production deployment patterns

Related tutorials and quizzes for this topic.