[new beat]Panic with the beat code


(Rémi Desgrange) #1

Hi,

I would monitor file and folder size.

So I said "ok I saw all this community beat, it must be not so hard :D".

At first I'm trying using filepath.Walk with a custom (simple) callback function like that :

package main

import (
	"os"
	"log"
	"fmt"
	"path/filepath"
)

type FileSize struct {
	size int64
	nbFile int64
	nbDir int64
}

func (st *FileSize) MyWalk(path string, info os.FileInfo, err error) error {
	if info.IsDir() {
    st.nbDir += 1
  } else {
    st.nbFile += 1
    st.size += info.Size()
  }
	return nil
}

func main() {
	paths := []string{"./", "../"}
	for _, path := range paths {
		var st FileSize
		err := filepath.Walk(path, st.MyWalk)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("total size %d, nb file %d, nb folder %d", st.size, st.nbFile, st.nbDir)
	}
}

it is dumb, but it works.

the gh repo is here it's certainly not perfect and lack of comment, but the code is pretty much the same, and panic...

I know that a great idea would be to use fsnotify. But maybe I will try to get it working and then use fsnotify.

In advance thanks


(Steffen Siering) #2

So your beat is failing with a panic? I didn't test it yet, but can you also add a stack-trace? You are running on windows?


(Rémi Desgrange) #3

The stack trace is prety heavy. Maybe I need to use channel ? At first I just want to do something that's working ,that's why I didn't use channel, goroutine etc... but if I need to...

stack trace :


(Steffen Siering) #4

I see you're not handling errors in your walk function callback. The stack trace indicates 'info' being nil. With os.FileInfo being an interface, it might be nil if an error occured while walking your tree.

For some debugging you can try to insert logp.Debug("filesize", "path=%v, info=%v, err=%v\n", path, info, err) and run your beat with -d "filesize".


(Rémi Desgrange) #5

thanks, I rewrote the method this morning. I'm currently cleaning the fmt.Printf debug message, i'm doing to commit very soon. Thanks anyway. I have to admit that this is my first "real" go code, and the stack trace scared me a bit :wink:


(system) #6