Actualizar su proceso de resolución de CAPTCHA no debería significar tiempo de inactividad. Las implementaciones azul-verde le permiten ejecutar dos entornos idénticos: cambiar el tráfico a la nueva versión, verificar que funciona y retroceder instantáneamente si algo falla.
Arquitectura azul-verde
┌─────────────────────┐
[Scraper Clients] → │ Traffic Router │
└──────┬──────┬───────┘
│ │
Active│ │Standby
▼ ▼
┌───────┐ ┌───────┐
│ BLUE │ │ GREEN │
│Workers│ │Workers│
└───┬───┘ └───┬───┘
│ │
└────┬─────┘
▼
[CaptchaAI API]
## Runbook mínimo de corte de tráfico
| Fase | Qué validar | Señal de rollback |
|---|---|---|
| Antes del corte | Misma versión de config, proxies, secretos y límites en azul y verde | Diferencias entre variables o rutas de checkout |
| Canary 5-10% | Latencia, `CAPCHA_NOT_READY`, tasa de error y consumo por worker | Error rate > línea base + 2% |
| Corte total | El entorno verde mantiene la carga al menos un ciclo completo de tráfico | Picos de timeout, saldo inconsistente o errores por sesión |
| Post-corte | Azul queda listo para rollback con colas drenadas y métricas vivas | Imposibilidad de volver atrás en menos de 5 minutos |
Implementación
Python: enrutador azul-verde
import os
import time
import threading
import requests
API_KEY = os.environ["CAPTCHAAI_API_KEY"]
class CaptchaWorkerPool:
"""Represents one environment (blue or green)."""
def __init__(self, name, config):
self.name = name
self.config = config
self.session = requests.Session()
self.tasks_solved = 0
self.errors = 0
self.healthy = True
def solve(self, task):
resp = self.session.post("https://ocr.captchaai.com/in.php", data={
"key": API_KEY,
"method": task.get("method", "userrecaptcha"),
"googlekey": task["sitekey"],
"pageurl": task["pageurl"],
"json": 1
})
data = resp.json()
if data.get("status") != 1:
self.errors += 1
return {"error": data.get("request")}
captcha_id = data["request"]
for _ in range(60):
time.sleep(5)
result = self.session.get(
"https://ocr.captchaai.com/res.php",
params={
"key": API_KEY,
"action": "get",
"id": captcha_id,
"json": 1
}
).json()
if result.get("status") == 1:
self.tasks_solved += 1
return {"solution": result["request"]}
if result.get("request") != "CAPCHA_NOT_READY":
self.errors += 1
return {"error": result.get("request")}
self.errors += 1
return {"error": "TIMEOUT"}
@property
def error_rate(self):
total = self.tasks_solved + self.errors
return self.errors / total if total > 0 else 0.0
@property
def stats(self):
return {
"name": self.name,
"solved": self.tasks_solved,
"errors": self.errors,
"error_rate": round(self.error_rate, 4),
"healthy": self.healthy
}
class BlueGreenRouter:
def __init__(self, blue_config, green_config):
self.blue = CaptchaWorkerPool("blue", blue_config)
self.green = CaptchaWorkerPool("green", green_config)
self.active = self.blue
self.standby = self.green
self.lock = threading.Lock()
def solve(self, task):
"""Route task to the active environment."""
with self.lock:
pool = self.active
return pool.solve(task)
def switch(self):
"""Swap active and standby environments."""
with self.lock:
self.active, self.standby = self.standby, self.active
print(f"Switched: {self.active.name} is now ACTIVE")
return self.active.name
def rollback(self):
"""Switch back to the previous environment."""
return self.switch()
def canary_test(self, test_tasks, threshold=0.9):
"""Run test tasks on standby before switching."""
successes = 0
for task in test_tasks:
result = self.standby.solve(task)
if "solution" in result:
successes += 1
success_rate = successes / len(test_tasks) if test_tasks else 0
passed = success_rate >= threshold
print(
f"Canary test: {successes}/{len(test_tasks)} "
f"({success_rate:.0%}) — {'PASS' if passed else 'FAIL'}"
)
return passed
@property
def status(self):
return {
"active": self.active.stats,
"standby": self.standby.stats
}
# Usage
router = BlueGreenRouter(
blue_config={"version": "1.2.0", "workers": 4},
green_config={"version": "1.3.0", "workers": 4}
)
# Canary test before switching
test_tasks = [
{"sitekey": "6Le-wvkS...", "pageurl": "https://example.com/test"}
]
if router.canary_test(test_tasks, threshold=0.8):
router.switch()
print(f"Now active: {router.status['active']['name']}")
else:
print("Canary failed — staying on current environment")
JavaScript: conmutador azul-verde automatizado
const axios = require("axios");
const API_KEY = process.env.CAPTCHAAI_API_KEY;
class BlueGreenDeployment {
constructor() {
this.environments = {
blue: { name: "blue", version: null, solved: 0, errors: 0 },
green: { name: "green", version: null, solved: 0, errors: 0 },
};
this.activeEnv = "blue";
}
get active() {
return this.environments[this.activeEnv];
}
get standby() {
return this.environments[this.activeEnv === "blue" ? "green" : "blue"];
}
async deploy(version, config = {}) {
const target = this.standby;
target.version = version;
target.solved = 0;
target.errors = 0;
console.log(`Deployed v${version} to ${target.name} (standby)`);
// Run canary checks
const canaryPassed = await this.canaryCheck(config.canaryTasks || []);
if (!canaryPassed && config.canaryTasks?.length > 0) {
console.log("Canary check failed — aborting deployment");
return { success: false, reason: "canary_failed" };
}
// Switch traffic
this.activeEnv = target.name;
console.log(`Switched traffic to ${target.name} (v${version})`);
// Monitor for rollback
if (config.monitorDuration) {
const stable = await this.monitorAfterSwitch(config.monitorDuration);
if (!stable) {
this.rollback();
return { success: false, reason: "post_deploy_errors" };
}
}
return { success: true, active: this.activeEnv };
}
async canaryCheck(tasks) {
if (tasks.length === 0) return true;
let successes = 0;
for (const task of tasks) {
try {
await this.solveCaptcha(task);
successes++;
} catch (err) {
console.log(`Canary task failed: ${err.message}`);
}
}
const rate = successes / tasks.length;
console.log(`Canary: ${successes}/${tasks.length} (${(rate * 100).toFixed(0)}%)`);
return rate >= 0.8;
}
async monitorAfterSwitch(durationMs) {
const start = Date.now();
const checkInterval = 10000;
while (Date.now() - start < durationMs) {
await new Promise((r) => setTimeout(r, checkInterval));
const errorRate = this.active.errors /
Math.max(1, this.active.solved + this.active.errors);
if (errorRate > 0.2) {
console.log(`Error rate ${(errorRate * 100).toFixed(1)}% — triggering rollback`);
return false;
}
}
return true;
}
rollback() {
const previous = this.activeEnv === "blue" ? "green" : "blue";
console.log(`Rolling back: ${this.activeEnv} → ${previous}`);
this.activeEnv = previous === "blue" ? "blue" : "green";
}
async solveCaptcha(task) {
const submitResp = await axios.post("https://ocr.captchaai.com/in.php", null, {
params: {
key: API_KEY,
method: "userrecaptcha",
googlekey: task.sitekey,
pageurl: task.pageurl,
json: 1,
},
});
if (submitResp.data.status !== 1) {
this.active.errors++;
throw new Error(submitResp.data.request);
}
const captchaId = submitResp.data.request;
for (let i = 0; i < 60; i++) {
await new Promise((r) => setTimeout(r, 5000));
const pollResp = await axios.get("https://ocr.captchaai.com/res.php", {
params: { key: API_KEY, action: "get", id: captchaId, json: 1 },
});
if (pollResp.data.status === 1) {
this.active.solved++;
return pollResp.data.request;
}
if (pollResp.data.request !== "CAPCHA_NOT_READY") {
this.active.errors++;
throw new Error(pollResp.data.request);
}
}
this.active.errors++;
throw new Error("TIMEOUT");
}
}
// Deploy new version with canary and monitoring
const deployer = new BlueGreenDeployment();
deployer
.deploy("1.3.0", {
canaryTasks: [
{ sitekey: "6Le-wvkS...", pageurl: "https://example.com/test" },
],
monitorDuration: 60000, // Monitor for 1 minute after switch
})
.then((result) => console.log("Deploy result:", result));
Flujo de trabajo de implementación
| paso | acción | Activador de reversión |
|---|---|---|
| 1 | Implementar nuevo código en espera | Fallo de construcción |
| 2 | Ejecute pruebas canary en espera | Tasa de éxito <80% |
| 3 | Cambiar el tráfico a la nueva versión | - |
| 4 | Supervisar la tasa de error (5 min) | Tasa de error > 20% |
| 5 | Desmantelar el antiguo entorno | - |
Solución de problemas
| Problema | causa | Solución |
|---|---|---|
| Canarias pasa pero la producción falla | Tareas de prueba demasiado simples | Utilice tareas realistas de la cola de producción |
| Retrocesos frecuentes | Umbrales de seguimiento agresivos | Aumentar el umbral de error; período de remojo más largo |
| La división del tráfico no está limpia durante el cambio | Solicitudes en vuelo sobre el entorno antiguo | Espere a que se agoten las tareas en vuelo antes del desmantelamiento |
| Ambos entornos se vuelven insalubres | Fallo de dependencia compartida (red, API) | disyuntor; no retroceder por problemas de infraestructura |
Preguntas frecuentes
¿Cuánto tiempo deben durar las pruebas canary?
Ejecute al menos 10 soluciones CAPTCHA reales en el entorno de espera. Para sistemas críticos, ejecute un porcentaje del tráfico de producción (5-10 %) a través del modo de espera durante 10 minutos antes del cambio completo.
¿Puedo hacer azul-verde con un solo servidor?
Sí: ejecute azul y verde como procesos o contenedores separados en el mismo host. Utilice un proxy inverso (NGINX) para cambiar el tráfico entre puertos.
¿Cuál es la diferencia entre la implementación azul-verde y canaria?
Azul-verde cambia el 100% del tráfico a la vez. Canary aumenta gradualmente el tráfico a la nueva versión (1% → 10% → 50% → 100%). El azul verdoso es más sencillo; canary es más seguro para sistemas a gran escala.
Próximos pasos
Despliega con confianza: obtén tu API key de CaptchaAI y configura despliegues sin tiempo de inactividad.
Guías relacionadas:
- Arquitectura multirregional
- Alta disponibilidad con failover
- Solución en contenedores Docker