|
| 1 | +package org.labkey.mgap.etl; |
| 2 | + |
| 3 | +import org.apache.commons.io.FileUtils; |
| 4 | +import org.apache.commons.lang3.tuple.Pair; |
| 5 | +import org.apache.logging.log4j.Logger; |
| 6 | +import org.labkey.api.data.Container; |
| 7 | +import org.labkey.api.pipeline.PipelineJobException; |
| 8 | +import org.labkey.api.sequenceanalysis.run.SimpleScriptWrapper; |
| 9 | + |
| 10 | +import java.io.File; |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | + |
| 18 | +public class EtlQueueManager |
| 19 | +{ |
| 20 | + private static EtlQueueManager _instance = new EtlQueueManager(); |
| 21 | + |
| 22 | + private Map<Container, List<Pair<File, File>>> _pendingFileCopy = new HashMap<>(); |
| 23 | + |
| 24 | + private Map<Container, List<Pair<File, File>>> _pendingRsyncCopy = new HashMap<>(); |
| 25 | + |
| 26 | + private EtlQueueManager() |
| 27 | + { |
| 28 | + |
| 29 | + } |
| 30 | + |
| 31 | + public static EtlQueueManager get() |
| 32 | + { |
| 33 | + return _instance; |
| 34 | + } |
| 35 | + |
| 36 | + public void clearQueue(Container container, Logger log) |
| 37 | + { |
| 38 | + if (_pendingFileCopy.containsKey(container) && !_pendingFileCopy.get(container).isEmpty()) |
| 39 | + { |
| 40 | + log.error("The file copy queue was not empty!"); |
| 41 | + } |
| 42 | + |
| 43 | + if (_pendingRsyncCopy.containsKey(container) && !_pendingRsyncCopy.get(container).isEmpty()) |
| 44 | + { |
| 45 | + log.error("The rsync copy queue was not empty!"); |
| 46 | + } |
| 47 | + |
| 48 | + _pendingFileCopy.clear(); |
| 49 | + _pendingRsyncCopy.clear(); |
| 50 | + } |
| 51 | + |
| 52 | + public void performQueuedWork(Container container, Logger log) |
| 53 | + { |
| 54 | + List<Pair<File, File>> queue = _pendingFileCopy.get(container); |
| 55 | + if (queue != null && !queue.isEmpty()) |
| 56 | + { |
| 57 | + queue.forEach(x -> copyFile(x.getLeft(), x.getRight(), log)); |
| 58 | + } |
| 59 | + _pendingFileCopy.clear(); |
| 60 | + |
| 61 | + List<Pair<File, File>> rsyncQueue = _pendingRsyncCopy.get(container); |
| 62 | + if (rsyncQueue != null && !rsyncQueue.isEmpty()) |
| 63 | + { |
| 64 | + rsyncQueue.forEach(x -> doRsyncCopy(x.getLeft(), x.getRight(), log)); |
| 65 | + } |
| 66 | + _pendingRsyncCopy.clear(); |
| 67 | + } |
| 68 | + |
| 69 | + public void queueFileCopy(Container c, File source, File destination) |
| 70 | + { |
| 71 | + if (!_pendingFileCopy.containsKey(c)) |
| 72 | + { |
| 73 | + _pendingFileCopy.put(c, new ArrayList<>()); |
| 74 | + } |
| 75 | + |
| 76 | + _pendingFileCopy.get(c).add(Pair.of(source, destination)); |
| 77 | + } |
| 78 | + |
| 79 | + public void queueRsyncCopy(Container c, File source, File destination) |
| 80 | + { |
| 81 | + if (!_pendingRsyncCopy.containsKey(c)) |
| 82 | + { |
| 83 | + _pendingRsyncCopy.put(c, new ArrayList<>()); |
| 84 | + } |
| 85 | + |
| 86 | + _pendingRsyncCopy.get(c).add(Pair.of(source, destination)); |
| 87 | + } |
| 88 | + |
| 89 | + private void doRsyncCopy(File sourceDir, File destination, Logger log) |
| 90 | + { |
| 91 | + // NOTE: rsync should no-op if there are no source changes |
| 92 | + log.info("Performing rsync from: " + sourceDir.getPath() + " to " + destination.getPath()); |
| 93 | + try |
| 94 | + { |
| 95 | + new SimpleScriptWrapper(log).execute(Arrays.asList( |
| 96 | + "rsync", "-r", "-a", "--delete", "--no-owner", "--no-group", "--chmod=D2770,F660", sourceDir.getPath(), destination.getPath() |
| 97 | + )); |
| 98 | + } |
| 99 | + catch (PipelineJobException e) |
| 100 | + { |
| 101 | + log.error("Error running rsync", e); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private void copyFile(File source, File destination, Logger log) |
| 106 | + { |
| 107 | + if (destination.exists()) |
| 108 | + { |
| 109 | + destination.delete(); |
| 110 | + } |
| 111 | + |
| 112 | + try |
| 113 | + { |
| 114 | + log.info("Copying file: " + source.getPath()); |
| 115 | + FileUtils.copyFile(source, destination); |
| 116 | + } |
| 117 | + catch (IOException e) |
| 118 | + { |
| 119 | + log.error("Error copying file", e); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments