Skip to content

Commit fd9b12d

Browse files
committed
Allow long-running upgrade code to run separate from update
1 parent 49cf837 commit fd9b12d

2 files changed

Lines changed: 67 additions & 18 deletions

File tree

SequenceAnalysis/src/org/labkey/sequenceanalysis/SequenceAnalysisController.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5254,4 +5254,38 @@ public void setDataFileUrl(String dataFileUrl)
52545254
_dataFileUrl = dataFileUrl;
52555255
}
52565256
}
5257+
5258+
@UtilityAction(label = "Migrate Sequence Files", description = "This will start a background process to migrate sequence files from the flat .sequences folder into a hashed multi-folder scheme")
5259+
@RequiresSiteAdmin
5260+
public static class MigrateSequenceFilesAction extends ConfirmAction<Object>
5261+
{
5262+
@Override
5263+
public ModelAndView getConfirmView(Object o, BindException errors) throws Exception
5264+
{
5265+
setTitle("Migrate Sequence Files");
5266+
5267+
return(HtmlView.of("This will start a background process to migrate sequence files from the flat .sequences folder into a hashed multi-folder scheme. Do you want to continue?"));
5268+
}
5269+
5270+
@Override
5271+
public boolean handlePost(Object o, BindException errors) throws Exception
5272+
{
5273+
SequenceAnalysisUpgradeCode.doSequenceMigration(getUser(), _log);
5274+
5275+
return true;
5276+
}
5277+
5278+
@Override
5279+
public void validateCommand(Object o, Errors errors)
5280+
{
5281+
5282+
}
5283+
5284+
@NotNull
5285+
@Override
5286+
public URLHelper getSuccessURL(Object o)
5287+
{
5288+
return getContainer().getStartURL(getUser());
5289+
}
5290+
}
52575291
}

SequenceAnalysis/src/org/labkey/sequenceanalysis/SequenceAnalysisUpgradeCode.java

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import org.labkey.api.exp.api.ExperimentService;
2121
import org.labkey.api.module.ModuleContext;
2222
import org.labkey.api.query.FieldKey;
23+
import org.labkey.api.security.User;
2324
import org.labkey.api.sequenceanalysis.RefNtSequenceModel;
25+
import org.labkey.api.util.FileUtil;
2426
import org.labkey.api.util.PageFlowUtil;
2527

2628
import java.io.File;
@@ -234,68 +236,81 @@ public void updateBarcodeRC(final ModuleContext moduleContext)
234236
@SuppressWarnings({"UnusedDeclaration"})
235237
@DeferredUpgrade
236238
public void migrateSequenceDirs(final ModuleContext moduleContext)
239+
{
240+
doSequenceMigration(moduleContext.getUpgradeUser(), _log);
241+
}
242+
243+
public static void doSequenceMigration(User u, Logger log)
237244
{
238245
try
239246
{
240247
TableInfo ti = SequenceAnalysisSchema.getTable(SequenceAnalysisSchema.TABLE_REF_NT_SEQUENCES);
241248
TableSelector ts = new TableSelector(ti);
242249
List<RefNtSequenceModel> nts = ts.getArrayList(RefNtSequenceModel.class);
243-
_log.info(nts.size() + " total sequences to migrate");
250+
log.info(nts.size() + " total sequences to migrate");
244251
int processed = 0;
245252
int totalMigrated = 0;
246253
for (RefNtSequenceModel nt : nts)
247254
{
248255
processed++;
249-
250256
if (processed % 1000 == 0)
251257
{
252-
_log.info("{} of {} sequence files migrated", processed, nts.size());
258+
log.info("{} of {} sequence files migrated", processed, nts.size());
253259
}
254260

255261
ExpData legacyExpData = ExperimentService.get().getExpData(nt.getSequenceFile());
256262
if (legacyExpData == null)
257263
{
258-
_log.error("Missing ExpData for NT sequence: {}", nt.getSequenceFile());
264+
log.error("Missing ExpData for NT sequence: {}", nt.getSequenceFile());
259265
continue;
260266
}
261267

262268
File legacyFile = legacyExpData.getFile();
263-
if (!legacyFile.exists())
269+
if (!RefNtSequenceModel.BASE_DIRNAME.equals(legacyFile.getParentFile().getName()))
264270
{
265-
_log.error("Missing file for NT sequence: {}", legacyFile.getPath());
271+
// NOTE: this includes sequences imported to custom locations, such as refSequenceImport pipeline jobs
266272
continue;
267273
}
268274

269-
if (!RefNtSequenceModel.BASE_DIRNAME.equals(legacyFile.getParentFile().getName()))
275+
File newLocation = nt.getExpectedSequenceFile(null);
276+
if (legacyFile.equals(newLocation))
270277
{
271-
// NOTE: this includes sequences imported to custom locations, such as refSequenceImport pipeline jobs
272278
continue;
273279
}
274280

275-
File newLocation = nt.getExpectedSequenceFile(null);
281+
if (!legacyFile.exists())
282+
{
283+
log.error("Missing file for NT sequence: {}", legacyFile.getPath());
284+
continue;
285+
}
286+
276287
if (!newLocation.getParentFile().exists())
277288
{
278-
newLocation.getParentFile().mkdirs();
289+
FileUtil.mkdirs(newLocation.getParentFile());
279290
}
280291

281292
if (newLocation.exists())
282293
{
283-
_log.error("Target location for migrated sequence file exists, this might indicate a retry after a filed move: {}", newLocation.getPath());
284-
continue;
294+
if (newLocation.length() == legacyFile.length())
295+
{
296+
continue;
297+
}
298+
299+
log.error("Target location for migrated sequence file exists, but file size is smaller. this might indicate a retry after a filed move. deleting the target and retrying: {}", newLocation.getPath());
300+
FileUtils.delete(newLocation);
285301
}
286302

287-
totalMigrated++;
288-
FileUtils.copyFile(legacyFile, newLocation);
303+
FileUtils.moveFile(legacyFile, newLocation);
289304
legacyExpData.setDataFileURI(newLocation.toURI());
290-
legacyExpData.save(moduleContext.getUpgradeUser());
291-
legacyFile.delete();
305+
legacyExpData.save(u);
306+
totalMigrated++;
292307
}
293308

294-
_log.info("Total sequences migrated: {}", totalMigrated);
309+
log.info("Total sequences migrated: {}", totalMigrated);
295310
}
296311
catch (Exception e)
297312
{
298-
_log.error("Error upgrading sequenceanalysis module", e);
313+
log.error("Error migrating sequence files", e);
299314
}
300315
}
301316
}

0 commit comments

Comments
 (0)