<commit_before>package server

import (
	"context"
	"time"

	"k8s.io/kubernetes/pkg/api"

	ppsclient "github.com/pachyderm/pachyderm/src/client/pps"
	"github.com/pachyderm/pachyderm/src/server/pps/persist"

	"go.pedge.io/lion/proto"
	"go.pedge.io/pb/go/google/protobuf"
	"go.pedge.io/proto/time"
)

var (
	// DefaultGCPolicy is the default GC policy used by a pipeline if one is not
	// specified.
	DefaultGCPolicy = &ppsclient.GCPolicy{
		// a day
		Success: &google_protobuf.Duration{
			Seconds: 24 * 60 * 60,
		},
		// a week
		Failure: &google_protobuf.Duration{
			Seconds: 7 * 24 * 60 * 60,
		},
	}
)

func (a *apiServer) runGC(ctx context.Context, pipelineInfo *ppsclient.PipelineInfo) {
	successTick := time.Tick(prototime.DurationFromProto(pipelineInfo.GcPolicy.Success))
	failureTick := time.Tick(prototime.DurationFromProto(pipelineInfo.GcPolicy.Failure))
	// wait blocks until it's time to run GC again
	wait := func() {
		select {
		case <-successTick:
		case <-failureTick:
		}
	}
	for {
		client, err := a.getPersistClient()
		if err != nil {
			protolion.Errorf("error getting persist client: %s", err)
			wait()
			continue
		}
		jobIDs, err := client.ListGCJobs(ctx, &persist.ListGCJobsRequest{
			PipelineName: pipelineInfo.Pipeline.Name,
			GcPolicy:     pipelineInfo.GcPolicy,
		})
		if err != nil {
			protolion.Errorf("error listing jobs to GC: %s", err)
			wait()
			continue
		}
		for _, jobID := range jobIDs.Jobs {
			zero := int64(0)
			falseVal := false
			go func(jobID string) {
				if err := a.kubeClient.Extensions().Jobs(a.namespace).Delete(jobID, &api.DeleteOptions{
					GracePeriodSeconds: &zero,
					OrphanDependents:   &falseVal,
				}); err != nil {
					// TODO: if the error indicates that the job has already been
					// deleted, just proceed to the next step
					protolion.Errorf("error deleting kubernetes job %s: %s", jobID, err)
					return
				}
				if _, err := client.GCJob(ctx, &ppsclient.Job{jobID}); err != nil {
					protolion.Errorf("error marking job %s as GC-ed: %s", jobID, err)
				}
				return
			}(jobID)
		}
		wait()
	}
	panic("unreachable")
}<commit_msg>Fix job gc for a second time.<commit_after>package server

import (
	"context"
	"time"

	ppsclient "github.com/pachyderm/pachyderm/src/client/pps"
	"github.com/pachyderm/pachyderm/src/server/pps/persist"

	"go.pedge.io/lion/proto"
	"go.pedge.io/pb/go/google/protobuf"
	"go.pedge.io/proto/time"
)

var (
	// DefaultGCPolicy is the default GC policy used by a pipeline if one is not
	// specified.
	DefaultGCPolicy = &ppsclient.GCPolicy{
		// a day
		Success: &google_protobuf.Duration{
			Seconds: 24 * 60 * 60,
		},
		// a week
		Failure: &google_protobuf.Duration{
			Seconds: 7 * 24 * 60 * 60,
		},
	}
)

func (a *apiServer) runGC(ctx context.Context, pipelineInfo *ppsclient.PipelineInfo) {
	successTick := time.Tick(prototime.DurationFromProto(pipelineInfo.GcPolicy.Success))
	failureTick := time.Tick(prototime.DurationFromProto(pipelineInfo.GcPolicy.Failure))
	// wait blocks until it's time to run GC again
	wait := func() {
		select {
		case <-successTick:
		case <-failureTick:
		}
	}
	for {
		client, err := a.getPersistClient()
		if err != nil {
			protolion.Errorf("error getting persist client: %s", err)
			wait()
			continue
		}
		jobIDs, err := client.ListGCJobs(ctx, &persist.ListGCJobsRequest{
			PipelineName: pipelineInfo.Pipeline.Name,
			GcPolicy:     pipelineInfo.GcPolicy,
		})
		if err != nil {
			protolion.Errorf("error listing jobs to GC: %s", err)
			wait()
			continue
		}
		for _, jobID := range jobIDs.Jobs {
			jobID := jobID
			go func() {
				jobInfo, err := client.InspectJob(ctx, &ppsclient.InspectJobRequest{
					Job: &ppsclient.Job{ID: jobID},
				})
				if err != nil {
					protolion.Errorf("error deleting job: %s", err)
					return
				}
				if err := a.deleteJob(ctx, jobInfo); err != nil {
					protolion.Errorf("error deleting job: %s", err)
					return
				}
				if _, err := client.GCJob(ctx, &ppsclient.Job{jobID}); err != nil {
					protolion.Errorf("error marking job %s as GC-ed: %s", jobID, err)
				}
				return
			}()
		}
		wait()
	}
	panic("unreachable")
}
{'fraction_non_alphanumeric': 0.13763909046927914, 'fraction_numerical': 0.00725689404934688, 'mean_word_length': 3.3117831074035453, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5257991', 'n_tokens_mistral': 1748, 'n_tokens_neox': 1491, 'n_words': 344}