Skip to content

Commit

Permalink
Fix memory consumption errors with new -m flag
Browse files Browse the repository at this point in the history
  • Loading branch information
tonimelisma committed Dec 13, 2020
1 parent 71897d4 commit 738b127
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
43 changes: 35 additions & 8 deletions cmd/fastgallery/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
"text/template"
Expand Down Expand Up @@ -55,6 +56,7 @@ var optIgnoreVideos = false
var optDryRun = false
var optVerbose = false
var optCleanUp = false
var optMemoryUse = false

// this function parses command-line arguments
func parseArgs() (inputDirectory string, outputDirectory string) {
Expand All @@ -64,6 +66,7 @@ func parseArgs() (inputDirectory string, outputDirectory string) {
optDryRunPtr := flag.Bool("d", false, "Dry run - don't make changes, only explain what would be done")
optVerbosePtr := flag.Bool("v", false, "Verbose - print debugging information to stderr")
optProfile := flag.Bool("p", false, "Run Go pprof profiling service for debugging")
optMemory := flag.Bool("m", false, "Minimize memory usage")

flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTION]... DIRECTORY\n\n", os.Args[0])
Expand Down Expand Up @@ -97,6 +100,10 @@ func parseArgs() (inputDirectory string, outputDirectory string) {
os.Exit(1)
}

if *optMemory {
optMemoryUse = true
}

if *optProfile {
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
Expand Down Expand Up @@ -233,6 +240,22 @@ func isMediaFile(filename string) bool {
return false
}

// Checks if given directory entry is symbolic link to a directory
func isSymlinkDir(directory string, entry os.FileInfo) (is bool) {
if entry.Mode()&os.ModeSymlink != 0 {
realPath, err := filepath.EvalSymlinks(filepath.Join(directory, entry.Name()))
checkError(err)

fileinfo, err := os.Lstat(realPath)
checkError(err)

if fileinfo.IsDir() {
return true
}
}
return false
}

func recurseDirectory(thisDirectory string, relativeDirectory string) (root directory) {
root.name = filepath.Base(thisDirectory)
asIsStat, _ := os.Stat(thisDirectory)
Expand All @@ -247,7 +270,7 @@ func recurseDirectory(thisDirectory string, relativeDirectory string) (root dire
}

for _, entry := range list {
if entry.IsDir() {
if entry.IsDir() || isSymlinkDir(thisDirectory, entry) {
if !isEmptyDir(filepath.Join(thisDirectory, entry.Name())) {
root.subdirectories = append(root.subdirectories, recurseDirectory(filepath.Join(thisDirectory, entry.Name()), filepath.Join(relativeDirectory, root.name)))
}
Expand Down Expand Up @@ -713,6 +736,9 @@ func fullsizeImageWorker(wg *sync.WaitGroup, imageJobs chan job, progressBar *pb
resizeFullsizeImage(job.source, job.destination)
if !optDryRun {
progressBar.Increment()
if optMemoryUse {
runtime.GC()
}
}
}
}
Expand All @@ -723,6 +749,9 @@ func fullsizeVideoWorker(wg *sync.WaitGroup, videoJobs chan job, progressBar *pb
resizeFullsizeVideo(job.source, job.destination)
if !optDryRun {
progressBar.Increment()
if optMemoryUse {
runtime.GC()
}
}
}
}
Expand Down Expand Up @@ -914,15 +943,13 @@ func main() {
progressBar = pb.StartNew(changes)
if optVerbose {
vips.LoggingSettings(nil, vips.LogLevelDebug)
} else {
vips.LoggingSettings(nil, vips.LogLevelMessage)
}
if optVerbose {
vips.Startup(&vips.Config{
ReportLeaks: true})
CacheTrace: false,
CollectStats: false,
ReportLeaks: true})
} else {
vips.Startup(&vips.Config{
ReportLeaks: false})
vips.LoggingSettings(nil, vips.LogLevelError)
vips.Startup(nil)
}
defer vips.Shutdown()
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.15

require (
github.com/cheggaaa/pb/v3 v3.0.5
github.com/davidbyttow/govips/v2 v2.1.1-0.20201210220308-63f6534b5fe1
github.com/davidbyttow/govips/v2 v2.2.0 // indirect
github.com/fatih/color v1.10.0 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/stretchr/testify v1.6.1
Expand Down
9 changes: 7 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ github.com/cheggaaa/pb/v3 v3.0.5/go.mod h1:X1L61/+36nz9bjIsrDU52qHKOQukUQe2Ge+Yv
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davidbyttow/govips/v2 v2.1.1-0.20201210220308-63f6534b5fe1 h1:Z+LDD8N8J7C08ZJ/ScZKM8+zdOROUKajBEHaJQQw3KI=
github.com/davidbyttow/govips/v2 v2.1.1-0.20201210220308-63f6534b5fe1/go.mod h1:goq38QD8XEMz2aWEeucEZqRxAWsemIN40vbUqfPfTAw=
github.com/davidbyttow/govips v0.0.0-20201026223743-b1b72c7305d9 h1:nXNjG6pGALu++DSxtrdO79AZhtU+jZnyMlq3FTkFH74=
github.com/davidbyttow/govips/v2 v2.1.0 h1:n18SBq7dnjvW1+WKk65tnuF9IBaA4A1YHYrQ1iKZ28g=
github.com/davidbyttow/govips/v2 v2.1.0/go.mod h1:goq38QD8XEMz2aWEeucEZqRxAWsemIN40vbUqfPfTAw=
github.com/davidbyttow/govips/v2 v2.1.1-0.20201213191833-b69d46af32bb h1:SbICNpa72dhbQaJ+R5yYLGjbpEXU0RE5puO8qa+muag=
github.com/davidbyttow/govips/v2 v2.1.1-0.20201213191833-b69d46af32bb/go.mod h1:goq38QD8XEMz2aWEeucEZqRxAWsemIN40vbUqfPfTAw=
github.com/davidbyttow/govips/v2 v2.2.0 h1:O21ykVQNJ7LYxpa7yG6TN2g2VF/WonOksljCD2jQK4o=
github.com/davidbyttow/govips/v2 v2.2.0/go.mod h1:goq38QD8XEMz2aWEeucEZqRxAWsemIN40vbUqfPfTAw=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
Expand Down

0 comments on commit 738b127

Please sign in to comment.