Skip to content

Commit

Permalink
Full signal handler support and cleanup on ctrl-C
Browse files Browse the repository at this point in the history
  • Loading branch information
tonimelisma committed Feb 28, 2021
1 parent 32c2925 commit b805895
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
29 changes: 27 additions & 2 deletions cmd/fastgallery/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down
Binary file removed testing/gallery/_fullsize/2021-01-02 23.51.34.mp4
Binary file not shown.
1 change: 0 additions & 1 deletion testing/gallery/_original/2021-01-02 23.51.34.mp4

This file was deleted.

Binary file removed testing/gallery/_thumbnail/2021-01-02 23.51.34.jpg
Binary file not shown.
21 changes: 4 additions & 17 deletions testing/gallery/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,22 @@ <h1 class="px-2 pb-2 my-0 m-md-3 m-lg-4">source</h1>
</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>

Expand Down Expand Up @@ -116,14 +111,6 @@ <h1 class="px-2 pb-2 my-0 m-md-3 m-lg-4">source</h1>
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",
Expand Down

0 comments on commit b805895

Please sign in to comment.