-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.php
executable file
·58 lines (53 loc) · 2.54 KB
/
monitor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
// This script is called from upload.php, and monitors PV output for montage images
// When a new montage image is generated by PV, this script converts it to png
// and copies it to the url that the front-end is requesting. Once the final image
// is copied, a zip file is created with all of the frames, all output images are deleted
// and the original uploaded file is removed from the uploads directory.
$target_file = $argv[1];
$fileHash = $argv[2];
$montagePath = $argv[3];
$montage = $montagePath . $fileHash . ".png";
$pngOutput = $montagePath . "pngOutput/";
/// While the final montage image has not been generated, look for intermediate ones and display them
$montageCount = 0;
while (!file_exists(glob($montagePath . $fileHash . "*last_*_final.tif")[0])) {
$montageFiles = glob($montagePath . $fileHash . "*[0-9]_[0-9]*.tif");
if (count($montageFiles) > $montageCount) {
sort($montageFiles, SORT_NATURAL);
sleep(1);
// Use IM to convert PV's .tif to .png
exec("convert " . $montageFiles[$montageCount] . "[0] " . $montage . "_tmp.png");
// Put the image where the front-end expects it to be
copy($montage."_tmp.png", $montage); // Stupid hack to deal with asynchrony
// Copy the image to a seperate directory where an archive will be made
copy($montage."_tmp.png", $pngOutput.$montageCount++."_".$fileHash.".png");
}
sleep(1);
}
// Grab and move the final output montage image
$montageFinal = glob($montagePath . $fileHash . "*last_*_final.tif")[0];
exec("convert " . $montageFinal . "[0] " . $montage);
copy($montage, $pngOutput.$montageCount."_".$fileHash.".png");
/// Garbage Collection
// Clean PV output images
unlink($montageFinal);
$montageFiles = glob($montagePath . $fileHash . "*[0-9]_[0-9]*.tif"); // Re-glob in case some were missed (Earlier race condition)
foreach ($montageFiles as $toClean) {
unlink($toClean);
}
sleep(1); // To be sure js refreshed the last image
unlink($montage."_tmp.png");
// Create archive with all frames from the run
// TODO: Check if this was done successfully before moving on to cleanup
$phArchive = new PharData($pngOutput.'MontageFrames.zip');
$phArchive->buildFromDirectory($pngOutput, '/\.png$/');
//Clean images that were saved in archive
foreach (glob($pngOutput."*.png") as $pngFile) {
unlink($pngFile);
}
// Move the last montage image into another directory (for testing, mostly).
rename($montage, "archive/" . $fileHash . ".png");
// This should happen last because it tells the server that this script is finished.
unlink($target_file);
?>