Skip to content

Fix scheduling issues on Zeabur production deployment#10

Merged
agmonetti merged 3 commits intomainfrom
copilot/fix-scheduling-issues-zeabur-again
Oct 18, 2025
Merged

Fix scheduling issues on Zeabur production deployment#10
agmonetti merged 3 commits intomainfrom
copilot/fix-scheduling-issues-zeabur-again

Conversation

Copy link
Contributor

Copilot AI commented Oct 18, 2025

Problem

When deployed on Zeabur at 18:18 (6:18 PM), the bot would execute a few times and then stop working. The bot is configured to run every 1.5 hours from 6 AM to 11 PM (Buenos Aires time), but was failing to maintain this schedule in production.

Looking at the issue data, the last update was at 2025-10-17T08:16:31 even though the bot should have continued running throughout the day. The scheduling system was breaking down after just a few execution cycles.

Root Cause

The sleep calculation logic used integer hour arithmetic that didn't account for minutes and seconds:

# OLD CODE (BUGGY)
if hora_actual < 6:
    horas_hasta_inicio = 6 - hora_actual
else: 
    horas_hasta_inicio = (24 - hora_actual) + 6
time.sleep(horas_hasta_inicio * 3600)

This caused time drift that accumulated over multiple cycles. For example:

  • Bot runs at 23:30 and needs to sleep until 6 AM
  • Old calculation: (24-23)+6 = 7 hours → sleeps until 6:30 AM
  • Correct: Should sleep until exactly 6:00 AM

After multiple cycles, these small errors compounded, causing the bot to drift outside its intended 6 AM - 11 PM operating window and eventually stop executing.

Additionally, the code had a hardcoded 6 instead of using the configurable horario_analisis_inicio, and mixed time.localtime() with time.time() which could cause timezone issues on UTC servers like Zeabur.

Solution

Replaced integer-based calculations with precise datetime timestamp arithmetic:

# NEW CODE (FIXED)
ahora = datetime.now(timezone_local)

if ahora.hour < horario_analisis_inicio:
    # Same day - calculate until start hour
    proxima_ejecucion = ahora.replace(hour=horario_analisis_inicio, minute=0, second=0, microsecond=0)
else: 
    # Next day - calculate until start hour tomorrow
    proxima_ejecucion = (ahora + timedelta(days=1)).replace(hour=horario_analisis_inicio, minute=0, second=0, microsecond=0)

segundos_hasta_inicio = (proxima_ejecucion - ahora).total_seconds()
if segundos_hasta_inicio > 0:
    time.sleep(segundos_hasta_inicio)

Changes

  1. Precise time calculations: Uses full datetime objects with timedelta instead of integer hours
  2. Configuration compliance: Replaced hardcoded 6 with horario_analisis_inicio from config
  3. Timezone consistency: Uses timezone-aware datetime objects throughout (Buenos Aires UTC-3)
  4. Better logging: Shows exact timestamps (HH:MM:SS format) instead of just hours
  5. Defensive programming: Added check to prevent negative sleep times in edge cases

Testing

Comprehensive testing confirms the fix:

  • 36-hour simulation: Bot executes every 1.5 hours during window, sleeps until exactly 6:00 AM
  • Issue scenario: Deployment at 18:18 → runs at 18:18, 19:48, 21:18, 22:48, then sleeps until 06:00 next day
  • Execution frequency: 11.3 per day (matches expected rate)
  • Timezone handling: UTC-3 preserved correctly across all operations
  • No time drift: All executions remain within 6 AM - 11 PM window

Expected Behavior

After deployment, the bot will:

  • Execute every 1.5 hours precisely during 6 AM - 11 PM Buenos Aires time
  • Sleep until exactly 6:00 AM (not drifted times)
  • Continue running indefinitely without stopping
  • Work correctly on UTC servers (Zeabur) thanks to timezone-aware datetime handling

The logs will now show precise timestamps for better monitoring:

Nos encontramos dentro del horario de analisis - Hora actual 18:18:00
Esperando hasta la próxima ejecución (2025-10-17 19:48:00)...

Or when outside the window:

Fuera del horario de análisis. Durmiendo hasta 2025-10-18 06:00:00 (5.70 horas)

Fixes the scheduling issue described where the bot would stop after running a few times.

Original prompt

This section details on the original issue you should resolve

<issue_title>scheduling issues on production (zeabur)</issue_title>
<issue_description>When deployed on Zeabur at 18:18 (6:18 PM), the bot would execute a few times and then stop working. The bot is configured to run from 6 AM to 11 PM (23:00) with a 1.5-hour interval between executions.
The bot configuration in config.py is marked as an integer, I think zeabur does not understand it cause locally that configuration works. Of you see subte_alerta.py the timestamps defined to mark the executions (1.5 hours) of difference works perfectly.
I think you can translate the horario_analisis_inicio and horario_analisis_fin into the same languague timestaps works. I attached files for you to see how is working the system(not good)
image
image

error: por algun motivo se deguto la ejecución:"{
"ultima_actualizacion": "2025-10-17T08:16:31.817681-03:00",
"estados_actuales": {
"Linea A": "Estación Río de Janeiro cerrada por obras de renovación integral.",
"Linea B": "Carlos Gardel y Uruguay cerradas por obras. Viernes y Sábado con horario extendido.",
"Linea C": "Normal",
"Linea D": "Plaza Italia y Agüero cerradas por obras de renovación integral.",
"Linea E": "Normal",
"Linea H": "Hoy horario extendido desde Caseros hasta 1:30 am.",
"Linea Premetro": "Normal"
},
"historial": {
"Linea A_obra": {
"estado": "Estación Río de Janeiro cerrada por obras de renovación integral",
"linea_original": "Linea A",
"tipo": "obra",
"contador": 12,
"primera_deteccion": "2025-10-16T09:45:30.272689-03:00",
"ultima_notificacion": "2025-10-16T09:45:30.272773-03:00",
"es_obra_programada": true,
"detectada_por_texto": true,
"activa": true,
"ya_notificada": true
},
"Linea B_obra": {
"estado": "Carlos Gardel y Uruguay cerradas por obras",
"linea_original": "Linea B",
"tipo": "obra",
"contador": 2,
"primera_deteccion": "2025-10-17T06:46:26.190151-03:00",
"ultima_notificacion": "2025-10-17T08:16:31.817671-03:00",
"es_obra_programada": true,
"detectada_por_texto": true,
"activa": true,
"ya_notificada": true
},
"Linea D_obra": {
"estado": "Plaza Italia y Agüero cerradas por obras de renovación integral",
"linea_original": "Linea D",
"tipo": "obra",
"contador": 8,
"primera_deteccion": "2025-10-16T15:45:54.437706-03:00",
"ultima_notificacion": "2025-10-16T15:45:54.437725-03:00",
"es_obra_programada": true,
"detectada_por_texto": true,
"activa": true,
"ya_notificada": true
},
"Linea B_problema": {
"estado": "Viernes y Sábado con horario extendido",
"linea_original": "Linea B",
"tipo": "problema",
"contador": 2,
"primera_deteccion": "2025-10-17T06:46:26.190163-03:00",
"ultima_notificacion": "2025-10-17T08:16:31.817671-03:00",
"es_obra_programada": false,
"detectada_por_texto": false,
"activa": true,
"ya_notificada": true
},
"Linea H_problema": {
"estado": "Hoy horario extendido desde Caseros hasta 1:30 am",
"linea_original": "Linea H",
"tipo": "problema",
"contador": 2,
"primera_deteccion": "2025-10-17T06:46:26.190205-03:00",
"ultima_notificacion": "2025-10-17T08:16:31.817671-03:00",
"es_obra_programada": false,
"detectada_por_texto": false,
"activa": true,
"ya_notificada": true
}
}
}</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #8


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@vercel
Copy link

vercel bot commented Oct 18, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
bot-subte Ready Ready Preview Comment Oct 18, 2025 1:44am

💡 Enable Vercel Agent with $100 free credit for automated AI reviews

Co-authored-by: agmonetti <179014840+agmonetti@users.noreply.github.com>
Co-authored-by: agmonetti <179014840+agmonetti@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix scheduling issues for bot on Zeabur Fix scheduling issues on Zeabur production deployment Oct 18, 2025
Copilot AI requested a review from agmonetti October 18, 2025 01:48
Copy link
Owner

@agmonetti agmonetti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buena idea

@agmonetti agmonetti marked this pull request as ready for review October 18, 2025 01:51
@agmonetti agmonetti merged commit 76cfe87 into main Oct 18, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

scheduling issues on production (zeabur)

2 participants