Skip to content

Commit

Permalink
feat(check): add min_free_space config option for each uploader
Browse files Browse the repository at this point in the history
this allows you to set a minimum amount of free space in bytes (relative to local_folder). if this threshold is exceeded, an upload will be forced.
  • Loading branch information
l3uddz committed Jul 12, 2020
1 parent 3ddd2a8 commit 6be4148
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 21 deletions.
61 changes: 52 additions & 9 deletions cmd/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/l3uddz/crop/rclone"
"github.com/l3uddz/crop/uploader"
"github.com/pkg/errors"
"github.com/shirou/gopsutil/disk"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"strings"
Expand Down Expand Up @@ -96,19 +97,56 @@ var uploadCmd = &cobra.Command{
}

// check if upload criteria met
forced := false

if !flagNoCheck {
// no check was not enabled
if res, err := upload.Check(); err != nil {
res, err := upload.Check()
if err != nil {
upload.Log.WithError(err).Error("Failed checking if uploader check conditions met, skipping...")
continue
} else if !res.Passed {
upload.Log.WithField("until", res.Info).Info("Upload conditions not met, skipping...")
continue
}

if !res.Passed {
// get free disk space
freeDiskSpace := "Unknown"
du, err := disk.Usage(upload.Config.LocalFolder)
if err == nil {
freeDiskSpace = humanize.Bytes(du.Free)
}

// check available disk space
switch {
case err != nil && upload.Config.Check.MinFreeSpace > 0:
// error checking free space
upload.Log.WithError(err).Errorf("Failed checking available free space for: %q",
upload.Config.LocalFolder)
case err == nil && du.Free < upload.Config.Check.MinFreeSpace:
// free space has gone below the free space threshold
forced = true
upload.Log.WithFields(logrus.Fields{
"until": res.Info,
"free_disk": freeDiskSpace,
}).Infof("Upload conditions not met, however, proceeding as free space below %s",
humanize.Bytes(upload.Config.Check.MinFreeSpace))
default:
break
}

if !forced {
upload.Log.WithFields(logrus.Fields{
"until": res.Info,
"free_disk": freeDiskSpace,
}).Info("Upload conditions not met, skipping...")
continue
}

// the upload was forced as min_free_size was met
}
}

// perform upload
if err := performUpload(upload); err != nil {
if err := performUpload(upload, forced); err != nil {
upload.Log.WithError(err).Error("Error occurred while running uploader, skipping...")
continue
}
Expand All @@ -126,7 +164,7 @@ func init() {
uploadCmd.Flags().BoolVar(&flagNoCheck, "no-check", false, "Ignore check and run")
}

func performUpload(u *uploader.Uploader) error {
func performUpload(u *uploader.Uploader, forced bool) error {
u.Log.Info("Running...")

var liveRotateParams []string
Expand All @@ -153,9 +191,14 @@ func performUpload(u *uploader.Uploader) error {
/* Generate Additional Rclone Params */
var additionalRcloneParams []string

if !flagNoCheck || u.Config.Check.Forced {
// if no-check is false (default) or check is forced via config, include check params
additionalRcloneParams = u.CheckRcloneParams()
switch forced {
case false:
if !flagNoCheck || u.Config.Check.Forced {
// if no-check is false (default) or check is forced via config, include check params
additionalRcloneParams = u.CheckRcloneParams()
}
default:
break
}

// add live rotate params set
Expand Down
11 changes: 6 additions & 5 deletions config/uploader.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package config

type UploaderCheck struct {
Forced bool
Type string
Limit uint64
Exclude []string
Include []string
Forced bool
MinFreeSpace uint64 `yaml:"min_free_space"`
Type string
Limit uint64
Exclude []string
Include []string
}

type UploaderHidden struct {
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ go 1.14
require (
github.com/DataDog/zstd v1.4.5 // indirect
github.com/ReneKroon/ttlcache v1.6.0
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/blang/semver v3.5.1+incompatible
github.com/dgraph-io/badger/v2 v2.0.3 // indirect
github.com/dgraph-io/ristretto v0.0.3 // indirect
github.com/dustin/go-humanize v1.0.0
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/go-cmd/cmd v1.2.1
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/gofiber/fiber v1.12.6
github.com/gofiber/recover v0.1.1
github.com/golang/protobuf v1.4.2 // indirect
Expand All @@ -23,6 +25,7 @@ require (
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
github.com/pkg/errors v0.9.1
github.com/rhysd/go-github-selfupdate v1.2.2
github.com/shirou/gopsutil v2.20.6+incompatible
github.com/sirupsen/logrus v1.6.0
github.com/sony/sonyflake v1.0.0 // indirect
github.com/spf13/cobra v1.0.0
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/ReneKroon/ttlcache v1.6.0 h1:aO+GDNVKTQmcuI0H78PXCR9E59JMiGfSXHAkVBUlzbA=
github.com/ReneKroon/ttlcache v1.6.0/go.mod h1:DG6nbhXKUQhrExfwwLuZUdH7UnRDDRA1IW+nBuCssvs=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4=
Expand Down Expand Up @@ -65,6 +67,8 @@ github.com/go-cmd/cmd v1.2.1/go.mod h1:F2yJeMVdy5ymftSgCR0zMN7XLhKFJpG5/1brXju8E
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-test/deep v1.0.6 h1:UHSEyLZUwX9Qoi99vVwvewiMC8mM2bf7XEM2nqvzEn8=
github.com/go-test/deep v1.0.6/go.mod h1:QV8Hv/iy04NyLBxAdO9njL0iVPN1S4d/A3NVv1V36o8=
Expand Down Expand Up @@ -195,6 +199,8 @@ github.com/rhysd/go-github-selfupdate v1.2.2/go.mod h1:khesvSyKcXDUxeySCedFh621i
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shirou/gopsutil v2.20.6+incompatible h1:P37G9YH8M4vqkKcwBosp+URN5O8Tay67D2MbR361ioY=
github.com/shirou/gopsutil v2.20.6+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
Expand Down
14 changes: 7 additions & 7 deletions uploader/checker/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ type Size struct{}

func (Size) Check(cfg *config.UploaderCheck, log *logrus.Entry, paths []pathutils.Path, size uint64) (*Result, error) {
// Check Total Size
s := humanize.Bytes(size)

if size > cfg.Limit {
s := humanize.Bytes(size)
log.WithFields(logrus.Fields{
"max_size": humanize.Bytes(cfg.Limit),
"current_size": s,
"over_size": humanize.Bytes(size - cfg.Limit),
}).Info("Size is greater than specified limit")

return &Result{
Passed: true,
Info: s,
}, nil
} else {
return &Result{
Passed: false,
Info: humanize.Bytes(cfg.Limit - size),
}, nil
}

return &Result{
Passed: false,
Info: humanize.Bytes(cfg.Limit - size),
}, nil
}

func (Size) CheckFile(cfg *config.UploaderCheck, log *logrus.Entry, path pathutils.Path, size uint64) (bool, error) {
Expand Down

0 comments on commit 6be4148

Please sign in to comment.