<commit_before>package main

import (
	"bufio"
	"bytes"
	"encoding/json"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"os/exec"
	"text/template"
	"time"
)

type JobStatus int

const (
	NEVER_RUN JobStatus = iota
	POLLING
	RUNNING
	FAILED
	RECOVERED
	SUCCESSFUL
)

type Job struct {
	Name       string
	Enabled    bool
	running    bool
	Git_url    string
	LastRun    time.Time
	LastStatus JobStatus
	CurStatus  JobStatus
}

func (j *Job) needsRunning() bool {
	return j.CurStatus == NEVER_RUN || j.needsUpdate()
}

func (j *Job) run() {
	log.Println("Running job:", j.Name)
	// todo: akelmore - make status a stack, not just two
	j.LastStatus = j.CurStatus
	j.CurStatus = RUNNING
	j.LastRun = time.Now()

	if j.LastStatus == NEVER_RUN {
		j.firstTimeSetup()
	} else {
		j.update()
	}

	if j.CurStatus != FAILED {
		switch j.LastStatus {
		case FAILED:
			j.CurStatus = RECOVERED
		default:
			j.CurStatus = SUCCESSFUL
		}
	}

	saveConfig()
}

func (j *Job) firstTimeSetup() {
	log.Println("Running first time setup for:", j.Name)

	var b bytes.Buffer
	writer := bufio.NewWriter(&b)
	multi := io.MultiWriter(writer, os.Stdout)

	cmd := exec.Command("git", "-c", "jobs/"+j.Name, "clone", "--depth", "1", j.Git_url)
	cmd.Stdout = multi
	cmd.Stderr = multi
	if err := cmd.Run(); err != nil {
		log.Println("Error doing first time setup for:", j.Name)
		j.CurStatus = FAILED
	}
}

func (j *Job) needsUpdate() bool {
	if time.Since(j.LastRun) < 30*time.Second {
		return false
	}
	log.Println("Running needsUpdate for:", j.Name)

	// todo: akelmore - pull this multiwriter into Job so it can be output on the web
	var b bytes.Buffer
	writer := bufio.NewWriter(&b)
	multi := io.MultiWriter(writer, os.Stdout)

	cmd := exec.Command("git", "-c", "jobs/"+j.Name, "remote", "update")
	cmd.Stdout = multi
	cmd.Stderr = multi
	if err := cmd.Run(); err != nil {
		return false
	}

	cmd = exec.Command("git", "-c", "jobs/"+j.Name, "status", "-uno")
	cmd.Stdout = multi
	cmd.Stderr = multi
	if err := cmd.Run(); err != nil {
		return false
	}

	return !bytes.Contains(b.Bytes(), []byte("Your branch is up-to-date"))
}

func (j *Job) update() {
	log.Println("Running update for:", j.Name)

	var b bytes.Buffer
	writer := bufio.NewWriter(&b)
	multi := io.MultiWriter(writer, os.Stdout)

	cmd := exec.Command("git", "-c", "jobs/"+j.Name, "pull")
	cmd.Stdout = multi
	cmd.Stderr = multi
	if err := cmd.Run(); err != nil {
		log.Println("Error pulling git for:", j.Name)<commit_msg>Making it so git calls ls-remote to see if it needs to sync against HEAD. Doesn't actually fetch first.<commit_after>package main

import (
	"bytes"
	"encoding/json"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"os/exec"
	"text/template"
	"time"
)

type JobStatus int

const (
	NEVER_RUN JobStatus = iota
	POLLING
	RUNNING
	FAILED
	RECOVERED
	SUCCESSFUL
)

type Job struct {
	Name       string
	Enabled    bool
	running    bool
	Git_url    string
	LastRun    time.Time
	LastStatus JobStatus
	CurStatus  JobStatus
}

func (j *Job) needsRunning() bool {
	return j.CurStatus == NEVER_RUN || j.needsUpdate()
}

func (j *Job) run() {
	log.Println("Running job:", j.Name)
	// todo: akelmore - make status a stack, not just two
	j.LastStatus = j.CurStatus
	j.CurStatus = RUNNING
	j.LastRun = time.Now()

	if j.LastStatus == NEVER_RUN {
		j.firstTimeSetup()
	} else {
		j.update()
	}

	if j.CurStatus != FAILED {
		switch j.LastStatus {
		case FAILED:
			j.CurStatus = RECOVERED
		default:
			j.CurStatus = SUCCESSFUL
		}
	}

	saveConfig()
}

func (j *Job) firstTimeSetup() {
	log.Println("Running first time setup for:", j.Name)

	var b bytes.Buffer
	multi := io.MultiWriter(&b, os.Stdout)

	cmd := exec.Command("git", "-c", "jobs/"+j.Name, "clone", "--depth", "1", j.Git_url)
	cmd.Stdout = multi
	cmd.Stderr = multi
	if err := cmd.Run(); err != nil {
		log.Println("Error doing first time setup for:", j.Name)
		j.CurStatus = FAILED
	}
}

func (j *Job) needsUpdate() bool {
	if time.Since(j.LastRun) < 30*time.Second {
		return false
	}
	log.Println("Running needsUpdate for:", j.Name)

	// todo: akelmore - pull this multiwriter into Job so it can be output on the web
	var b bytes.Buffer
	multi := io.MultiWriter(&b, os.Stdout)

	cmd := exec.Command("git", "-c", "jobs/"+j.Name, "ls-remote", "origin", "-h", "HEAD")
	cmd.Stdout = multi
	cmd.Stderr = multi
	if err := cmd.Run(); err != nil {
		return false
	}

	remoteHead := string(bytes.Fields(b.Bytes())[0])

	b.Reset()
	cmd = exec.Command("git", "-c", "jobs/"+j.Name, "rev-parse", "HEAD")
	cmd.Stdout = multi
	cmd.Stderr = multi
	if err := cmd.Run(); err != nil {
		return false
	}

	localHead := string(bytes.Fields(b.Bytes())[0])

	return remoteHead != localHead
}

func (j *Job) update() {
	log.Println("Running update for:", j.Name)

	var b bytes.Buffer
	multi := io.MultiWriter(&b, os.Stdout)

	cmd := exec.Command("git", "-c", "jobs/"+j.Name, "pull", "--depth", "1")
	cmd.Stdout = multi
	cmd.Stderr = multi
	if err := cmd.Run(); err != nil {
		log.Println("Error pulling git for:", j.Name)
{'fraction_non_alphanumeric': 0.16147082334132695, 'fraction_numerical': 0.0017985611510791368, 'mean_word_length': 3.4370567375886525, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2163720', 'n_tokens_mistral': 1990, 'n_tokens_neox': 1905, 'n_words': 514}