@@ -2,7 +2,7 @@ import { Context } from 'hono';
22import { GitHubService } from './services/github' ;
33import { PackagistService } from './services/packagist' ;
44import { CacheService } from './services/cache' ;
5- import { AppContext , Env , GithubWebhook } from './types' ;
5+ import { AppContext , Env , GithubWebhook , GitHubTaskMessage } from './types' ;
66
77// Main Routes Handler
88export const listRepositories = async ( c : Context < { Bindings : Env } > ) => {
@@ -40,19 +40,16 @@ export const githubIssueWebhook = async (c: Context<{ Bindings: Env }>) => {
4040 const payload : GithubWebhook = await c . req . json ( ) ;
4141 const repoName = payload . repository . name ;
4242 const ownerLogin = payload . repository . owner . login ;
43+
44+ // Send a message to the queue to update issues
45+ await c . env . GITHUB_QUEUE . send ( {
46+ type : 'fetch-repository-issues' ,
47+ owner : ownerLogin ,
48+ repo : repoName ,
49+ timestamp : Date . now ( )
50+ } as GitHubTaskMessage ) ;
4351
44- const appContext : AppContext = {
45- env : c . env ,
46- octokit : c . env . octokit !
47- } ;
48-
49- const githubService = new GitHubService ( appContext ) ;
50- const cacheService = new CacheService ( c . env ) ;
51-
52- const issues = await githubService . getAllIssues ( ownerLogin , repoName ) ;
53- await cacheService . updateIssuesForRepository ( repoName , issues ) ;
54-
55- return c . text ( `Updated issues for ${ repoName } ` , 200 ) ;
52+ return c . text ( `Queued update for issues of ${ repoName } ` , 200 ) ;
5653 } catch ( error ) {
5754 console . error ( 'Error processing webhook:' , error ) ;
5855 return c . text ( 'Error processing webhook' , 500 ) ;
@@ -61,7 +58,7 @@ export const githubIssueWebhook = async (c: Context<{ Bindings: Env }>) => {
6158
6259// Background refreshers
6360export const refreshGithubStats = async ( context : AppContext ) => {
64- console . log ( 'Refreshing Github Stats ' ) ;
61+ console . log ( 'Fetching repositories and queueing GitHub stats tasks ' ) ;
6562 const githubService = new GitHubService ( context ) ;
6663 const cacheService = new CacheService ( context . env ) ;
6764
@@ -70,27 +67,58 @@ export const refreshGithubStats = async (context: AppContext) => {
7067 const repos = await githubService . getAllRepos ( orgName ) ;
7168 await cacheService . setRepositories ( repos ) ;
7269
73- // Get and cache contributors
74- const contributors = await githubService . getUserContributions ( repos ) ;
75- await cacheService . setContributors ( contributors ) ;
70+ // Queue contributor and PR tasks for each repository
71+ for ( const repo of repos ) {
72+ // Queue contributors fetch
73+ await context . env . GITHUB_QUEUE . send ( {
74+ type : 'fetch-repository-contributors' ,
75+ owner : repo . owner . login ,
76+ repo : repo . name ,
77+ timestamp : Date . now ( )
78+ } as GitHubTaskMessage ) ;
79+
80+ // Queue pull requests fetch
81+ await context . env . GITHUB_QUEUE . send ( {
82+ type : 'fetch-repository-pull-requests' ,
83+ owner : repo . owner . login ,
84+ repo : repo . name ,
85+ timestamp : Date . now ( )
86+ } as GitHubTaskMessage ) ;
87+ }
7688
77- console . log ( 'Refreshed Github Stats' ) ;
89+ // Queue a task to process all contributor data after the individual tasks
90+ // We'll send this separately without a delay since Cloudflare Workers queues
91+ // will process messages in roughly the order they were received
92+ await context . env . GITHUB_QUEUE . send ( {
93+ type : 'process-contributors' ,
94+ owner : orgName ,
95+ timestamp : Date . now ( ) ,
96+ metadata : {
97+ // Add a higher priority flag that we can check in the consumer
98+ highPriority : false
99+ }
100+ } as GitHubTaskMessage ) ;
101+
102+ console . log ( 'Queued GitHub stats tasks for all repositories' ) ;
78103} ;
79104
80105export const refreshRepositoryIssues = async ( context : AppContext ) => {
81- console . log ( 'Refreshing Repository Issues' ) ;
82- const githubService = new GitHubService ( context ) ;
106+ console . log ( 'Queueing Repository Issues refresh tasks' ) ;
83107 const cacheService = new CacheService ( context . env ) ;
84108
85109 const repos = await cacheService . getRepositories ( ) ;
86- const issues : Record < string , any [ ] > = { } ;
87110
111+ // Queue issue fetching for each repository
88112 for ( const repo of repos ) {
89- issues [ repo . name ] = await githubService . getAllIssues ( repo . owner . login , repo . name ) ;
113+ await context . env . GITHUB_QUEUE . send ( {
114+ type : 'fetch-repository-issues' ,
115+ owner : repo . owner . login ,
116+ repo : repo . name ,
117+ timestamp : Date . now ( )
118+ } as GitHubTaskMessage ) ;
90119 }
91120
92- await cacheService . setIssues ( issues ) ;
93- console . log ( 'Refreshed Repository Issues' ) ;
121+ console . log ( 'Queued Repository Issues tasks' ) ;
94122} ;
95123
96124export const refreshPackagistStats = async ( context : AppContext ) => {
0 commit comments