소스 검색

Migrated the scheduler for Hybridbackup from core scheduler to internal one

tobychui 4 년 전
부모
커밋
818e596332
8개의 변경된 파일76개의 추가작업 그리고 56개의 파일을 삭제
  1. 1 1
      file_system.go
  2. 0 0
      mod/disk/hybridBackup/fastwalk.go
  3. 0 0
      mod/disk/hybridBackup/fileCopy.go
  4. 67 47
      mod/disk/hybridBackup/hybridBackup.go
  5. 1 1
      mod/filesystem/filesystem.go
  6. 1 1
      mod/storage/storage.go
  7. 5 5
      startup.go
  8. 1 1
      storage.go

+ 1 - 1
file_system.go

@@ -24,10 +24,10 @@ import (
 	"github.com/gorilla/websocket"
 	uuid "github.com/satori/go.uuid"
 
+	"imuslab.com/arozos/mod/disk/hybridBackup"
 	fs "imuslab.com/arozos/mod/filesystem"
 	fsp "imuslab.com/arozos/mod/filesystem/fspermission"
 	hidden "imuslab.com/arozos/mod/filesystem/hidden"
-	"imuslab.com/arozos/mod/filesystem/hybridBackup"
 	metadata "imuslab.com/arozos/mod/filesystem/metadata"
 	module "imuslab.com/arozos/mod/modules"
 	prout "imuslab.com/arozos/mod/prouter"

+ 0 - 0
mod/filesystem/hybridBackup/fastwalk.go → mod/disk/hybridBackup/fastwalk.go


+ 0 - 0
mod/filesystem/hybridBackup/fileCopy.go → mod/disk/hybridBackup/fileCopy.go


+ 67 - 47
mod/filesystem/hybridBackup/hybridBackup.go → mod/disk/hybridBackup/hybridBackup.go

@@ -35,7 +35,9 @@ import (
 */
 
 type Manager struct {
-	Tasks []*BackupTask
+	Ticker     *time.Ticker  //The main ticker
+	StopTicker chan bool     //Channel for stopping the backup
+	Tasks      []*BackupTask //The backup tasks that is running under this manager
 }
 
 type BackupTask struct {
@@ -64,52 +66,65 @@ type RestorableReport struct {
 	RestorableFiles []RestorableFile //A list of restorable files
 }
 
+var (
+	internalTickerTime time.Duration = 60
+)
+
 func NewHyperBackupManager() *Manager {
-	return &Manager{
-		Tasks: []*BackupTask{},
+	//Create a new minute ticker
+	ticker := time.NewTicker(internalTickerTime * time.Second)
+	stopper := make(chan bool, 1)
+
+	newManager := &Manager{
+		Ticker:     ticker,
+		StopTicker: stopper,
+		Tasks:      []*BackupTask{},
 	}
-}
 
-func (m *Manager) AddTask(newtask *BackupTask) error {
-	log.Println(">>>> [Debug] New Backup Tasks added: ", newtask)
-
-	/*for _, thisHandler := range fsHandlers {
+	///Create task executor
+	go func() {
+		defer log.Println("[HybridBackup] Ticker Stopped")
+		for {
+			select {
+			case <-ticker.C:
+				for _, task := range newManager.Tasks {
+					task.HandleBackupProcess()
+				}
+			case <-stopper:
+				return
+			}
+		}
+	}()
 
-			if thisHandler.Hierarchy == "backup" {
-				//This is a backup drive. Generate it handler
-				backupConfig := thisHandler.HierarchyConfig.(hybridBackup.BackupTask)
+	//Return the manager
+	return newManager
+}
 
-				//Get its parent mount point for backup
-				parentFileSystemHandler, err := GetFsHandlerByUUID(backupConfig.ParentUID)
-				if err != nil {
-					log.Println("Virtual Root with UUID: " + backupConfig.ParentUID + " not loaded. Unable to start backup process.")
-					break
-				}
+func (m *Manager) AddTask(newtask *BackupTask) error {
+	//Create a job for this
+	newtask.JobName = "backup-[" + newtask.DiskUID + "]"
 
-				backupConfig.JobName = "backup-daemon [" + thisHandler.UUID + "]"
-				backupConfig.ParentPath = parentFileSystemHandler.Path
-				backupConfig.CycleCounter = 1
+	//Check if the same job name exists
+	for _, task := range m.Tasks {
+		if task.JobName == newtask.JobName {
+			return errors.New("Task already exists")
+		}
+	}
 
-				//Debug backup execution
-				hybridBackup.HandleBackupProcess(&backupConfig)
+	m.Tasks = append(m.Tasks, newtask)
 
-				//Remove the previous job if it exists
-				if systemScheduler.JobExists(backupConfig.JobName) {
-					systemScheduler.RemoveJobFromScheduleList(backupConfig.JobName)
-				}
+	log.Println(">>>> [Debug] New Backup Tasks added: ", newtask.JobName, newtask)
 
-				//Create a scheudler for this disk
-				systemScheduler.CreateNewScheduledFunctionJob(backupConfig.JobName,
-					"Backup daemon from "+backupConfig.ParentUID+":/ to "+backupConfig.DiskUID+":/",
-					60,
-					func() (string, error) {
-						return hybridBackup.HandleBackupProcess(&backupConfig)
-					},
-				)
-			}
+	return nil
+}
 
-	}*/
+func (m *Manager) StopTask(jobname string) error {
+	return nil
+}
 
+//Stop all managed handlers
+func (m *Manager) Close() error {
+	m.StopTicker <- true
 	return nil
 }
 
@@ -166,7 +181,7 @@ func executeBackup(backupConfig *BackupTask, deepBackup bool) (string, error) {
 				//Copy the file to target
 				err := BufferedLargeFileCopy(fileAbs, assumedTargetPosition, 1024)
 				if err != nil {
-					log.Println("*Hybrid Backup* Copy Failed for file "+filepath.Base(fileAbs), err.Error(), " Skipping.")
+					log.Println("[HybridBackup] Copy Failed for file "+filepath.Base(fileAbs), err.Error(), " Skipping.")
 				} else {
 					//No problem. Add this filepath into the list
 					copiedFileList = append(copiedFileList, assumedTargetPosition)
@@ -175,10 +190,15 @@ func executeBackup(backupConfig *BackupTask, deepBackup bool) (string, error) {
 		} else {
 			//Deep copy. Check and match the modtime of each file
 			if !fileExists(assumedTargetPosition) {
+				if !fileExists(filepath.Dir(assumedTargetPosition)) {
+					//Folder containing this file not exists. Create it
+					os.MkdirAll(filepath.Dir(assumedTargetPosition), 0755)
+				}
+
 				//Copy the file to target
 				err := BufferedLargeFileCopy(fileAbs, assumedTargetPosition, 1024)
 				if err != nil {
-					log.Println("*Hybrid Backup* Copy Failed for file "+filepath.Base(fileAbs), err.Error(), " Skipping.")
+					log.Println("[HybridBackup] Copy Failed for file "+filepath.Base(fileAbs), err.Error(), " Skipping.")
 					return nil
 				} else {
 					//No problem. Add this filepath into the list
@@ -188,12 +208,12 @@ func executeBackup(backupConfig *BackupTask, deepBackup bool) (string, error) {
 				//Target file already exists. Check if their hash matches
 				srcHash, err := getFileHash(fileAbs)
 				if err != nil {
-					log.Println("*Hybrid Backup* Hash calculation failed for file "+filepath.Base(fileAbs), err.Error(), " Skipping.")
+					log.Println("[HybridBackup] Hash calculation failed for file "+filepath.Base(fileAbs), err.Error(), " Skipping.")
 					return nil
 				}
 				targetHash, err := getFileHash(assumedTargetPosition)
 				if err != nil {
-					log.Println("*Hybrid Backup* Hash calculation failed for file "+filepath.Base(assumedTargetPosition), err.Error(), " Skipping.")
+					log.Println("[HybridBackup] Hash calculation failed for file "+filepath.Base(assumedTargetPosition), err.Error(), " Skipping.")
 					return nil
 				}
 
@@ -202,7 +222,7 @@ func executeBackup(backupConfig *BackupTask, deepBackup bool) (string, error) {
 					//This file has been recently changed. Copy it to new location
 					err = BufferedLargeFileCopy(fileAbs, assumedTargetPosition, 1024)
 					if err != nil {
-						log.Println("*Hybrid Backup* Copy Failed for file "+filepath.Base(fileAbs), err.Error(), " Skipping.")
+						log.Println("[HybridBackup] Copy Failed for file "+filepath.Base(fileAbs), err.Error(), " Skipping.")
 					} else {
 						//No problem. Add this filepath into the list
 						copiedFileList = append(copiedFileList, assumedTargetPosition)
@@ -259,7 +279,7 @@ func executeBackup(backupConfig *BackupTask, deepBackup bool) (string, error) {
 }
 
 //Main handler function for hybrid backup
-func HandleBackupProcess(backupConfig *BackupTask) (string, error) {
+func (backupConfig *BackupTask) HandleBackupProcess() (string, error) {
 	log.Println(">>>>>> [Debug] Running backup process: ", backupConfig)
 
 	//Check if the target disk is writable and mounted
@@ -268,13 +288,13 @@ func HandleBackupProcess(backupConfig *BackupTask) (string, error) {
 
 	} else {
 		//File system not mounted even after 3 backup cycle. Terminate backup scheduler
-		log.Println("*HybridBackup* Skipping backup cycle for " + backupConfig.ParentUID + ":/")
+		log.Println("[HybridBackup] Skipping backup cycle for " + backupConfig.ParentUID + ":/")
 		return "Parent drive (" + backupConfig.ParentUID + ":/) not mounted", nil
 	}
 
 	//Check if the backup disk is mounted. If no, stop the scheulder
 	if backupConfig.CycleCounter > 3 && !(fileExists(filepath.Join(backupConfig.DiskPath, "aofs.db")) && fileExists(filepath.Join(backupConfig.DiskPath, "aofs.db.lock"))) {
-		log.Println("*HybridBackup* Backup schedule stopped for " + backupConfig.DiskUID + ":/")
+		log.Println("[HybridBackup] Backup schedule stopped for " + backupConfig.DiskUID + ":/")
 		return "Backup drive (" + backupConfig.DiskUID + ":/) not mounted", errors.New("Backup File System Handler not mounted")
 	}
 
@@ -309,14 +329,14 @@ func HandleBackupProcess(backupConfig *BackupTask) (string, error) {
 }
 
 //Restore accidentailly removed file from backup
-func HandleRestore(backupConfig *BackupTask, targetFile string) error {
+func HandleRestore(parentDiskID string, restoreDiskID string, targetFileRelpath string) error {
 
 	return nil
 }
 
 //List the file that is restorable from the given disk
-func ListRestorable(backupConfig *BackupTask) {
-
+func (m *Manager) ListRestorable(parentDiskID string) RestorableReport {
+	return RestorableReport{}
 }
 
 //Get and return the file hash for a file

+ 1 - 1
mod/filesystem/filesystem.go

@@ -19,7 +19,7 @@ import (
 	"time"
 
 	db "imuslab.com/arozos/mod/database"
-	"imuslab.com/arozos/mod/filesystem/hybridBackup"
+	"imuslab.com/arozos/mod/disk/hybridBackup"
 )
 
 //Options for creating new file system handler

+ 1 - 1
mod/storage/storage.go

@@ -12,8 +12,8 @@ import (
 	"log"
 	"os"
 
+	"imuslab.com/arozos/mod/disk/hybridBackup"
 	fs "imuslab.com/arozos/mod/filesystem"
-	"imuslab.com/arozos/mod/filesystem/hybridBackup"
 )
 
 type StoragePool struct {

+ 5 - 5
startup.go

@@ -49,11 +49,11 @@ func RunStartup() {
 	PackagManagerInit() //Start APT service agent
 
 	//7. Kickstart the File System and Desktop
-	SchedulerInit()        //Start System Scheudler
-	FileSystemInit()       //Start FileSystem
-	DesktopInit()          //Start Desktop
-	HardwarePowerInit()    //Start host power manager
-	FilesystemDaemonInit() //Start File System handler daemon (for backup and other sync process)
+	SchedulerInit()     //Start System Scheudler
+	FileSystemInit()    //Start FileSystem
+	DesktopInit()       //Start Desktop
+	HardwarePowerInit() //Start host power manager
+	StorageDaemonInit() //Start File System handler daemon (for backup and other sync process)
 
 	//8 Start AGI and Subservice modules (Must start after module)
 	AGIInit()        //ArOZ Javascript Gateway Interface, must start after fs

+ 1 - 1
storage.go

@@ -108,7 +108,7 @@ func LoadBaseStoragePool() error {
 
 	This function must be called after the scheduler initiated.
 */
-func FilesystemDaemonInit() {
+func StorageDaemonInit() {
 
 }