<commit_before>package runner

import (
	"database/sql"
	"fmt"
	"regexp"
	"time"

	"github.com/harrowio/harrow/cast"
	"github.com/harrowio/harrow/domain"
	"github.com/harrowio/harrow/logger"
	"github.com/harrowio/harrow/stores"
	"github.com/jmoiron/sqlx"
	"github.com/lib/pq"
	"github.com/pkg/errors"
)

type OperationFromDbOrBus struct {
	dbConnStr string
	db        *sqlx.DB
	log       logger.Logger
}

func (ofdob *OperationFromDbOrBus) WaitForNew() {
	ofdob.log.Info().Msg("waiting for sth to happen on db new-operation channel")
	l := pq.NewListener(ofdob.dbConnStr, 10*time.Second, time.Minute, nil)
	if err := l.Listen("new-operation"); err != nil {
		ofdob.log.Fatal().Msgf("error listening on pg channel %q: %s", "new-operation", err)
	}
	<-l.Notify
	ofdob.log.Info().Msg("something happened in db, returning")
	return
}

// AppendStatusLog appends a record to the stauts log for the event given
// a transaction to use. The transaction must be given as we have two
// different transactions in play here. If nil is given then a new one will
// be started on the OperationFromDbOrBus.db
func (ofdob *OperationFromDbOrBus) AppendStatusLog(tx *sqlx.Tx, uuid, entryType, subject string) error {

	if tx == nil {
		ofdob.log.Info().Msg("appending status logs in independent tx")
		var err error
		tx, err = ofdob.db.Beginx()<commit_msg>Close the listener when we've recieved a message<commit_after>package runner

import (
	"database/sql"
	"fmt"
	"regexp"
	"time"

	"github.com/harrowio/harrow/cast"
	"github.com/harrowio/harrow/domain"
	"github.com/harrowio/harrow/logger"
	"github.com/harrowio/harrow/stores"
	"github.com/jmoiron/sqlx"
	"github.com/lib/pq"
	"github.com/pkg/errors"
)

type OperationFromDbOrBus struct {
	dbConnStr string
	db        *sqlx.DB
	log       logger.Logger
}

func (ofdob *OperationFromDbOrBus) WaitForNew() {
	ofdob.log.Info().Msg("waiting for sth to happen on db new-operation channel")
	l := pq.NewListener(ofdob.dbConnStr, 10*time.Second, time.Minute, nil)
	if err := l.Listen("new-operation"); err != nil {
		ofdob.log.Fatal().Msgf("error listening on pg channel %q: %s", "new-operation", err)
	}
	defer l.Close()
	<-l.Notify
	ofdob.log.Info().Msg("something happened in db, returning")
	return
}

// AppendStatusLog appends a record to the stauts log for the event given
// a transaction to use. The transaction must be given as we have two
// different transactions in play here. If nil is given then a new one will
// be started on the OperationFromDbOrBus.db
func (ofdob *OperationFromDbOrBus) AppendStatusLog(tx *sqlx.Tx, uuid, entryType, subject string) error {

	if tx == nil {
		ofdob.log.Info().Msg("appending status logs in independent tx")
		var err error
		tx, err = ofdob.db.Beginx()
{'fraction_non_alphanumeric': 0.1258229700073153, 'fraction_numerical': 0.001463057790782736, 'mean_word_length': 4.102611940298507, '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': '2765525', 'n_tokens_mistral': 997, 'n_tokens_neox': 979, 'n_words': 270}