1
1
use crate :: error:: Result ;
2
2
use crate :: repositories:: { GitHub , GitLab } ;
3
- use crate :: utils:: { daemon :: cron , MetadataPackage } ;
4
- use crate :: { db:: Pool , Config , Context } ;
3
+ use crate :: utils:: MetadataPackage ;
4
+ use crate :: { db:: Pool , Config } ;
5
5
use chrono:: { DateTime , Utc } ;
6
6
use log:: { debug, info, trace, warn} ;
7
7
use once_cell:: sync:: Lazy ;
8
8
use postgres:: Client ;
9
9
use regex:: Regex ;
10
10
use std:: collections:: { HashMap , HashSet } ;
11
11
use std:: fmt;
12
- use std:: sync:: Arc ;
13
- use std:: time:: Duration ;
14
12
15
13
pub trait RepositoryForge {
16
14
/// Result used both as the `host` column in the DB and to match repository URLs during
@@ -52,6 +50,7 @@ pub struct FetchRepositoriesResult {
52
50
53
51
pub struct RepositoryStatsUpdater {
54
52
updaters : Vec < Box < dyn RepositoryForge + Send + Sync > > ,
53
+ pool : Pool ,
55
54
}
56
55
57
56
impl fmt:: Debug for RepositoryStatsUpdater {
@@ -65,33 +64,30 @@ impl fmt::Debug for RepositoryStatsUpdater {
65
64
}
66
65
67
66
impl RepositoryStatsUpdater {
68
- pub fn new ( config : & Config ) -> Self {
69
- let mut updaters: Vec < Box < dyn RepositoryForge + Send + Sync > > = Vec :: with_capacity ( 3 ) ;
67
+ pub fn new ( config : & Config , pool : Pool ) -> Self {
68
+ let mut updaters: Vec < Box < dyn RepositoryForge + Send + Sync > > = Vec :: new ( ) ;
70
69
if let Ok ( Some ( updater) ) = GitHub :: new ( & config) {
71
70
updaters. push ( Box :: new ( updater) ) ;
72
71
}
73
- if let Ok ( Some ( updater) ) = GitLab :: new ( "gitlab.com" , & config. gitlab_accesstoken ) {
72
+ if let Ok ( updater) = GitLab :: new ( "gitlab.com" , & config. gitlab_accesstoken ) {
74
73
updaters. push ( Box :: new ( updater) ) ;
75
74
}
76
- if let Ok ( Some ( updater) ) = GitLab :: new ( "gitlab.freedesktop.org" , & None ) {
75
+ if let Ok ( updater) = GitLab :: new ( "gitlab.freedesktop.org" , & None ) {
77
76
updaters. push ( Box :: new ( updater) ) ;
78
77
}
79
- Self { updaters }
78
+ Self { updaters, pool }
80
79
}
81
80
82
- pub ( crate ) fn load_repository (
83
- & self ,
84
- conn : & mut Client ,
85
- metadata : & MetadataPackage ,
86
- ) -> Result < Option < i32 > > {
81
+ pub ( crate ) fn load_repository ( & self , metadata : & MetadataPackage ) -> Result < Option < i32 > > {
87
82
let url = match & metadata. repository {
88
83
Some ( url) => url,
89
84
None => {
90
85
debug ! ( "did not collect stats as no repository URL was present" ) ;
91
86
return Ok ( None ) ;
92
87
}
93
88
} ;
94
- self . load_repository_inner ( conn, url)
89
+ let mut conn = self . pool . get ( ) ?;
90
+ self . load_repository_inner ( & mut conn, url)
95
91
}
96
92
97
93
fn load_repository_inner ( & self , conn : & mut Client , url : & str ) -> Result < Option < i32 > > {
@@ -109,7 +105,7 @@ impl RepositoryStatsUpdater {
109
105
}
110
106
for updater in self . updaters . iter ( ) . filter ( |u| u. host ( ) == name. host ) {
111
107
let res = match updater. fetch_repository ( & name) {
112
- Ok ( Some ( repo) ) => Self :: store_repository ( conn, updater. host ( ) , repo) ,
108
+ Ok ( Some ( repo) ) => self . store_repository ( conn, updater. host ( ) , repo) ,
113
109
Err ( err) => {
114
110
warn ! ( "failed to collect `{}` stats: {}" , updater. host( ) , err) ;
115
111
return Ok ( None ) ;
@@ -128,19 +124,8 @@ impl RepositoryStatsUpdater {
128
124
Ok ( None )
129
125
}
130
126
131
- pub fn start_crons ( config : Arc < Config > , pool : Pool ) -> Result < ( ) > {
132
- let instance = Self :: new ( & config) ;
133
- cron (
134
- "repositories stats updater" ,
135
- Duration :: from_secs ( 60 * 60 ) ,
136
- move || instance. update_all_crates ( & pool) ,
137
- ) ?;
138
-
139
- Ok ( ( ) )
140
- }
141
-
142
- pub fn update_all_crates ( & self , pool : & Pool ) -> Result < ( ) > {
143
- let mut conn = pool. get ( ) ?;
127
+ pub fn update_all_crates ( & self ) -> Result < ( ) > {
128
+ let mut conn = self . pool . get ( ) ?;
144
129
for updater in & self . updaters {
145
130
info ! ( "started updating `{}` repositories stats" , updater. host( ) ) ;
146
131
@@ -165,20 +150,19 @@ impl RepositoryStatsUpdater {
165
150
for chunk in needs_update. chunks ( updater. chunk_size ( ) ) {
166
151
let res = updater. fetch_repositories ( chunk) ?;
167
152
for node in res. missing {
168
- Self :: delete_repository ( & mut conn, & node, updater. host ( ) ) ?;
153
+ self . delete_repository ( & mut conn, & node, updater. host ( ) ) ?;
169
154
}
170
155
for ( _, repo) in res. present {
171
- Self :: store_repository ( & mut conn, updater. host ( ) , repo) ?;
156
+ self . store_repository ( & mut conn, updater. host ( ) , repo) ?;
172
157
}
173
158
}
174
159
info ! ( "finished updating `{}` repositories stats" , updater. host( ) ) ;
175
160
}
176
161
Ok ( ( ) )
177
162
}
178
163
179
- pub fn backfill_repositories ( & self , ctx : & dyn Context ) -> Result < ( ) > {
180
- let pool = ctx. pool ( ) ?;
181
- let mut conn = pool. get ( ) ?;
164
+ pub fn backfill_repositories ( & self ) -> Result < ( ) > {
165
+ let mut conn = self . pool . get ( ) ?;
182
166
for updater in & self . updaters {
183
167
info ! (
184
168
"started backfilling `{}` repositories stats" ,
@@ -234,10 +218,11 @@ impl RepositoryStatsUpdater {
234
218
return updater. icon ( ) ;
235
219
}
236
220
}
237
- ""
221
+ // The default icon in case it doesn't match any of the "known" ones.
222
+ "code-branch"
238
223
}
239
224
240
- fn store_repository ( conn : & mut Client , host : & str , repo : Repository ) -> Result < i32 > {
225
+ fn store_repository ( & self , conn : & mut Client , host : & str , repo : Repository ) -> Result < i32 > {
241
226
trace ! (
242
227
"storing {} repository stats for {}" ,
243
228
host,
@@ -271,7 +256,7 @@ impl RepositoryStatsUpdater {
271
256
Ok ( data. get ( 0 ) )
272
257
}
273
258
274
- fn delete_repository ( conn : & mut Client , host_id : & str , host : & str ) -> Result < ( ) > {
259
+ fn delete_repository ( & self , conn : & mut Client , host_id : & str , host : & str ) -> Result < ( ) > {
275
260
trace ! (
276
261
"removing repository stats for host ID `{}` and host `{}`" ,
277
262
host_id,
0 commit comments