making changes to common library, starting to integrate database functions
This commit is contained in:
		@@ -2,12 +2,11 @@ package engine
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	logger "github.com/apsdehal/go-logger"
 | 
			
		||||
	radovskyb "github.com/radovskyb/watcher"
 | 
			
		||||
	"github.com/deranjer/gvc/common/database"
 | 
			
		||||
	watcher "github.com/radovskyb/watcher"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type key string
 | 
			
		||||
@@ -23,9 +22,9 @@ 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) (Watcher, error) {
 | 
			
		||||
	w := Watcher{
 | 
			
		||||
		radovskyb.New(),
 | 
			
		||||
func NewWatcher(logger *logger.Logger, KEYFOLDER, DOWNLOADFOLDER, SYNCFOLDER, THUMBFOLDER, DIFFFOLDER string) (FileWatcher, error) {
 | 
			
		||||
	w := FileWatcher{
 | 
			
		||||
		watcher.New(),
 | 
			
		||||
		logger,
 | 
			
		||||
		true, //used to temporarily ignore events if necessary
 | 
			
		||||
		KEYFOLDER, DOWNLOADFOLDER, SYNCFOLDER, THUMBFOLDER, DIFFFOLDER,
 | 
			
		||||
@@ -33,16 +32,16 @@ func NewWatcher(logger *logger.Logger, KEYFOLDER, DOWNLOADFOLDER, SYNCFOLDER, TH
 | 
			
		||||
	return w, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (w *Watcher) Ignore() bool {
 | 
			
		||||
	w.Enabled = false
 | 
			
		||||
	return w.Enabled
 | 
			
		||||
func (fw *FileWatcher) Ignore() bool {
 | 
			
		||||
	fw.Enabled = false
 | 
			
		||||
	return fw.Enabled
 | 
			
		||||
}
 | 
			
		||||
func (w *Watcher) Enable() bool {
 | 
			
		||||
	w.Enabled = true
 | 
			
		||||
	return w.Enabled
 | 
			
		||||
func (fw *FileWatcher) Enable() bool {
 | 
			
		||||
	fw.Enabled = true
 | 
			
		||||
	return fw.Enabled
 | 
			
		||||
}
 | 
			
		||||
func (w *Watcher) IsEnabled() bool {
 | 
			
		||||
	return w.Enabled
 | 
			
		||||
func (fw *FileWatcher) IsEnabled() bool {
 | 
			
		||||
	return fw.Enabled
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// BeginWatcherRoutine kicks off the watcher. When the watcher noticies a file change,
 | 
			
		||||
@@ -51,45 +50,31 @@ func (w *Watcher) IsEnabled() bool {
 | 
			
		||||
// If certain functions need to be called then this will
 | 
			
		||||
// need to be specified as part of the managers lambda functions
 | 
			
		||||
// TODO: Should return an error
 | 
			
		||||
func (w *Watcher) BeginWatcherRoutine(ctx context.Context, wg *sync.WaitGroup, diffChannel chan utilities.DiffObject, onFileChanged func(string) (utilities.File, error)) {
 | 
			
		||||
func (fw *FileWatcher) BeginWatcherRoutine(ctx context.Context, wg *sync.WaitGroup, diffChannel chan database.DiffObject, onFileChanged func(string) (database.File, error)) {
 | 
			
		||||
 | 
			
		||||
	//seems a bit barking, but we can now cancel any diff that is occuring on a file when it fires again
 | 
			
		||||
	cancelFunctions := make(map[string]func())
 | 
			
		||||
	for {
 | 
			
		||||
		select {
 | 
			
		||||
		// we have filtered already on the [Op]erations we want to listen for so no need to check here
 | 
			
		||||
		case event := <-w.Event:
 | 
			
		||||
			if !w.IsEnabled() {
 | 
			
		||||
				w.Infof("ignoring event and reenabling the watcher %s\r\n", event)
 | 
			
		||||
				w.Enable()
 | 
			
		||||
		case event := <-fw.Watcher.Event:
 | 
			
		||||
			if !fw.IsEnabled() {
 | 
			
		||||
				fw.Infof("ignoring event and reenabling the watcher %s\r\n", event)
 | 
			
		||||
				fw.Enable()
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
			w.Infof("event fired ", event)
 | 
			
		||||
			fw.Infof("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
 | 
			
		||||
			syncFilePath := fileInfo.CurrentBase
 | 
			
		||||
			uniqueName := fileInfo.Unique
 | 
			
		||||
			// //begin taking screenshot if we are supposed to
 | 
			
		||||
			screenshotChannel := make(chan utilities.ScreenshotWrapper)
 | 
			
		||||
			go func(ssChannel chan utilities.ScreenshotWrapper) {
 | 
			
		||||
				w.Infof("beginning taking screenshot at ", time.Now())
 | 
			
		||||
				var ssStruct utilities.ScreenshotWrapper
 | 
			
		||||
				if screenshotFileName, err := takeScreenShot(w.THUMBFOLDER, uniqueName); err != nil {
 | 
			
		||||
					w.WarningF("could not take screenshot", err)
 | 
			
		||||
					ssStruct.ScreenshotError = err
 | 
			
		||||
				} else {
 | 
			
		||||
					ssStruct.Screenshot = filepath.Join(w.THUMBFOLDER, screenshotFileName)
 | 
			
		||||
					w.Infof("screenshot recorded ", ssStruct.Screenshot, " at ", time.Now())
 | 
			
		||||
				}
 | 
			
		||||
				ssChannel <- ssStruct
 | 
			
		||||
			}(screenshotChannel)
 | 
			
		||||
			//uniqueName := fileInfo.Unique
 | 
			
		||||
 | 
			
		||||
			// fileID := fileInfo.ID
 | 
			
		||||
			//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 {
 | 
			
		||||
				w.ErrorF("path was not returned to sync path", err)
 | 
			
		||||
				fw.ErrorF("path was not returned to sync path", err)
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
			//cancel the event if it indeed is running...
 | 
			
		||||
@@ -118,14 +103,14 @@ func (w *Watcher) BeginWatcherRoutine(ctx context.Context, wg *sync.WaitGroup, d
 | 
			
		||||
				Total:    100,
 | 
			
		||||
			}
 | 
			
		||||
			eventContext := context.WithValue(cancelContext, key(event.Path), e)
 | 
			
		||||
			if err := manageFileDiffing(eventContext, event.Path, syncFilePath, w.DIFFFOLDER, true, screenshotChannel, diffChannel, wg); err != nil {
 | 
			
		||||
			if err := manageFileDiffing(eventContext, event.Path, syncFilePath, fw.DIFFFOLDER, true, diffChannel, wg); err != nil {
 | 
			
		||||
				// I don't think this can be reached...
 | 
			
		||||
				w.WarningF("Error managing the diffing process %s", err)
 | 
			
		||||
				fw.WarningF("Error managing the diffing process %s", err)
 | 
			
		||||
			}
 | 
			
		||||
		case err := <-w.Watcher.Error:
 | 
			
		||||
			w.ErrorF("%s\r\n", err)
 | 
			
		||||
		case <-w.Closed:
 | 
			
		||||
			w.Notice("radovskyb closed")
 | 
			
		||||
		case err := <-fw.Watcher.Error:
 | 
			
		||||
			fw.Errorf("%s\r\n", err)
 | 
			
		||||
		case <-fw.Watcher.Closed:
 | 
			
		||||
			fw.Notice("radovskyb closed")
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user