func()

in astro/sessions.go [109:166]


func (s *Session) apply(boundExecutions []*boundExecution) (<-chan string, <-chan *Result, error) {
	logger.Trace.Println("astro session: running apply without graph")

	numberOfExecutions := len(boundExecutions)
	// Needs to be big enough to buffer log lines from below for tests that
	// don't consume from the channel.
	status := make(chan string, numberOfExecutions*10)
	results := make(chan *Result, numberOfExecutions)

	logger.Trace.Printf("astro: %d executions to apply\n", numberOfExecutions)

	fns := []func(){}
	for _, e := range boundExecutions {
		b := e // save for use inside the loop
		fns = append(fns, func() {
			terraform, err := s.newTerraformSession(b)
			if err != nil {
				results <- &Result{
					id:  b.ID(),
					err: err,
				}
				return
			}

			status <- fmt.Sprintf("[%s] Initializing...", b.ID())
			if result, err := terraform.Init(); err != nil {
				results <- &Result{
					id:              b.ID(),
					terraformResult: result,
					err:             err,
				}
				return
			}

			status <- fmt.Sprintf("[%s] Applying...", b.ID())
			result, err := terraform.Apply()
			results <- &Result{
				id:              b.ID(),
				terraformResult: result,
				err:             err,
			}
		})
	}

	ctx, cancel := context.WithCancel(context.Background())
	go func() {
		sig := <-s.signalChan
		fmt.Printf("\nReceived signal: %s, cancelling all operations...\n", sig)
		cancel()
	}()

	go func() {
		defer close(results) // signals the end of all executions
		utils.Parallel(ctx, 10, fns...)
	}()

	return status, results, nil
}