aliyunpan/internal/syncdrive/sync_task_mgr.go

185 lines
4.9 KiB
Go
Raw Normal View History

2022-05-24 19:56:06 +08:00
package syncdrive
import (
"encoding/json"
"fmt"
"github.com/tickstep/aliyunpan-api/aliyunpan"
2022-06-14 21:05:40 +08:00
"github.com/tickstep/aliyunpan/internal/config"
2022-05-24 19:56:06 +08:00
"github.com/tickstep/aliyunpan/internal/utils"
"github.com/tickstep/library-go/logger"
"io/ioutil"
"path"
2022-06-09 19:58:16 +08:00
"time"
2022-05-24 19:56:06 +08:00
)
type (
// SyncTaskManager 同步任务管理器
SyncTaskManager struct {
2022-06-10 13:55:00 +08:00
syncDriveConfig *SyncDriveConfig
fileDownloadParallel int
fileUploadParallel int
fileDownloadBlockSize int64
fileUploadBlockSize int64
2022-06-10 14:36:52 +08:00
useInternalUrl bool
2022-06-10 13:55:00 +08:00
2022-06-12 17:03:05 +08:00
maxDownloadRate int64 // 限制最大下载速度
maxUploadRate int64 // 限制最大上传速度
2022-06-14 21:05:40 +08:00
PanUser *config.PanUser
2022-05-24 19:56:06 +08:00
DriveId string
PanClient *aliyunpan.PanClient
SyncConfigFolderPath string
2022-06-17 10:27:53 +08:00
// useConfigFile 是否使用配置文件启动
useConfigFile bool
2022-05-24 19:56:06 +08:00
}
// SyncDriveConfig 同步盘配置文件
SyncDriveConfig struct {
ConfigVer string `json:"configVer"`
SyncTaskList []*SyncTask `json:"syncTaskList"`
}
)
var (
ErrSyncTaskListEmpty error = fmt.Errorf("no sync task")
)
2022-06-14 21:05:40 +08:00
func NewSyncTaskManager(user *config.PanUser, driveId string, panClient *aliyunpan.PanClient, syncConfigFolderPath string,
2022-06-12 17:03:05 +08:00
fileDownloadParallel, fileUploadParallel int, fileDownloadBlockSize, fileUploadBlockSize int64, useInternalUrl bool,
maxDownloadRate, maxUploadRate int64) *SyncTaskManager {
2022-05-24 19:56:06 +08:00
return &SyncTaskManager{
2022-06-14 21:05:40 +08:00
PanUser: user,
2022-05-24 19:56:06 +08:00
DriveId: driveId,
PanClient: panClient,
SyncConfigFolderPath: syncConfigFolderPath,
2022-06-10 13:55:00 +08:00
2022-06-10 14:36:52 +08:00
fileDownloadParallel: fileDownloadParallel,
fileUploadParallel: fileUploadParallel,
2022-06-10 13:55:00 +08:00
fileDownloadBlockSize: fileDownloadBlockSize,
fileUploadBlockSize: fileUploadBlockSize,
2022-06-10 14:36:52 +08:00
useInternalUrl: useInternalUrl,
2022-06-12 17:03:05 +08:00
maxDownloadRate: maxDownloadRate,
maxUploadRate: maxUploadRate,
2022-05-24 19:56:06 +08:00
}
}
func (m *SyncTaskManager) parseConfigFile() error {
/** 样例
{
"configVer": "1.0",
"syncTaskList": [
{
"name": "NS游戏备份",
"id": "5b2d7c10-e927-4e72-8f9d-5abb3bb04814",
"driveId": "19519111",
"localFolderPath": "D:\\smb\\datadisk\\game",
"panFolderPath": "/sync_drive/game",
"mode": "sync",
"lastSyncTime": ""
}
]
}
*/
2022-06-10 22:48:29 +08:00
configFilePath := m.ConfigFilePath()
2022-05-24 19:56:06 +08:00
r := &SyncDriveConfig{
ConfigVer: "1.0",
SyncTaskList: []*SyncTask{},
}
m.syncDriveConfig = r
if b, _ := utils.PathExists(configFilePath); b != true {
2022-06-09 19:58:16 +08:00
//text := utils.ObjectToJsonStr(r, true)
2022-06-16 22:30:12 +08:00
//ioutil.WriteFile(ConfigFilePath, []byte(text), 0755)
2022-06-10 22:48:29 +08:00
return fmt.Errorf("备份配置文件不存在:" + m.ConfigFilePath())
2022-05-24 19:56:06 +08:00
}
data, e := ioutil.ReadFile(configFilePath)
if e != nil {
return e
}
if len(data) > 0 {
if err2 := json.Unmarshal(data, m.syncDriveConfig); err2 != nil {
logger.Verboseln("parse sync drive config json error ", err2)
return err2
}
}
return nil
}
2022-06-10 22:48:29 +08:00
func (m *SyncTaskManager) ConfigFilePath() string {
2022-05-24 19:56:06 +08:00
return path.Join(m.SyncConfigFolderPath, "sync_drive_config.json")
}
// Start 启动同步进程
2022-06-17 10:27:53 +08:00
func (m *SyncTaskManager) Start(tasks []*SyncTask) (bool, error) {
if tasks != nil && len(tasks) > 0 {
m.syncDriveConfig = &SyncDriveConfig{
ConfigVer: "1.0",
SyncTaskList: []*SyncTask{},
}
m.syncDriveConfig.SyncTaskList = tasks
m.useConfigFile = false
} else {
if er := m.parseConfigFile(); er != nil {
return false, er
}
m.useConfigFile = true
2022-05-24 19:56:06 +08:00
}
if m.syncDriveConfig.SyncTaskList == nil || len(m.syncDriveConfig.SyncTaskList) == 0 {
return false, ErrSyncTaskListEmpty
}
// start the sync task one by one
for _, task := range m.syncDriveConfig.SyncTaskList {
if len(task.Id) == 0 {
task.Id = utils.UuidStr()
}
2022-06-14 21:05:40 +08:00
task.panUser = m.PanUser
2022-05-24 19:56:06 +08:00
task.DriveId = m.DriveId
task.syncDbFolderPath = m.SyncConfigFolderPath
task.panClient = m.PanClient
2022-06-10 13:55:00 +08:00
task.fileUploadParallel = m.fileUploadParallel
task.fileDownloadParallel = m.fileDownloadParallel
task.fileUploadBlockSize = m.fileUploadBlockSize
task.fileDownloadBlockSize = m.fileDownloadBlockSize
2022-06-10 14:36:52 +08:00
task.useInternalUrl = m.useInternalUrl
2022-06-12 17:03:05 +08:00
task.maxDownloadRate = m.maxDownloadRate
task.maxUploadRate = m.maxUploadRate
2022-05-24 19:56:06 +08:00
if e := task.Start(); e != nil {
logger.Verboseln(e)
fmt.Println("start sync task error: {}", task.Id)
continue
}
fmt.Println("\n启动同步任务")
fmt.Println(task)
2022-06-09 19:58:16 +08:00
time.Sleep(200 * time.Millisecond)
2022-05-24 19:56:06 +08:00
}
// save config file
2022-06-17 10:27:53 +08:00
if m.useConfigFile {
ioutil.WriteFile(m.ConfigFilePath(), []byte(utils.ObjectToJsonStr(m.syncDriveConfig, true)), 0755)
}
2022-05-24 19:56:06 +08:00
return true, nil
}
// Stop 停止同步进程
func (m *SyncTaskManager) Stop() (bool, error) {
// stop task one by one
for _, task := range m.syncDriveConfig.SyncTaskList {
if e := task.Stop(); e != nil {
logger.Verboseln(e)
2022-05-24 23:01:08 +08:00
fmt.Println("stop sync task error: ", task.NameLabel())
2022-05-24 19:56:06 +08:00
continue
}
fmt.Println("停止同步任务: ", task.NameLabel())
}
// save config file
2022-06-17 10:27:53 +08:00
if m.useConfigFile {
ioutil.WriteFile(m.ConfigFilePath(), []byte(utils.ObjectToJsonStr(m.syncDriveConfig, true)), 0755)
}
2022-05-24 19:56:06 +08:00
return true, nil
}