Skip to content

Fix concurrency limiter and add sync service tests - #8

Closed
AlecKriebel wants to merge 2 commits into
devfrom
task/41189058-implement-cloudflare-worker-cr
Closed

Fix concurrency limiter and add sync service tests#8
AlecKriebel wants to merge 2 commits into
devfrom
task/41189058-implement-cloudflare-worker-cr

Conversation

@AlecKriebel

Copy link
Copy Markdown
Contributor

Summary

  • Fixed a bug in the processWithConcurrency function in the cron handler where the logic for detecting completed promises was incorrect (Promise.resolve always wins the race, causing all promises to be marked as "pending")
  • Replaced array-based tracking with Set-based self-removal pattern for cleaner concurrency management
  • Added comprehensive test suite for SyncService with 8 new tests
  • Fixed a TypeScript error in the google-calendar test file

Changes

  • packages/worker/src/cron/index.ts - Fixed concurrency limiter implementation
  • packages/worker/src/__tests__/sync-service.test.ts - New test file for SyncService
  • packages/worker/src/__tests__/google-calendar.test.ts - Fixed type casting for fetch mock

Test plan

  • All 25 tests pass
  • TypeScript compilation succeeds
  • Cron handler correctly limits concurrent sync processing

Task Reference

Task ID: #41189058 - Implement Cloudflare Worker cron for scheduled syncs

🤖 Generated with Claude Code

AlecKriebel and others added 2 commits January 28, 2026 07:37
This commit implements the scheduled sync functionality for CalendarSync:

Database Schema:
- Add syncConfigs table to store sync configuration (calendar, destination, schedule)
- Add syncHistory table to track sync run results
- Add oauthCredentials table to store Google OAuth tokens
- Add enums for sync status, frequency, destination type, and run status

Worker Cron Implementation:
- Cron runs every 15 minutes to process due syncs
- Queries syncConfigs where next_sync_at <= now and status is active
- Processes syncs in parallel with concurrency limit (5 at a time)
- Limits to 50 syncs per cron run to prevent timeout

Google Calendar Integration:
- Full sync on first run (fetch events from now onwards)
- Incremental sync using syncToken for subsequent runs
- Automatic token refresh when expired
- Handles 410 Gone response (expired syncToken) with automatic full sync

Destination Adapters:
- Google Sheets: Creates/updates rows in spreadsheet with event data
- Notion: Creates/updates pages in database with event properties
- Both support incremental updates (track existing events by ID)

Error Handling:
- Each sync processes independently (one failure doesn't stop others)
- Failed syncs are logged and marked with error status
- Next sync is still scheduled even on failure (will retry)
- Sync history records all run details including errors

Testing:
- Unit tests for Google Calendar client
- Unit tests for event transformation
- Integration tests for cron handler with mocked sync service

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Fixed bug in processWithConcurrency function where promises were
  incorrectly identified as pending/resolved (used Set instead of
  array with self-removal pattern)
- Added comprehensive test suite for SyncService covering:
  - getDueSyncs() query functionality
  - processSync() success path with event push
  - Event deletion handling
  - OAuth credentials validation
  - Destination push error handling
  - Calendar API error handling
  - Next sync scheduling
- Fixed type error in google-calendar test (fetch mock)

Task: #41189058 - Implement Cloudflare Worker cron for scheduled syncs

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@AlecKriebel

Copy link
Copy Markdown
Contributor Author

Review: Cannot Merge - Architecture Conflict

This PR cannot be merged due to fundamental conflicts with the evolved dev branch. Since this PR was created, dev has merged multiple PRs (#3-#6) that implemented the sync functionality with a different architecture:

Current Dev Architecture:

  • Database: calendars, destinations, googleConnections, airtableConnections, syncedEvents tables
  • Cron: Sync logic in shared package, called from worker cron
  • Features: Google Sheets, Airtable, and Notion destinations already implemented

This PR Architecture:

  • Database: syncConfigs, oauthCredentials, syncHistory tables
  • Cron: SyncService in worker package
  • Features: Google Sheets and Notion stubs only

Merge Conflicts:

  • 6 files with unresolvable conflicts
  • Database schema completely different
  • Cannot merge without complete rewrite

Recommendation:
Close this PR. The functionality it provides already exists on dev in a more complete and production-ready form. The underlying task can be marked complete since cron-based syncing is already implemented on dev.

Note: The concurrency limiter bug fix in this PR (commit 1705d68) is well-designed but doesn't apply to dev, which uses sequential processing instead of parallel processing.


Code Quality: The implementation itself is high quality with 25 passing tests and good architecture. However, it's been superseded by the evolution of the dev branch.

@AlecKriebel

Copy link
Copy Markdown
Contributor Author

Closing this PR as the functionality already exists on dev

@AlecKriebel AlecKriebel closed this Feb 1, 2026
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.

1 participant