Skip to content

Conversation

@devadathanmb
Copy link
Owner

@devadathanmb devadathanmb commented Jan 2, 2026

This pull request introduces a new Attachment Delivery Worker to the project, enabling asynchronous file downloads and delivery to users without blocking the bot. The changes span configuration, documentation, Docker setup for all environments, and bot logic refactoring to leverage the new worker. This improves scalability, user experience, and error handling for delivering attachments.

Attachment Delivery Worker Implementation

  • Added a new attachment-delivery-worker service to all Docker Compose files (docker/compose.dev.yaml, docker/compose.staging.yaml, docker/compose.yaml) with appropriate environment variables, health checks, and resource limits. [1] [2] [3] [4] [5] [6] [7]
  • Created environment files for the worker in all environments (env/dev/attachment-delivery-worker.env, env/staging/attachment-delivery-worker.env, env/prod/attachment-delivery-worker.env).
  • Added new npm scripts to package.json for running the worker in dev and production modes.
  • Updated .tool-versions to specify Node.js 20 for compatibility.

Bot Logic Refactoring

  • Refactored inline query attachment delivery in src/bot/composers/inlineQuery/composer.ts to queue attachment delivery jobs via the new worker instead of downloading and sending files directly in the bot process. [1] [2] [3]

Documentation and Architecture Updates

  • Expanded documentation in README.md, CLAUDE.md, and docs/working.md to describe the new worker's responsibilities, architecture, and developer workflow. Added diagrams and updated service/port references. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

These changes collectively improve the system's ability to handle file attachments efficiently and robustly, while keeping the bot responsive for all users.

Copilot AI review requested due to automatic review settings January 2, 2026 19:37
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces a new attachment delivery worker to handle file downloads and delivery asynchronously, preventing the bot from being blocked during large file operations. The implementation follows the established BaseWorker pattern and integrates seamlessly with the existing BullMQ-based worker architecture.

Key changes:

  • New AttachmentDeliveryWorker with concurrency of 2 for async file processing
  • Bot composers refactored to queue attachment jobs instead of synchronous file delivery
  • Extraction of common worker bot creation logic into createWorkerBot utility

Reviewed changes

Copilot reviewed 24 out of 24 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/workers/attachment-delivery/worker.ts Core worker implementation handling file downloads, media group delivery, and error handling
src/workers/attachment-delivery/queue.ts Queue configuration with job interface and helper functions
src/workers/attachment-delivery/startup.ts Worker startup with health checks and monitoring setup
src/workers/attachment-delivery/index.ts Module exports for queue and worker
src/configs/attachmentDeliveryWorker.ts Zod-validated configuration schema for worker health check port
src/bot/utils/createWorkerBot.ts Extracted utility for creating bot instances used by workers
src/workers/broadcasts/worker.ts Updated to use new createWorkerBot utility
src/workers/shared/queueRegistry.ts Registered new attachment delivery queue for Bull Board monitoring
src/workers/index.ts Added export for attachment-delivery module
src/bot/composers/lookups/exam-timetable/composer.ts Refactored to queue attachment jobs instead of direct file delivery
src/bot/composers/lookups/announcements/composer.ts Refactored to queue attachment jobs with batch processing support
src/bot/composers/lookups/academic-calendar/composer.ts Refactored to queue attachment jobs instead of direct file delivery
src/bot/composers/inlineQuery/composer.ts Refactored inline query result handler to use attachment queue
docker/compose.yaml Added attachment-delivery-worker service to production compose
docker/compose.staging.yaml Added attachment-delivery-worker service to staging compose
docker/compose.dev.yaml Added attachment-delivery-worker service to dev compose with hot reload
env/dev/attachment-delivery-worker.env Dev environment configuration for worker health check port
env/staging/attachment-delivery-worker.env Staging environment configuration for worker health check port
env/prod/attachment-delivery-worker.env Production environment configuration for worker health check port
package.json Added npm scripts for running attachment-delivery-worker in dev and prod
docs/working.md Updated architecture documentation with attachment worker details
README.md Added attachment delivery worker to services table and startup commands
CLAUDE.md Updated development guide with attachment worker information
.tool-versions Added Node.js version specification (v20)

- Add missing env file reference in production docker-compose.yaml
- Add missing networks configuration in staging docker-compose.yaml
- Remove redundant totalAttachments field from AttachmentDeliveryJob interface
- Fix Zod schema validation to be consistent with other worker configurations
- Revert incorrect error re-throwing (follow broadcasts worker pattern)

All changes verified with TypeScript compilation and build
- Created src/workers/shared/utils/telegramErrorHandler.ts
- Eliminated ~120 lines of duplicated error handling code
- Reduced BroadcastsWorker from 263 to 185 lines (-78 lines)
- Reduced AttachmentDeliveryWorker from 251 to 196 lines (-55 lines)
- Centralized user blocked/deactivated/rate limit handling
- All quality checks passed (typecheck, lint, build)

No functional changes - pure refactoring for better maintainability
- Added 'ktu-bot-network' definition to docker/compose.staging.yaml
- Fixes error: 'service refers to undefined network ktu-bot-network'
- Network configuration now consistent with dev and prod environments
- Academic Calendar: "Downloading your calendar in the background... This may take a moment!"
- Exam Timetable: "Fetching your timetable in the background... Please wait!"
- Announcements: "Downloading your file(s) in the background... This may take a moment!"
- Inline Query: "Downloading your file(s) from [resource] in the background... Please wait!"

Changes make it clear that processing happens in the background and set realistic expectations
@devadathanmb devadathanmb changed the title feat (WIP): Bring up separate attachment worker to handle file attachments feat: Bring up separate attachment delivery worker to handle file attachments instead of blocking the middleware Jan 3, 2026
@devadathanmb devadathanmb merged commit 41676d7 into grammy-rewrite Jan 3, 2026
5 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.

2 participants