Skip to content

Commit

Permalink
comments and setting max cores by passing 0 to WithThreads
Browse files Browse the repository at this point in the history
  • Loading branch information
kmulvey committed May 2, 2024
1 parent b15bf0b commit 07f9f77
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions internal/app/imageconvert/imageconvert.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package imageconvert
import (
"fmt"
"os"
"runtime"
"strings"

"github.com/kmulvey/goutils"
"github.com/kmulvey/humantime"
"github.com/kmulvey/path"
)

// ImageConverter is the main config
type ImageConverter struct {
Compress bool
Force bool
Expand All @@ -28,10 +30,12 @@ type ImageConverter struct {
ShutdownCompleted []chan struct{}
}

// NewWithDefaults returns a new ImageConverter with conservative defaults. Use the WithX() functions to
// further configure.
func NewWithDefaults(inputPath, skipFile string, directoryDepth uint8) (ImageConverter, error) {

var ic = ImageConverter{
Threads: 1, // uint8(runtime.NumCPU() - 1),
Threads: 1,
ShutdownCompleted: make([]chan struct{}, 1),
}
var err error
Expand Down Expand Up @@ -66,35 +70,49 @@ func NewWithDefaults(inputPath, skipFile string, directoryDepth uint8) (ImageCon
return ic, nil
}

// Shutdown gracefully closes all chans and quits.
func (ic *ImageConverter) Shutdown() {
close(ic.ShutdownTrigger)
<-goutils.MergeChannels(ic.ShutdownCompleted...)
}

// WithCompression will compress the images.
func (ic *ImageConverter) WithCompression() {
ic.Compress = true
}

// WithForce will process files even if ther are present in the skip file.
func (ic *ImageConverter) WithForce() {
ic.Force = true
}

// WithResize resizes images down to a size given by width X height greater than a threshold
// given by widthThreshold X heightThreshold.
func (ic *ImageConverter) WithResize(width, height, widthThreshold, heightThreshold uint16) {
ic.ResizeWidth = width
ic.ResizeWidthThreshold = widthThreshold
ic.ResizeHeight = height
ic.ResizeHeightThreshold = heightThreshold
}

// WithWatch enables watching a directory for new or modified files.
func (ic *ImageConverter) WithWatch() {
ic.Watch = true
}

// WithThreads specifies the number of CPU threads to use. The default is one but increacing this
// will significaltny improve performance epsically when compressing images. Pass a positive number
// of threads you wish to use, if 0 is passed, num cores - 1 will be set.
func (ic *ImageConverter) WithThreads(threads uint8) {
ic.Threads = threads
ic.ShutdownCompleted = make([]chan struct{}, threads)
if threads == 0 {
ic.Threads = uint8(runtime.NumCPU() - 1)
} else {
ic.Threads = threads
}
ic.ShutdownCompleted = make([]chan struct{}, ic.Threads)
}

// WithTimeRange will set a time range within images must have been last modified in order to be considered for processing.
func (ic *ImageConverter) WithTimeRange(tr humantime.TimeRange) {
ic.TimeRange = tr
}

0 comments on commit 07f9f77

Please sign in to comment.