<commit_before>package automator

import (
	"strings"
	"time"

	"github.com/go-kit/kit/log"
	"github.com/pkg/errors"

	"github.com/weaveworks/flux"
	"github.com/weaveworks/flux/jobs"
)

const (
	automationCycle = 60 * time.Second
)

// Automator orchestrates continuous deployment for specific services.
type Automator struct {
	cfg Config
}

// New creates a new automator.
func New(cfg Config) (*Automator, error) {
	if err := cfg.Validate(); err != nil {
		return nil, err
	}
	return &Automator{
		cfg: cfg,
	}, nil
}

func (a *Automator) Start(errorLogger log.Logger) {
	tick := time.Tick(automationCycle)
	for range tick {
		insts, err := a.cfg.InstanceDB.All()
		if err != nil {
			errorLogger.Log("err", err)
			continue
		}
		for _, inst := range insts {
			for service, conf := range inst.Config.Services {
				if conf.Policy() == flux.PolicyAutomated {
					a.cfg.Jobs.PutJob(inst.ID, jobs.Job{
						// Key stops us getting two jobs for the same service
						Key: strings.Join([]string{
							jobs.AutomatedServiceJob,
							string(inst.ID),
							string(service),
						}, "|"),
						Method:   jobs.AutomatedServiceJob,
						Priority: jobs.PriorityBackground,
						Params: jobs.AutomatedServiceJobParams{
							ServiceSpec: flux.ServiceSpec(service),
						},
					})
				}
			}
		}
	}
}

func (a *Automator) Handle(j *jobs.Job, _ jobs.JobUpdater) error {
	params := j.Params.(jobs.AutomatedServiceJobParams)

	serviceID, err := flux.ParseServiceID(string(params.ServiceSpec))
	if err != nil {
		// I don't see how we could ever expect this to work, so let's not<commit_msg>Add error logging when failing to queue automated service jobs and do an initial automation cycle check on boot<commit_after>package automator

import (
	"strings"
	"time"

	"github.com/go-kit/kit/log"
	"github.com/pkg/errors"

	"github.com/weaveworks/flux"
	"github.com/weaveworks/flux/jobs"
)

const (
	automationCycle = 60 * time.Second
)

// Automator orchestrates continuous deployment for specific services.
type Automator struct {
	cfg Config
}

// New creates a new automator.
func New(cfg Config) (*Automator, error) {
	if err := cfg.Validate(); err != nil {
		return nil, err
	}
	return &Automator{
		cfg: cfg,
	}, nil
}

func (a *Automator) Start(errorLogger log.Logger) {
	a.checkAll(errorLogger)
	tick := time.Tick(automationCycle)
	for range tick {
		a.checkAll(errorLogger)
	}
}

func (a *Automator) checkAll(errorLogger log.Logger) {
	insts, err := a.cfg.InstanceDB.All()
	if err != nil {
		errorLogger.Log("err", err)
		return
	}
	for _, inst := range insts {
		for service, conf := range inst.Config.Services {
			if conf.Policy() != flux.PolicyAutomated {
				continue
			}
			_, err := a.cfg.Jobs.PutJob(inst.ID, jobs.Job{
				// Key stops us getting two jobs for the same service
				Key: strings.Join([]string{
					jobs.AutomatedServiceJob,
					string(inst.ID),
					string(service),
				}, "|"),
				Method:   jobs.AutomatedServiceJob,
				Priority: jobs.PriorityBackground,
				Params: jobs.AutomatedServiceJobParams{
					ServiceSpec: flux.ServiceSpec(service),
				},
			})
			if err != nil && err != jobs.ErrJobAlreadyQueued {
				errorLogger.Log("err", errors.Wrapf(err, "queueing automated service job"))
			}
		}
	}
}

func (a *Automator) Handle(j *jobs.Job, _ jobs.JobUpdater) error {
	params := j.Params.(jobs.AutomatedServiceJobParams)

	serviceID, err := flux.ParseServiceID(string(params.ServiceSpec))
	if err != nil {
		// I don't see how we could ever expect this to work, so let's not
{'fraction_non_alphanumeric': 0.12848209209778283, 'fraction_numerical': 0.0011370096645821489, 'mean_word_length': 3.415307402760351, 'pattern_counts': {'":': 0, '<': 3, '<?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': '622489', 'n_tokens_mistral': 1351, 'n_tokens_neox': 1172, 'n_words': 303}