-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (117 loc) Β· 3.45 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Package main
package main
import (
//"fmt"
"log"
"os"
"os/exec"
"syscall"
"time"
"github.com/gin-gonic/gin"
"loco-tasks/data"
"loco-tasks/handlers"
"loco-tasks/helpers"
//"loco-tasks/models"
"loco-tasks/status"
)
func main() {
data.LoadTasks()
// removinf finished tasks
for keyTask, taskRecord := range data.GetTasks() {
if taskRecord.CurrentStatus == status.Finished {
data.RemoveTask(keyTask)
}
}
/*
go func() {
for {
data.SaveTasks()
time.Sleep(5 * time.Second)
}
}()
*/
// Starting tasks
// https://www.sohamkamani.com/golang/exec-shell-command/#killing-a-child-process
// https://blog.kowalczyk.info/article/wOYk/advanced-command-execution-in-go-with-osexec.html
go func() {
for {
for keyTask, taskRecord := range data.GetTasks() {
// start the waiting task
if taskRecord.CurrentStatus == status.Waiting {
// run it / do something
cmd := &exec.Cmd{
Path: taskRecord.Path,
Args: taskRecord.Args,
Stdout: os.Stdout,
Stderr: os.Stdout,
}
newEnv := append(os.Environ(), taskRecord.Env...)
cmd.Env = newEnv
log.Println("π", cmd.Args)
err := cmd.Start()
if err != nil {
// update the status
taskRecord.CurrentStatus = status.Failed
taskRecord.StatusDescription = err.Error()
taskRecord.FailedAt = time.Now()
log.Println("π", err.Error())
} else {
// update the status
taskRecord.CurrentStatus = status.Started
taskRecord.StatusDescription = ""
taskRecord.StartedAt = time.Now()
taskRecord.Pid = cmd.Process.Pid
taskRecord.Cmd = cmd
}
// update the task
data.SetTask(keyTask, taskRecord)
}
}
time.Sleep(5 * time.Second)
}
}()
// Monitoring running tasks
go func() {
for {
for keyTask, taskRecord := range data.GetTasks() {
// start the waiting task
if taskRecord.CurrentStatus == status.Started {
//log.Println("π", keyTask)
process, _ := os.FindProcess(taskRecord.Pid)
err := process.Signal(syscall.Signal(0))
if err != nil {
log.Println("π[unknown]", process.Pid, err)
// remove the task, the process does not exist => do it at start
// data.RemoveTask(keyTask)
taskRecord.CurrentStatus = status.Finished
taskRecord.StatusDescription = "π[unknown]"
} else {
log.Println("π[running]", process.Pid, taskRecord.Name, "("+taskRecord.Description+")")
taskRecord.StatusDescription = "π[running]"
}
taskRecord.CheckedAt = time.Now()
// update the task
data.SetTask(keyTask, taskRecord)
}
}
data.SaveTasks()
time.Sleep(5 * time.Second)
}
}()
httpServer := gin.Default()
locoTaskEndPoint := helpers.GetEnv("LOCO_TASKS_ENDPOINT", "tasks")
// π§ work in progress
httpServer.POST("/admin/"+locoTaskEndPoint+"/start", handlers.CreateTask)
httpServer.GET("/admin/"+locoTaskEndPoint+"/list", handlers.GetTasks)
httpServer.GET("/admin/"+locoTaskEndPoint+"/list/:group", handlers.GetTasksOfGroup)
httpServer.DELETE("/admin/"+locoTaskEndPoint+"/processes/:group", handlers.StopProcessesOfGroup)
if helpers.GetEnv("LOCO_TASKS_CRT", "") != "" {
httpServer.RunTLS(
":"+helpers.GetEnv("LOCO_TASKS_HTTPS_PORT", "4443"),
helpers.GetEnv("LOCO_TASKS_CRT", "certs/loco-tasks.local.crt"),
helpers.GetEnv("LOCO_TASKS_KEY", "certs/loco-tasks.local.key"),
)
} else {
httpServer.Run(":" + helpers.GetEnv("LOCO_TASKS_HTTP_PORT", "9090"))
}
}