mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-05 04:56:37 +08:00
fix organize path mapping and task flow
This commit is contained in:
@@ -46,6 +46,7 @@ type SchedulerService struct {
|
||||
organizer *OrganizerService
|
||||
storageCfg *StorageConfigService
|
||||
hub *Hub
|
||||
tasks *TaskTrackerService
|
||||
cacheDir string
|
||||
now func() time.Time
|
||||
|
||||
@@ -54,6 +55,10 @@ type SchedulerService struct {
|
||||
jobs []*scheduledJob
|
||||
}
|
||||
|
||||
func (s *SchedulerService) SetTaskTracker(tasks *TaskTrackerService) {
|
||||
s.tasks = tasks
|
||||
}
|
||||
|
||||
// scheduledJob is one recurring task.
|
||||
type scheduledJob struct {
|
||||
name string
|
||||
@@ -479,11 +484,34 @@ func (s *SchedulerService) jobOrganizeSource(ctx context.Context) error {
|
||||
if s.organizer == nil || (!manual && !s.autoOrganizeSourceEnabled(ctx)) {
|
||||
return nil
|
||||
}
|
||||
task := s.startScheduledOrganizeTask(ctx, manual)
|
||||
res, err := s.organizer.OrganizeDirectory(ctx, OrganizeOptions{})
|
||||
if err != nil {
|
||||
if task != nil {
|
||||
task.Finish(err, TaskUpdate{
|
||||
Stage: "organize",
|
||||
Message: "自动整理入库失败",
|
||||
})
|
||||
}
|
||||
return err
|
||||
}
|
||||
if task != nil && res != nil {
|
||||
task.Update(TaskUpdate{
|
||||
Stage: "organize",
|
||||
SourcePath: res.SourcePath,
|
||||
DestPath: res.DestPath,
|
||||
Message: "自动整理完成,准备扫描入库",
|
||||
Metrics: OrganizeTaskMetrics(res),
|
||||
})
|
||||
}
|
||||
if s.scanner != nil && res != nil && strings.TrimSpace(res.DestPath) != "" && OrganizeResultHasChanges(res) {
|
||||
if task != nil {
|
||||
task.Update(TaskUpdate{
|
||||
Stage: "scan_scrape",
|
||||
Message: "正在扫描入库并按设置刮削",
|
||||
Metrics: OrganizeTaskMetrics(res),
|
||||
})
|
||||
}
|
||||
res.Scans, res.Scrapes = s.scanner.ScanAndScrapeLibrariesForPath(ctx, res.DestPath, "", OrganizeScrapeAfterEnabled(ctx, s.repo))
|
||||
} else if s.log != nil && res != nil && !OrganizeResultHasChanges(res) {
|
||||
s.log.Info("scheduled source organize skipped scan; no destination changes",
|
||||
@@ -494,6 +522,13 @@ func (s *SchedulerService) jobOrganizeSource(ctx context.Context) error {
|
||||
zap.Int("skipped", res.Skipped),
|
||||
)
|
||||
}
|
||||
if task != nil {
|
||||
task.Finish(nil, TaskUpdate{
|
||||
Stage: "completed",
|
||||
Message: "自动整理入库结束",
|
||||
Metrics: OrganizeTaskMetrics(res),
|
||||
})
|
||||
}
|
||||
if s.log != nil && res != nil {
|
||||
s.log.Info("scheduled source organize finished",
|
||||
zap.String("source", res.SourcePath),
|
||||
@@ -508,6 +543,24 @@ func (s *SchedulerService) jobOrganizeSource(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SchedulerService) startScheduledOrganizeTask(ctx context.Context, manual bool) *TaskHandle {
|
||||
if s == nil || s.tasks == nil {
|
||||
return nil
|
||||
}
|
||||
name := "自动整理重命名入库"
|
||||
message := "正在执行计划自动整理/重命名/入库"
|
||||
if manual {
|
||||
name = "手动触发自动整理重命名入库"
|
||||
message = "正在执行手动触发的自动整理/重命名/入库"
|
||||
}
|
||||
return s.tasks.Start(TaskKindOrganize, name, TaskUpdate{
|
||||
Stage: "organize",
|
||||
SourcePath: s.organizer.defaultSourceRoot(ctx, ""),
|
||||
DestPath: s.organizer.defaultDestRoot(ctx, ""),
|
||||
Message: message,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *SchedulerService) autoOrganizeSourceEnabled(ctx context.Context) bool {
|
||||
if s.repo == nil || s.repo.Setting == nil {
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user