Last active
August 14, 2020 15:05
-
-
Save Hypro999/e272e969484f7ba95bf5d978d284b7a2 to your computer and use it in GitHub Desktop.
A simple concurrent go program to clear out (log)files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"os" | |
"path/filepath" | |
"sync" | |
"time" | |
) | |
var ( | |
// This will be set once (controlled via. command line flags) in the main function | |
// then never modified again. Thus multiple concurrent readers are allowed. | |
Mock = false | |
// This will be set once (controlled via. command line flags) in the main function | |
// then never modified again. | |
RemovalThreshold = 7 | |
) | |
func shouldBeRemoved(dirent os.FileInfo) bool { | |
if int(time.Since(dirent.ModTime()).Hours()) > (RemovalThreshold * 24) { | |
return true | |
} | |
return false | |
} | |
func remove(dirname string, dirent os.FileInfo, wg *sync.WaitGroup) { | |
dirpath := filepath.Join(dirname, dirent.Name()) | |
if Mock { | |
fmt.Println(dirpath) | |
} else { | |
err := os.Remove(dirpath) | |
if err != nil { | |
if os.IsPermission(err) { | |
fmt.Printf("Could not remove %s due to insufficient permissions.\n", dirpath) | |
} else { | |
log.Println(err) | |
} | |
} else { | |
fmt.Printf("Removed %s\n", dirpath) | |
} | |
} | |
wg.Done() | |
} | |
func clearLogDir(dirname string, wg *sync.WaitGroup) { | |
dirents, err := ioutil.ReadDir(dirname) | |
if err != nil { | |
log.Println(err) | |
wg.Done() | |
return | |
} | |
for _, dirent := range dirents { | |
if dirent.IsDir() { | |
// Recursively walk all subdirectories (in a concurrent manner). | |
wg.Add(1) | |
go clearLogDir(filepath.Join(dirname, dirent.Name()), wg) | |
} else if shouldBeRemoved(dirent) { | |
// Remove the file. Use another goroutine since file removal will require a syscall. | |
wg.Add(1) | |
go remove(dirname, dirent, wg) | |
} | |
} | |
wg.Done() | |
} | |
func main() { | |
flag.BoolVar(&Mock, "mock", Mock, "Set this flag if you want to perform a dry/mock run.") | |
flag.IntVar(&RemovalThreshold, "days", RemovalThreshold, "The minimum age of a file relative to now for it to be deleted.") | |
flag.Parse() | |
var wg sync.WaitGroup | |
wg.Add(1) | |
go clearLogDir("/var/log/", &wg) | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment