Skip to content

Commit

Permalink
feat: added process limit, so your pc doesn't turn into an aviation e…
Browse files Browse the repository at this point in the history
…ngine
  • Loading branch information
mysterion committed Sep 6, 2024
1 parent 2a0722c commit b7f4eba
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 122 deletions.
13 changes: 5 additions & 8 deletions avrp.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,24 @@ func main() {
flag.BoolVar(&reset, "reset", false, "removes all configs, thumbnails & 'aframe-vr-player' files")
flag.IntVar(&port, "port", 5000, "port to serve on (default 5000)")
flag.BoolVar(&getFfmpeg, "get-ffmpeg", false, "downloads ffmpeg")
flag.BoolVar(&noThumb, "no-thumb", false, "disables thumbnail generation")
flag.BoolVar(&noThumb, "no-thumb", false, "disable/enable thumbnail generation")

flag.Parse()

utils.Init()
dist.Init()
thumbnails.Init()

// commands 👇

if getFfmpeg {
utils.Panic(thumbnails.DownloadFfmpeg())
return
}

if noThumb {
if thumbnails.NoFfmpeg() {
thumbnails.NoFfmpegFileRemove()
if thumbnails.NoThumb() {
thumbnails.NoThumbFileRemove()
log.Println("thumbnail generation enabled👍")
} else {
thumbnails.NoFfmpegFileCreate()
thumbnails.NoThumbFileCreate()
log.Println("thumbnail generation disabled👎")
}
return
Expand All @@ -96,7 +93,6 @@ func main() {
dist.Update(sha)
return
}

// commands 👆

// running for the first time
Expand All @@ -112,6 +108,7 @@ func main() {
}
}

thumbnails.Init()
server.Init(servDir)
server.Start(port)
}
8 changes: 1 addition & 7 deletions internal/dist/dist.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ import (
"github.com/mysterion/avrp/internal/utils"
)

var VersionFile string

func Init() {
VersionFile = filepath.Join(utils.ConfigDir, "VERSION")
}

// checks if dist is present
func Valid() bool {
_, err := os.Stat(filepath.Join(utils.DistDir, "index.html"))
Expand All @@ -26,7 +20,7 @@ func Delete() error {

// returns sha of aframe-vr-player dist
func Ver() string {
d, err := os.ReadFile(VersionFile)
d, err := os.ReadFile(utils.VersionFile)

if err != nil {
log.Printf("WARN: while reading dist version, %v\n", err.Error())
Expand Down
2 changes: 1 addition & 1 deletion internal/dist/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func DownloadCommit(sha string) error {
return err
}

err = os.WriteFile(VersionFile, []byte(sha), 0644)
err = os.WriteFile(utils.VersionFile, []byte(sha), 0644)

if err != nil {
log.Println("ERR: Failed to write version file")
Expand Down
70 changes: 41 additions & 29 deletions internal/thumbnails/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ var (
binFfprobe string
)

var (
noffmpegfile string
ffmpegDir = ""
)

type release struct {
ID int `json:"id"`
Name string `json:"name"`
Expand Down Expand Up @@ -143,8 +138,8 @@ func DownloadFfmpeg() error {

log.Println("Extracted successfully")

if NoFfmpeg() {
NoFfmpegFileRemove()
if NoThumb() {
NoThumbFileRemove()
}

return nil
Expand Down Expand Up @@ -202,7 +197,11 @@ func promptDownloadFfmpeg() bool {
return ans == "yes"
}

func CheckFfmpegInPath() (bool, string, string) {
func initFfmpeg() bool {

if NoThumb() {
return false
}

ffmpeg := "ffmpeg"
ffprobe := "ffprobe"
Expand All @@ -212,43 +211,56 @@ func CheckFfmpegInPath() (bool, string, string) {
ffprobe += ".exe"
}

ffmpegPath, err1 := exec.LookPath(ffmpeg)
var err1, err2 error

ffprobePath, err2 := exec.LookPath(ffprobe)
binFfmpeg, err1 = exec.LookPath(ffmpeg)
binFfprobe, err2 = exec.LookPath(ffprobe)

return err1 == nil && err2 == nil, ffmpegPath, ffprobePath
}
if err1 == nil && err2 == nil {
log.Println("ffmpeg found in $PATH")
return true
}

func CheckFfmpeg() (bool, string, string) {
binFfmpeg = filepath.Join(utils.FfmpegDir, "bin", ffmpeg)
binFfprobe = filepath.Join(utils.FfmpegDir, "bin", ffprobe)

ffmpeg := filepath.Join("bin", "ffmpeg")
ffprobe := filepath.Join("bin", "ffprobe")
_, err1 = os.Stat(binFfmpeg)
_, err2 = os.Stat(binFfprobe)

if runtime.GOOS == "windows" {
ffmpeg += ".exe"
ffprobe += ".exe"
if err1 == nil && err2 == nil {
log.Println("ffmpeg found in the ffmpegDir")
return true
}

ffmpegPath := filepath.Join(ffmpegDir, ffmpeg)
ffprobePath := filepath.Join(ffmpegDir, ffprobe)
// code to download ffmpeg download 👇👇👇👇
accept := promptDownloadFfmpeg()

if !accept {
fmt.Printf("\n\nYou can disable this message, by running: avrp --no-thumb\n\n")
return false
}

_, err1 := os.Stat(ffmpegPath)
_, err2 := os.Stat(ffprobePath)
err := DownloadFfmpeg()

if err != nil {
log.Println("ERR: Failed to download ffmpeg")
return false
}

return err1 == nil && err2 == nil, ffmpegPath, ffprobePath
return true
}

func NoFfmpegFileCreate() {
_, err := os.Create(noffmpegfile)
func NoThumbFileCreate() {
_, err := os.Create(utils.NoThumbFile)
utils.Panic(err)
}

func NoFfmpegFileRemove() {
err := os.Remove(noffmpegfile)
func NoThumbFileRemove() {
err := os.Remove(utils.NoThumbFile)
utils.Panic(err)
}

func NoFfmpeg() bool {
_, err := os.Stat(noffmpegfile)
func NoThumb() bool {
_, err := os.Stat(utils.NoThumbFile)
return err == nil
}
76 changes: 76 additions & 0 deletions internal/thumbnails/guard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package thumbnails

import (
"log"
"os"
"runtime"
"strconv"
"sync"
)

/* Controls the no of concurrent processes used to generate thumbnails */
type GuardProcs struct {
g chan struct{}
max int
}

func NewGuardProcs() GuardProcs {

maxProcs := runtime.NumCPU()
maxProcsEnv := os.Getenv("AVRP_MAX_PROCS")
if maxProcsEnv != "" {
mp, err := strconv.Atoi(maxProcsEnv)
if err == nil {
maxProcs = mp
}
}

log.Printf("Using %v threads for thumbnail generation\n", maxProcs)

return GuardProcs{
g: make(chan struct{}, maxProcs),
max: maxProcs,
}

}

func (p *GuardProcs) MaxProcs() int {
return p.max
}

func (p *GuardProcs) In() {
p.g <- struct{}{}
}

func (p *GuardProcs) Out() {
<-p.g
}

type GuardFile struct {
locks map[string]*sync.Mutex
}

func NewGuardFile() GuardFile {
return GuardFile{
locks: make(map[string]*sync.Mutex),
}
}

func (p *GuardFile) Lock(key string) {
l, exists := p.locks[key]
if exists {
l.Lock()
} else {
p.locks[key] = &sync.Mutex{}
p.locks[key].Lock()
}
}

func (p *GuardFile) Unlock(key string) {
l, exists := p.locks[key]
if exists {
l.Unlock()
} else {
log.Printf("ERR: %s not found in GuardFile\n", key)
}
}
Loading

0 comments on commit b7f4eba

Please sign in to comment.