starting to write the manager library
This commit is contained in:
		@@ -4,9 +4,9 @@ import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"sync"
 | 
			
		||||
 | 
			
		||||
	logger "github.com/apsdehal/go-logger"
 | 
			
		||||
	"github.com/deranjer/gvc/common/database"
 | 
			
		||||
	watcher "github.com/radovskyb/watcher"
 | 
			
		||||
	"github.com/rs/zerolog"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type key string
 | 
			
		||||
@@ -22,7 +22,7 @@ type Event struct {
 | 
			
		||||
// * copying any versions and keeping them safe (even if temporary)
 | 
			
		||||
// * creating the diff of the file, in both directions if necessary
 | 
			
		||||
// * storing the details in the database
 | 
			
		||||
func NewWatcher(logger *logger.Logger, KEYFOLDER, DOWNLOADFOLDER, SYNCFOLDER, THUMBFOLDER, DIFFFOLDER string) (FileWatcher, error) {
 | 
			
		||||
func NewWatcher(logger *zerolog.Logger, KEYFOLDER, DOWNLOADFOLDER, SYNCFOLDER, THUMBFOLDER, DIFFFOLDER string) (FileWatcher, error) {
 | 
			
		||||
	w := FileWatcher{
 | 
			
		||||
		watcher.New(),
 | 
			
		||||
		logger,
 | 
			
		||||
@@ -59,11 +59,11 @@ func (fw *FileWatcher) BeginWatcherRoutine(ctx context.Context, wg *sync.WaitGro
 | 
			
		||||
		// we have filtered already on the [Op]erations we want to listen for so no need to check here
 | 
			
		||||
		case event := <-fw.Watcher.Event:
 | 
			
		||||
			if !fw.IsEnabled() {
 | 
			
		||||
				fw.Infof("ignoring event and reenabling the watcher %s\r\n", event)
 | 
			
		||||
				fw.Info().Msgf("ignoring event and reenabling the watcher %s\r\n", event)
 | 
			
		||||
				fw.Enable()
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
			fw.Infof("event fired ", event)
 | 
			
		||||
			fw.Info().Msgf("event fired ", event)
 | 
			
		||||
			//this is currently slow as it does a db lookup on the path.
 | 
			
		||||
			//TODO: On load (or whenever a file is added to the watcher, the db information for files being watched, could be cached in memory. This would be much faster)
 | 
			
		||||
			fileInfo, err := onFileChanged(event.Path) //could return the 'Event' object here
 | 
			
		||||
@@ -74,7 +74,7 @@ func (fw *FileWatcher) BeginWatcherRoutine(ctx context.Context, wg *sync.WaitGro
 | 
			
		||||
			//we need the hash of the current base, not the hash of the original file
 | 
			
		||||
			// fileHash := fileInfo.CurrentHash //hash needs to come from
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				fw.ErrorF("path was not returned to sync path", err)
 | 
			
		||||
				fw.Err(err).Msg("path was not returned to sync path")
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
			//cancel the event if it indeed is running...
 | 
			
		||||
@@ -105,12 +105,12 @@ func (fw *FileWatcher) BeginWatcherRoutine(ctx context.Context, wg *sync.WaitGro
 | 
			
		||||
			eventContext := context.WithValue(cancelContext, key(event.Path), e)
 | 
			
		||||
			if err := manageFileDiffing(eventContext, event.Path, syncFilePath, fw.DIFFFOLDER, true, diffChannel, wg); err != nil {
 | 
			
		||||
				// I don't think this can be reached...
 | 
			
		||||
				fw.WarningF("Error managing the diffing process %s", err)
 | 
			
		||||
				fw.Warn().Msgf("Error managing the diffing process %s", err)
 | 
			
		||||
			}
 | 
			
		||||
		case err := <-fw.Watcher.Error:
 | 
			
		||||
			fw.Errorf("%s\r\n", err)
 | 
			
		||||
			fw.Err(err)
 | 
			
		||||
		case <-fw.Watcher.Closed:
 | 
			
		||||
			fw.Notice("radovskyb closed")
 | 
			
		||||
			//fw.Notice("radovskyb closed")
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user