-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (85 loc) · 2.86 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
package main
import (
"bufio"
"context"
"os"
"strings"
"sync"
"task-scheduler/constants"
"task-scheduler/internal/datacenter"
appConfig "task-scheduler/utils/context"
"task-scheduler/utils/logger"
"github.com/google/uuid"
"github.com/rs/zerolog/log"
)
func main() {
// intialize logger
logger.InitLogger()
// prepare application config
ctx := context.Background()
ctx = appConfig.IntializeContext(ctx)
// bulding the data center
// TODO:
// to build the data center we require the data center which will house the resources for the process executions
// and the tasks that are to be executed on the data center
// building the data center
dataCenter := datacenter.DataCenter{
DataCenterId: uuid.NewString(),
Location: "ap-south-1",
Resources: []*datacenter.Resource{},
Tasks: []*datacenter.Task{},
}
// starting the data center
// this will make the data center start listening for the commands and processing the tasks
// we will listening for the commands on the main thread and process the tasks on a different thread
var wg sync.WaitGroup
// starting the task processing of the data center
wg.Add(1)
go dataCenter.Start(ctx, &wg)
log.Info().Ctx(ctx).Msg("task manager started")
for {
// if all tasks are executed then we can exit the command listening
if dataCenter.AreAllTasksExecuted(ctx) {
log.Info().Ctx(ctx).Msg("all tasks executed successfully")
break
}
// command for the data center to process
reader := bufio.NewReader(os.Stdin)
command, err := reader.ReadString('\n')
if err != nil {
log.Fatal().Ctx(ctx).Msgf("error scanning for the command %s", err.Error())
}
parts := strings.SplitN(command, " ", 2)
commandType := parts[0]
commandArgs := parts[1]
switch commandType {
case constants.AddTask:
// get task from the command args
task := datacenter.Task{}
task.FromCommandArgString(ctx, commandArgs)
// add task to the dc
dataCenter.AddTask(&task)
log.Info().Ctx(ctx).Msg("task added to the dc")
case constants.AddResource:
resource := datacenter.Resource{}
resource.FromCommandArgString(ctx, commandArgs)
// add resource to the dc
resourceId := dataCenter.AddResource(ctx, &resource)
log.Info().Ctx(ctx).Msgf("resource added to the dc: %s", resourceId)
case constants.DeleteResource:
// delete resource from id
resourceId := commandArgs
if ok := dataCenter.DeleteResource(ctx, resourceId); !ok {
log.Debug().Ctx(ctx).Msgf("error deleting the resource with id %s", resourceId)
}
log.Info().Ctx(ctx).Msg("resource deleted successfully.")
case constants.ListResources:
tasks := datacenter.Task{}
log.Info().Ctx(ctx).Interface("tasks", tasks).Msg("tasks available")
default:
log.Fatal().Ctx(ctx).Msg("error invalid task")
}
}
wg.Wait()
log.Info().Ctx(ctx).Interface("logs", dataCenter.ExecutionSummary).Msg("execution summary")
}