<commit_before>package workqueue

import (
	"fmt"
	"time"
)

type worker struct {
	Id       string
	WorkerFn func(Job)
	quit     chan struct{}
}

func newWorker(id string, workerFn func(Job)) *worker {
	return &worker{
		Id:       id,
		WorkerFn: workerFn,
	}
}

func (w *worker) loop(jq jobQueue) {
	for {
		select {
		case <-w.quit:
			return

		default:
			if job, ok := jq.WaitForJob(); ok {
				w.execute(job)
			} else {
				time.Sleep(5 * time.Millisecond) // XXX
			}
		}
	}
}

func (w worker) execute(j Job) {
	defer func() {
		if r := recover(); r != nil {
			fmt.Printf("worker-%s: worker fn paniced: %s\n", w.Id, r)
		}
	}()

	w.WorkerFn(j)
}

func (w *worker) Start(jq jobQueue) {
	w.quit = make(chan struct{})
	go w.loop(jq)
}

// The method blocks until the waiters of worker's job queue are aborted
func (w *worker) Stop() {
	w.quit <- struct{}{}
}<commit_msg>Worker prints the stack to stdout when workerFn panics<commit_after>package workqueue

import (
	"fmt"
	"runtime/debug"
	"time"
)

type worker struct {
	Id       string
	WorkerFn func(Job)
	quit     chan struct{}
}

func newWorker(id string, workerFn func(Job)) *worker {
	return &worker{
		Id:       id,
		WorkerFn: workerFn,
	}
}

func (w *worker) loop(jq jobQueue) {
	for {
		select {
		case <-w.quit:
			return

		default:
			if job, ok := jq.WaitForJob(); ok {
				w.execute(job)
			} else {
				time.Sleep(5 * time.Millisecond) // XXX
			}
		}
	}
}

func (w worker) execute(j Job) {
	defer func() {
		if r := recover(); r != nil {
			fmt.Printf("worker-%s: worker fn paniced: %s: %s\n", w.Id, r, debug.Stack())
		}
	}()

	w.WorkerFn(j)
}

func (w *worker) Start(jq jobQueue) {
	w.quit = make(chan struct{})
	go w.loop(jq)
}

// The method blocks until the waiters of worker's job queue are aborted
func (w *worker) Stop() {
	w.quit <- struct{}{}
}
{'fraction_non_alphanumeric': 0.1558228540185894, 'fraction_numerical': 0.0010934937124111536, 'mean_word_length': 2.8853503184713376, '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': '3125286', 'n_tokens_mistral': 786, 'n_tokens_neox': 710, 'n_words': 203}