Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [1.0.23] - 2026-11-03

- Keep the Scrapling worker heartbeat refreshed on an independent async task while a browser search is running, so a slow but active search no longer makes `/readyz` falsely report that no healthy worker is available.
- Run the production worker under Docker's init process so cancelled browser subprocesses are reaped cleanly.

## [1.0.22] - 2026-10-29

- Preemptive proxy authentication forwarder: Chromium's Playwright driver omits `Proxy-Authorization` on initial CONNECT requests, causing residential proxy gateways (such as AnyIP) to reject tunnels with `407 Proxy Authentication Required` and hang indefinitely. Added in-process `PreemptiveProxyForwarder` that transparently attaches credentials on upstream CONNECT handshakes.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "google-lens-api",
"version": "1.0.22",
"version": "1.0.23",
"private": true,
"description": "Authenticated Google Lens search API with durable Redis-backed jobs and a Scrapling Chrome worker.",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "google-lens-scrapling-worker"
version = "1.0.22"
version = "1.0.23"
description = "Redis Streams worker for the authenticated Google Lens API"
license = { text = "MIT" }
requires-python = ">=3.12,<3.13"
Expand Down
39 changes: 27 additions & 12 deletions scrapling_worker/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,32 +102,47 @@ async def run(self) -> None:
try:
await self.ensure_group()
await self._check_adapter_ready()
await self._heartbeat()
self.logger.event(
"worker_started",
stream=self.config.stream_name,
group=self.config.group_name,
consumer=self.config.consumer_name,
mode=self.config.mode,
)
while not self._stop_event.is_set():
await self._heartbeat()
await self.reclaim_pending()
raw_entries = await self.redis.xreadgroup(
self.config.group_name,
self.config.consumer_name,
{self.config.stream_name: ">"},
count=1,
block=self.config.block_ms,
)
for entry_id, fields in _flatten_entries(raw_entries):
await self.process_entry(entry_id, fields)
async with asyncio.TaskGroup() as tasks:
tasks.create_task(self._heartbeat_loop())
tasks.create_task(self._consume_jobs())
except asyncio.CancelledError:
self.stop()
raise
finally:
await self.close()
self.logger.event("worker_stopped")

async def _consume_jobs(self) -> None:
while not self._stop_event.is_set():
await self.reclaim_pending()
raw_entries = await self.redis.xreadgroup(
self.config.group_name,
self.config.consumer_name,
{self.config.stream_name: ">"},
count=1,
block=self.config.block_ms,
)
for entry_id, fields in _flatten_entries(raw_entries):
await self.process_entry(entry_id, fields)

async def _heartbeat_loop(self) -> None:
"""Refresh liveness independently of browser job execution."""

refresh_interval = max(0.1, self.config.heartbeat_ttl_seconds / 3)
while not self._stop_event.is_set():
try:
await asyncio.wait_for(self._stop_event.wait(), timeout=refresh_interval)
except TimeoutError:
await self._heartbeat()

async def reclaim_pending(self) -> int:
"""Claim and process idle pending entries, returning the count seen."""

Expand Down
41 changes: 41 additions & 0 deletions tests/test_worker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import json
from contextlib import suppress
from dataclasses import replace
from pathlib import Path
from typing import Any, Mapping

Expand Down Expand Up @@ -248,6 +249,46 @@ async def search(self, job: Any, image_path: Path) -> LensPageSnapshot:
await task


@pytest.mark.asyncio
async def test_heartbeat_refreshes_while_browser_job_is_active(tmp_path: Path) -> None:
started = asyncio.Event()
release = asyncio.Event()

class OneJobRedis(FakeRedis):
def __init__(self) -> None:
super().__init__()
self.sent = False

async def xreadgroup(self, *args: Any, **kwargs: Any) -> list[Any]:
if not self.sent:
self.sent = True
return [("lens:jobs:v1", [("1-0", fields())])]
await asyncio.sleep(0.01)
return []

class WaitingAdapter(FakeAdapter):
async def search(self, job: Any, image_path: Path) -> LensPageSnapshot:
started.set()
await release.wait()
return success_snapshot()

redis = OneJobRedis()
worker_config = replace(config(tmp_path), heartbeat_ttl_seconds=1)
worker = StreamsWorker(redis, worker_config, WaitingAdapter())
task = asyncio.create_task(worker.run())
try:
await asyncio.wait_for(started.wait(), timeout=1)
heartbeat_count = redis.events.count("setex")
await asyncio.sleep(0.5)

assert redis.events.count("setex") > heartbeat_count
assert not release.is_set()
finally:
worker.stop()
release.set()
await asyncio.wait_for(task, timeout=1)


@pytest.mark.asyncio
async def test_expired_attempt_cancellation_does_not_poison_a_retry(tmp_path: Path) -> None:
redis = FakeRedis()
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading