-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
117 lines (92 loc) · 2.51 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"time"
"video_updater/internal/config"
"video_updater/internal/process"
)
var AppConfig = &config.ConfigStruct{}
func main() {
AppConfig = config.NewConfig()
defer AppConfig.DB.Close()
directories, err := postDirectories()
if err != nil {
log.Fatal(err)
}
if len(directories) == 0 {
log.Println("No directories to process")
return
}
// split directories into groups
var directory_groups [][]string
for i := 0; i < len(directories); i += AppConfig.GroupForProcessing {
end := i + AppConfig.GroupForProcessing
if end > len(directories) {
end = len(directories)
}
directory_groups = append(directory_groups, directories[i:end])
}
directory_pool := make(chan string, AppConfig.WorkerCount)
results := make(chan string, AppConfig.WorkerCount)
for worker := 1; worker <= AppConfig.WorkerCount; worker++ {
go processingWorker(worker, directory_pool, results)
}
defer close(directory_pool)
defer close(results)
for idx := range directory_groups {
work_directories := directory_groups[idx]
for _, directory := range work_directories {
directory_pool <- directory
}
for i := 0; i < len(work_directories); i++ {
result := <-results
log.Println(result)
}
time.Sleep(5 * time.Second)
}
}
// ********* private functions *********
func processingWorker(id int, directory_pool chan string, results chan string) error {
for directory := range directory_pool {
pr := process.NewProcess(directory, AppConfig)
pr.WorkerInvoke(id, results)
}
return nil
}
func postDirectories() ([]string, error) {
// get all files in the directory
files, err := getPostDirectories()
if err != nil {
log.Fatal(err)
}
processed_paths, err := AppConfig.DB.GetProcessedPaths()
if err != nil {
log.Fatal(err)
}
// exclude files that have already been processed
not_processed_paths := files
for _, db_path := range processed_paths {
for idx, path := range not_processed_paths {
if path == db_path {
not_processed_paths = append(not_processed_paths[:idx], not_processed_paths[idx+1:]...)
}
}
}
return not_processed_paths, nil
}
func getPostDirectories() ([]string, error) {
files, err := os.ReadDir(AppConfig.PostsDir)
if err != nil {
return nil, fmt.Errorf("failed to read posts directory: %v", err)
}
var postDirs []string
for _, file := range files {
if file.IsDir() && file.Name() != "." && file.Name() != ".." {
postDirs = append(postDirs, filepath.Join(AppConfig.PostsDir, file.Name()))
}
}
return postDirs, nil
}