func (s *StateStore) ReconcileJobSummaries(index uint64) error {
	txn := s.db.Txn(true)
	defer txn.Abort()

	// Get all the jobs
	iter, err := txn.Get("jobs", "id")
	if err != nil {
		return err
	}
	for {
		rawJob := iter.Next()
		if rawJob == nil {
			break
		}
		job := rawJob.(*structs.Job)

		// Create a job summary for the job
		summary := &structs.JobSummary{
			JobID:   job.ID,
			Summary: make(map[string]structs.TaskGroupSummary),
		}
		for _, tg := range job.TaskGroups {
			summary.Summary[tg.Name] = structs.TaskGroupSummary{}
		}

		// Find all the allocations for the jobs
		iterAllocs, err := txn.Get("allocs", "job", job.ID)
		if err != nil {
			return err
		}

		// Calculate the summary for the job
		for {
			rawAlloc := iterAllocs.Next()
			if rawAlloc == nil {
				break
			}
			alloc := rawAlloc.(*structs.Allocation)

			// Ignore the allocation if it doesn't belong to the currently
			// registered job. The allocation is checked because of issue #2304
			if alloc.Job == nil || alloc.Job.CreateIndex != job.CreateIndex {
				continue
			}

			tg := summary.Summary[alloc.TaskGroup]
			switch alloc.ClientStatus {
			case structs.AllocClientStatusFailed:
				tg.Failed += 1
			case structs.AllocClientStatusLost:
				tg.Lost += 1
			case structs.AllocClientStatusComplete:
				tg.Complete += 1
			case structs.AllocClientStatusRunning:
				tg.Running += 1
			case structs.AllocClientStatusPending:
				tg.Starting += 1
			default:
				s.logger.Printf("[ERR] state_store: invalid client status: %v in allocation %q", alloc.ClientStatus, alloc.ID)
			}
			summary.Summary[alloc.TaskGroup] = tg
		}

		// Set the create index of the summary same as the job's create index
		// and the modify index to the current index
		summary.CreateIndex = job.CreateIndex
		summary.ModifyIndex = index

		// Insert the job summary
		if err := txn.Insert("job_summary", summary); err != nil {
			return fmt.Errorf("error inserting job summary: %v", err)
		}
	}

	// Update the indexes table for job summary
	if err := txn.Insert("index", &IndexEntry{"job_summary", index}); err != nil {
		return fmt.Errorf("index update failed: %v", err)
	}
	txn.Commit()
	return nil
}
// ReconcileJobSummaries re-creates summaries for all jobs present in the state
// store
ReconcileJobSummaries re-creates summaries for all jobs present in the state
store
// ReconcileJobSummaries re - creates summaries for all jobs present in the state // store
func (s *StateStore) ReconcileJobSummaries(index uint64) error {
	txn := s.db.Txn(true)
	defer txn.Abort()
	iter, err := txn.Get("jobs", "id")
	if err != nil {
		return err
	}
	for {
		rawJob := iter.Next()
		if rawJob == nil {
			break
		}
		job := rawJob.(*structs.Job)
		summary := &structs.JobSummary{
			JobID:   job.ID,
			Summary: make(map[string]structs.TaskGroupSummary),
		}
		for _, tg := range job.TaskGroups {
			summary.Summary[tg.Name] = structs.TaskGroupSummary{}
		}
		iterAllocs, err := txn.Get("allocs", "job", job.ID)
		if err != nil {
			return err
		}
		for {
			rawAlloc := iterAllocs.Next()
			if rawAlloc == nil {
				break
			}
			alloc := rawAlloc.(*structs.Allocation)
			if alloc.Job == nil || alloc.Job.CreateIndex != job.CreateIndex {
				continue
			}
			tg := summary.Summary[alloc.TaskGroup]
			switch alloc.ClientStatus {
			case structs.AllocClientStatusFailed:
				tg.Failed += 1
			case structs.AllocClientStatusLost:
				tg.Lost += 1
			case structs.AllocClientStatusComplete:
				tg.Complete += 1
			case structs.AllocClientStatusRunning:
				tg.Running += 1
			case structs.AllocClientStatusPending:
				tg.Starting += 1
			default:
				s.logger.Printf("[ERR] state_store: invalid client status: %v in allocation %q", alloc.ClientStatus, alloc.ID)
			}
			summary.Summary[alloc.TaskGroup] = tg
		}
		summary.CreateIndex = job.CreateIndex
		summary.ModifyIndex = index
		if err := txn.Insert("job_summary", summary); err != nil {
			return fmt.Errorf("error inserting job summary: %v", err)
		}
	}
	if err := txn.Insert("index", &IndexEntry{"job_summary", index}); err != nil {
		return fmt.Errorf("index update failed: %v", err)
	}
	txn.Commit()
	return nil
}
func ( s * StateStore ) ReconcileJobSummaries ( index uint64 ) error { txn := s . db . Txn ( true ) defer txn . Abort ( ) iter , err := txn . Get ( " " , " " ) if err != nil { return err } for { rawJob := iter . Next ( ) if rawJob == nil { break } job := rawJob . ( * structs . Job ) summary := & structs . JobSummary { JobID : job . ID , Summary : make ( map [ string ] structs . TaskGroupSummary ) , } for _ , tg := range job . TaskGroups { summary . Summary [ tg . Name ] = structs . TaskGroupSummary { } } iterAllocs , err := txn . Get ( " " , " " , job . ID ) if err != nil { return err } for { rawAlloc := iterAllocs . Next ( ) if rawAlloc == nil { break } alloc := rawAlloc . ( * structs . Allocation ) if alloc . Job == nil || alloc . Job . CreateIndex != job . CreateIndex { continue } tg := summary . Summary [ alloc . TaskGroup ] switch alloc . ClientStatus { case structs . AllocClientStatusFailed : tg . Failed += 1 case structs . AllocClientStatusLost : tg . Lost += 1 case structs . AllocClientStatusComplete : tg . Complete += 1 case structs . AllocClientStatusRunning : tg . Running += 1 case structs . AllocClientStatusPending : tg . Starting += 1 default : s . logger . Printf ( " " , alloc . ClientStatus , alloc . ID ) } summary . Summary [ alloc . TaskGroup ] = tg } summary . CreateIndex = job . CreateIndex summary . ModifyIndex = index if err := txn . Insert ( " " , summary ) ; err != nil { return fmt . Errorf ( " " , err ) } } if err := txn . Insert ( " " , & IndexEntry { " " , index } ) ; err != nil { return fmt . Errorf ( " " , err ) } txn . Commit ( ) return nil }
ReconcileJobSummaries re-creates summaries for all jobs present in the state
store
ReconcileJobSummaries re - creates summaries for all jobs present in the state store
// Get all the jobs // Create a job summary for the job // Find all the allocations for the jobs // Calculate the summary for the job // Ignore the allocation if it doesn't belong to the currently // registered job. The allocation is checked because of issue #2304 // Set the create index of the summary same as the job's create index // and the modify index to the current index // Insert the job summary // Update the indexes table for job summary