// this function will make the agent a candidate to receive job offers. func (a *Agent) advertiseExecutorRoleTask() *task.Task { t := task.New("advertiseExecutorRoleTask", func() error { a.log("Renewing my executor role...") return a.clusterClient.RegisterAsExecutor(a.ID, a.IPv4) }) t.RunEvery(time.Second * cluster.HEARTBEAT) a.taskManager.RegisterTask(t) return t }
// This function register the leader (aka scheduler) IP periodically. // Keep in mind that the "title" of leader expires every `SCHEDULER_LEADER_TTL` // and it has to be renewed. Fail to do that, the rest of cluster will compete // again for a leaders position. func (a *Agent) advertiseSchedulerRoleTask() *task.Task { t := task.New("advertiseSchedulerRoleTask", func() error { a.log("Renewing my leader role...") _, err := a.clusterClient.RegisterAsScheduler(a.IPv4) return err }) t.RunEvery(time.Second * cluster.HEARTBEAT) a.taskManager.RegisterTask(t) return t }
func (a *Agent) watchForSchedulerFailureTask() *task.Task { notifyC := make(chan bool, 1) watchLeaderStopC := make(chan bool, 1) t := task.New("watchForSchedulerFailureTask", func() error { a.clusterClient.SchedulerFailureWatcher(notifyC, watchLeaderStopC) <-notifyC return nil }) t.OnStopFn(func() { watchLeaderStopC <- true }) t.RunOnce() a.taskManager.RegisterTask(t) return t }
// This function will be run by the leader only, and it will manage the job scheduler // and by extent, the publication of job offers func (a *Agent) publishJobOffersTask() *task.Task { dueJobC := make(chan *scheduler.Job, 10) stopDueJobC := make(chan bool) t := task.New("publishJobOffersTask", func() error { a.jobScheduler.Notify(dueJobC, stopDueJobC) for job := range dueJobC { a.logf("Publishing jobOffer: %s ...", job.ToString()) a.clusterClient.MakeJobOffer(job) } return nil }) t.OnStopFn(func() { stopDueJobC <- true }) t.RunOnce() a.taskManager.RegisterTask(t) return t }
func (a *Agent) watchForJobOffersTask() *task.Task { jobOfferC := make(chan *scheduler.Job, 1) jobOfferStopC := make(chan bool, 1) t := task.New("watchForJobOffersTask", func() error { go a.clusterClient.WatchJobOffers(jobOfferC, jobOfferStopC) for jobOffer := range jobOfferC { if jobOffer != nil && a.clusterClient.TakeJobOffer(jobOffer, a.IPv4) { a.logf("Executing jobOffer: [%s]", jobOffer.ToString()) go a.jobSupervisor.RunIt(jobOffer) } } return nil }) t.OnStopFn(func() { jobOfferStopC <- true }) t.RunOnce() a.taskManager.RegisterTask(t) return t }
func (c *Configurator) addWatchOnChangeProxy() *task.Task { proxyChangesC := make(chan proxy.ApiProxySpec, 100) stopC := make(chan bool, 1) proxyWatcherTask := task.New("addWatchOnChangeProxy", func() error { go c.backend.WatchProxyChanges(proxyChangesC, stopC) for newProxy := range proxyChangesC { if newProxy != nil { c.observer.OnChangeProxy(newProxy) } } return nil }) proxyWatcherTask.OnStopFn(func() { stopC <- true }) proxyWatcherTask.RunOnce() c.taskManager.RegisterTask(proxyWatcherTask) return proxyWatcherTask }
// This function listen for new jobs pending for scheduling func (a *Agent) watchForJobsToScheduleTask() *task.Task { unscheduledJobsC := make(chan *scheduler.Job, 100) stopDueJobC := make(chan bool, 1) t := task.New("watchForJobsToScheduleTask", func() error { go a.clusterClient.WatchJobsToSchedule(unscheduledJobsC, stopDueJobC) a.log("Job sentinel activated") for unscheduledJob := range unscheduledJobsC { if unscheduledJob != nil { a.logf("Scheduling job: %s (due in %f)...", unscheduledJob.ToString(), unscheduledJob.WaitSecs()) a.jobScheduler.Enqueue(unscheduledJob) } } return nil }) t.OnStopFn(func() { stopDueJobC <- true }) t.RunOnce() a.taskManager.RegisterTask(t) return t }