@@ -178,67 +178,61 @@ def _get_version(self, task, args, kwargs):
178
178
return version
179
179
180
180
181
- class ArchiveBuilds (Task ):
182
-
183
- """Task to archive old builds to cold storage."""
184
-
185
- name = __name__ + '.archive_builds'
186
-
187
- def run (self , * args , ** kwargs ):
188
- if not settings .RTD_SAVE_BUILD_COMMANDS_TO_STORAGE :
189
- return
190
-
191
- lock_id = '{0}-lock' .format (self .name )
192
- days = kwargs .get ('days' , 14 )
193
- limit = kwargs .get ('limit' , 2000 )
194
- delete = kwargs .get ('delete' , True )
195
-
196
- with memcache_lock (lock_id , self .app .oid ) as acquired :
197
- if acquired :
198
- archive_builds_task (days = days , limit = limit , delete = delete )
199
- else :
200
- log .warning ('Archive Builds Task still locked' )
181
+ # NOTE: re-define this task to keep the old name. Delete it after deploy.
182
+ @app .task (queue = 'web' )
183
+ def archive_builds (days = 14 , limit = 200 , include_cold = False , delete = False ):
184
+ archive_builds_task .delay (days = days , limit = limit , include_cold = include_cold , delete = delete )
201
185
202
186
203
- def archive_builds_task (days = 14 , limit = 200 , include_cold = False , delete = False ):
187
+ @app .task (queue = 'web' , bind = True )
188
+ def archive_builds_task (self , days = 14 , limit = 200 , include_cold = False , delete = False ):
204
189
"""
205
- Find stale builds and remove build paths .
190
+ Task to archive old builds to cold storage .
206
191
207
192
:arg days: Find builds older than `days` days.
208
193
:arg include_cold: If True, include builds that are already in cold storage
209
194
:arg delete: If True, deletes BuildCommand objects after archiving them
210
195
"""
211
- max_date = timezone .now () - timezone .timedelta (days = days )
212
- queryset = Build .objects .exclude (commands__isnull = True )
213
- if not include_cold :
214
- queryset = queryset .exclude (cold_storage = True )
215
-
216
- queryset = (
217
- queryset
218
- .filter (date__lt = max_date )
219
- .prefetch_related ('commands' )
220
- .only ('date' , 'cold_storage' )
221
- [:limit ]
222
- )
196
+ if not settings .RTD_SAVE_BUILD_COMMANDS_TO_STORAGE :
197
+ return
223
198
224
- for build in queryset :
225
- commands = BuildCommandSerializer (build .commands , many = True ).data
226
- if commands :
227
- for cmd in commands :
228
- if len (cmd ['output' ]) > MAX_BUILD_COMMAND_SIZE :
229
- cmd ['output' ] = cmd ['output' ][- MAX_BUILD_COMMAND_SIZE :]
230
- cmd ['output' ] = "... (truncated) ...\n \n Command output too long. Truncated to last 1MB.\n \n " + cmd ['output' ] # noqa
231
- log .warning ('Truncating build command for build.' , build_id = build .id )
232
- output = BytesIO (json .dumps (commands ).encode ('utf8' ))
233
- filename = '{date}/{id}.json' .format (date = str (build .date .date ()), id = build .id )
234
- try :
235
- build_commands_storage .save (name = filename , content = output )
236
- build .cold_storage = True
237
- build .save ()
238
- if delete :
239
- build .commands .all ().delete ()
240
- except IOError :
241
- log .exception ('Cold Storage save failure' )
199
+ lock_id = '{0}-lock' .format (self .name )
200
+ with memcache_lock (lock_id , self .app .oid ) as acquired :
201
+ if not acquired :
202
+ log .warning ('Archive Builds Task still locked' )
203
+ return False
204
+
205
+ max_date = timezone .now () - timezone .timedelta (days = days )
206
+ queryset = Build .objects .exclude (commands__isnull = True )
207
+ if not include_cold :
208
+ queryset = queryset .exclude (cold_storage = True )
209
+
210
+ queryset = (
211
+ queryset
212
+ .filter (date__lt = max_date )
213
+ .prefetch_related ('commands' )
214
+ .only ('date' , 'cold_storage' )
215
+ [:limit ]
216
+ )
217
+
218
+ for build in queryset :
219
+ commands = BuildCommandSerializer (build .commands , many = True ).data
220
+ if commands :
221
+ for cmd in commands :
222
+ if len (cmd ['output' ]) > MAX_BUILD_COMMAND_SIZE :
223
+ cmd ['output' ] = cmd ['output' ][- MAX_BUILD_COMMAND_SIZE :]
224
+ cmd ['output' ] = "... (truncated) ...\n \n Command output too long. Truncated to last 1MB.\n \n " + cmd ['output' ] # noqa
225
+ log .warning ('Truncating build command for build.' , build_id = build .id )
226
+ output = BytesIO (json .dumps (commands ).encode ('utf8' ))
227
+ filename = '{date}/{id}.json' .format (date = str (build .date .date ()), id = build .id )
228
+ try :
229
+ build_commands_storage .save (name = filename , content = output )
230
+ build .cold_storage = True
231
+ build .save ()
232
+ if delete :
233
+ build .commands .all ().delete ()
234
+ except IOError :
235
+ log .exception ('Cold Storage save failure' )
242
236
243
237
244
238
@app .task (queue = 'web' )
0 commit comments