Full signal handler support and cleanup on ctrl-C

pull/7/head
Toni Melisma 3 years ago
parent 32c2925c26
commit b805895b02
No known key found for this signature in database
GPG Key ID: FFF9A7EDDEA34756

@ -28,6 +28,10 @@ var assets embed.FS
// Define global exit function, so unit tests can override this
var exit = os.Exit
// Define global state for slice of WIP transformation jobs, used by signalHandler()
var wipJobs = make(map[string]transformationJob)
var wipJobMutex = sync.Mutex{}
// configuration state is stored in this struct
type configuration struct {
files struct {
@ -76,7 +80,8 @@ func initializeConfig() (config configuration) {
config.media.fullsizeMaxHeight = 1080
config.media.videoMaxSize = 640
config.concurrency = 8
// TODO adjust based on cores
config.concurrency = 4
return config
}
@ -869,8 +874,17 @@ func getGalleryFilenames(sourceFilename string, config configuration) (thumbnail
return
}
// transformFile takes a transformation job (an image or video) and creates a thumbnail, full-size
// image and a copy of the original
func transformFile(thisJob transformationJob, progressBar *pb.ProgressBar, config configuration) {
// TODO add ctrl-C support, signal intercept
// Before we begin work, add all work-in-progress files to wipSlice
// In case the program is killed before we're finished, signalHandler() deletes all the wip files.
// This way, no half-finished files will stay on the hard drive
wipJobMutex.Lock()
wipJobs[thisJob.sourceFilepath] = thisJob
wipJobMutex.Unlock()
// Do the actual transformation and increment the progress bar
if isImageFile(thisJob.filename) {
transformImage(thisJob.sourceFilepath, thisJob.fullsizeFilepath, thisJob.thumbnailFilepath, config)
} else if isVideoFile(thisJob.filename) {
@ -881,6 +895,10 @@ func transformFile(thisJob transformationJob, progressBar *pb.ProgressBar, confi
}
createOriginal(thisJob.sourceFilepath, thisJob.originalFilepath)
progressBar.Increment()
wipJobMutex.Lock()
delete(wipJobs, thisJob.sourceFilepath)
wipJobMutex.Unlock()
}
// This is the main concurrent goroutine that takes care of the parallelisation. A big bunch of them
@ -995,6 +1013,13 @@ func setupSignalHandler() {
func signalHandler(signalChan chan os.Signal) {
<-signalChan
log.Println("Ctrl-C received, cleaning up and aborting...")
wipJobMutex.Lock()
for _, job := range wipJobs {
fmt.Println("cleaning:", job.filename)
os.Remove(job.thumbnailFilepath)
os.Remove(job.fullsizeFilepath)
os.Remove(job.originalFilepath)
}
exit(0)
}

@ -1 +0,0 @@
/home/toni/go/src/github.com/tonimelisma/fastgallery/testing/source/2021-01-02 23.51.34.mp4

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

@ -42,27 +42,22 @@
</div>
<div class="col-4 col-md-3 col-lg-2 float-left p-md-2 p-lg-3">
<img class="box border border-gray box-shadow width-fit" src="_thumbnail/2021-01-02 23.51.34.jpg" alt="2021-01-02 23.51.34.mp4" onclick="changePicture(2);displayModal(true);">
<span class="px-2 pb-2 width-fit css-truncate css-truncate-target">2021-01-02 23.51.34.mp4</span>
</div>
<div class="col-4 col-md-3 col-lg-2 float-left p-md-2 p-lg-3">
<img class="box border border-gray box-shadow width-fit" src="_thumbnail/2021-02-22 15.36.43-1.jpg" alt="2021-02-22 15.36.43-1.heic" onclick="changePicture(3);displayModal(true);">
<img class="box border border-gray box-shadow width-fit" src="_thumbnail/2021-02-22 15.36.43-1.jpg" alt="2021-02-22 15.36.43-1.heic" onclick="changePicture(2);displayModal(true);">
<span class="px-2 pb-2 width-fit css-truncate css-truncate-target">2021-02-22 15.36.43-1.heic</span>
</div>
<div class="col-4 col-md-3 col-lg-2 float-left p-md-2 p-lg-3">
<img class="box border border-gray box-shadow width-fit" src="_thumbnail/2021-02-25 15.40.44.jpg" alt="2021-02-25 15.40.44.png" onclick="changePicture(4);displayModal(true);">
<img class="box border border-gray box-shadow width-fit" src="_thumbnail/2021-02-25 15.40.44.jpg" alt="2021-02-25 15.40.44.png" onclick="changePicture(3);displayModal(true);">
<span class="px-2 pb-2 width-fit css-truncate css-truncate-target">2021-02-25 15.40.44.png</span>
</div>
<div class="col-4 col-md-3 col-lg-2 float-left p-md-2 p-lg-3">
<img class="box border border-gray box-shadow width-fit" src="_thumbnail/_DSC9363_DxO.jpg" alt="_DSC9363_DxO.jpg" onclick="changePicture(5);displayModal(true);">
<img class="box border border-gray box-shadow width-fit" src="_thumbnail/_DSC9363_DxO.jpg" alt="_DSC9363_DxO.jpg" onclick="changePicture(4);displayModal(true);">
<span class="px-2 pb-2 width-fit css-truncate css-truncate-target">_DSC9363_DxO.jpg</span>
</div>
<div class="col-4 col-md-3 col-lg-2 float-left p-md-2 p-lg-3">
<img class="box border border-gray box-shadow width-fit" src="_thumbnail/_DSC9439.jpg" alt="_DSC9439.jpg" onclick="changePicture(6);displayModal(true);">
<img class="box border border-gray box-shadow width-fit" src="_thumbnail/_DSC9439.jpg" alt="_DSC9439.jpg" onclick="changePicture(5);displayModal(true);">
<span class="px-2 pb-2 width-fit css-truncate css-truncate-target">_DSC9439.jpg</span>
</div>
@ -116,14 +111,6 @@
filename: "2021-01-01 17.11.35.heic"
}
,
{
thumbnail: "_thumbnail/2021-01-02 23.51.34.jpg",
fullsize: "_fullsize/2021-01-02 23.51.34.mp4",
original: "_original/2021-01-02 23.51.34.mp4",
filename: "2021-01-02 23.51.34.mp4"
}
,
{
thumbnail: "_thumbnail/2021-02-22 15.36.43-1.jpg",

Loading…
Cancel
Save