// Init => helps initialize
Init => helps initialize
// Init = > helps initialize
func Init(cleanUpTimeInMinutes int) Master {
	jobs = make(map[string]JobRelatedData)
	cleanUpTime = cleanUpTimeInMinutes
	ticker := time.NewTicker(1 * time.Second) 
	done := make(chan bool)
	go func() {
		for {
			select {
			case <-done:
				return
			case <-ticker.C:
				if !jobInProgress { 
				outerLoop:
					for priority := 1; priority <= 3; priority++ { 
						for jobID, v := range jobs { 
							if v.Priority == Priority(priority) && !v.completed {
								go func() {
									v.status = Running
									v.statusChangeTime = time.Now()
									v.channel = make(chan bool, 1)
									jobsMutex.Lock()
									jobs[jobID] = v 
									jobsMutex.Unlock()
									compute(v.JobStatus, JobRelated{jobID: jobID}, v) 
									v.status = Completed
									v.statusChangeTime = time.Now()
									v.completed = true
									jobsMutex.Lock()
									jobs[jobID] = v 
									jobsMutex.Unlock()
									go func() { 
										time.Sleep(time.Duration(cleanUpTime) * time.Minute)
										delete(jobs, jobID) 
									}()
									jobInProgress = false 
								}()
								v.createdTime = time.Now()
								jobInProgress = true
								jobsMutex.Lock()
								jobs[jobID] = v
								jobsMutex.Unlock()
								break outerLoop 
							}
						}
					}
				}
			}
		}
	}()
	return Master{jobs: jobs, done: done}
}
func Init ( cleanUpTimeInMinutes int ) Master { jobs = make ( map [ string ] JobRelatedData ) cleanUpTime = cleanUpTimeInMinutes ticker := time . NewTicker ( 1 * time . Second ) done := make ( chan bool ) go func ( ) { for { select { case <- done : return case <- ticker . C : if ! jobInProgress { outerLoop : for priority := 1 ; priority <= 3 ; priority ++ { for jobID , v := range jobs { if v . Priority == Priority ( priority ) && ! v . completed { go func ( ) { v . status = Running v . statusChangeTime = time . Now ( ) v . channel = make ( chan bool , 1 ) jobsMutex . Lock ( ) jobs [ jobID ] = v jobsMutex . Unlock ( ) compute ( v . JobStatus , JobRelated { jobID : jobID } , v ) v . status = Completed v . statusChangeTime = time . Now ( ) v . completed = true jobsMutex . Lock ( ) jobs [ jobID ] = v jobsMutex . Unlock ( ) go func ( ) { time . Sleep ( time . Duration ( cleanUpTime ) * time . Minute ) delete ( jobs , jobID ) } ( ) jobInProgress = false } ( ) v . createdTime = time . Now ( ) jobInProgress = true jobsMutex . Lock ( ) jobs [ jobID ] = v jobsMutex . Unlock ( ) break outerLoop } } } } } } } ( ) return Master { jobs : jobs , done : done } }
Init => helps initialize
Init = > helps initialize
// ticker that runs every couple seconds triggering the loop in the goroutine // gate keeper that makes sure only one job runs at any time // helps prioritize between tasks in hand based on priority // loops through jobs until a fit is found as in the condition below // updating the jobs map with updated values // running the job in question as requested by the user // updating the jobs map with updated values // we will remove the job from the list only after cleanUpTime time in minutes // remove the job that just got completed executing // helps open gates for new jobs // breaks outerloop once it finds a job to assign to