<commit_before>package payment

import (
	"fmt"
	"socialapi/config"
	"socialapi/workers/payment/paymentmodels"
	"socialapi/workers/payment/paypal"
	"socialapi/workers/payment/stripe"
	"time"

	kiteConfig "github.com/koding/kite/config"

	"github.com/koding/bongo"
	"github.com/koding/kite"
)

func Initialize(conf *config.Config) {
	stripe.InitializeClientKey(conf.Stripe.SecretToken)
	paypal.InitializeClientKey(conf.Paypal)

	KiteClient = initializeKiteClient(conf.Kloud.SecretKey, conf.Kloud.Address)

	go func() {
		err := stripe.CreateDefaultPlans()
		if err != nil {
			fmt.Println(err)
			panic(err)
		}
	}()

	go InitCheckers()
}

func InitCheckers() error {
	err := CheckForLeakedSubscriptions()
	if err != nil {
		Log.Error("Error checking for leaked subscriptions %v", err)
	}

	return err
}

func CheckForLeakedSubscriptions() error {
	thirtyDaysAgo := time.Now().Add(-30 * 24 * time.Hour)

	subscription := paymentmodels.NewSubscription()
	subscriptions, err := subscription.ByCanceledAtGte(thirtyDaysAgo)
	if err != nil {
		if err == bongo.RecordNotFound {
			return nil
		}

		return err
	}

	subscriptionIds := []int64{}

	for _, subscription := range subscriptions {
		subscriptionIds = append(subscriptionIds, subscription.Id)
	}

	if len(subscriptions) > 0 {
		Log.Error(
			"%v subscriptions have been expired for more than 30 days. %v",
			len(subscriptionIds), subscriptionIds,
		)
	}

	return nil
}

func initializeKiteClient(kloudSecretKey, kloudAddr string) *kite.Client {
	var err error

	// create new kite
	k := kite.New(WorkerName, WorkerVersion)
	config, err := kiteConfig.Get()
	if err != nil {
		Log.Fatal(err.Error())
	}

	// set skeleton config
	k.Config = config

	// create a new connection to the cloud
	kiteClient := k.NewClient(kloudAddr)
	kiteClient.Auth = &kite.Auth{
		Type: "kloudctl",
		Key:  kloudSecretKey,
	}

	// dial the kloud address
	if err := kiteClient.DialTimeout(time.Second * 10); err != nil {
		Log.Error("%s. Is kloud/kontrol running?", err.Error())
		return nil
	}

	Log.Info("Connected to klient: %s", kloudAddr)

	return kiteClient
}<commit_msg>payment: don't crash if kite fails to initialize<commit_after>package payment

import (
	"fmt"
	"socialapi/config"
	"socialapi/workers/payment/paymentmodels"
	"socialapi/workers/payment/paypal"
	"socialapi/workers/payment/stripe"
	"time"

	kiteConfig "github.com/koding/kite/config"

	"github.com/koding/bongo"
	"github.com/koding/kite"
)

func Initialize(conf *config.Config) {
	stripe.InitializeClientKey(conf.Stripe.SecretToken)
	paypal.InitializeClientKey(conf.Paypal)

	KiteClient = initializeKiteClient(conf.Kloud.SecretKey, conf.Kloud.Address)

	go func() {
		err := stripe.CreateDefaultPlans()
		if err != nil {
			fmt.Println(err)
			panic(err)
		}
	}()

	go InitCheckers()
}

func InitCheckers() error {
	err := CheckForLeakedSubscriptions()
	if err != nil {
		Log.Error("Error checking for leaked subscriptions %v", err)
	}

	return err
}

func CheckForLeakedSubscriptions() error {
	thirtyDaysAgo := time.Now().Add(-30 * 24 * time.Hour)

	subscription := paymentmodels.NewSubscription()
	subscriptions, err := subscription.ByCanceledAtGte(thirtyDaysAgo)
	if err != nil {
		if err == bongo.RecordNotFound {
			return nil
		}

		return err
	}

	subscriptionIds := []int64{}

	for _, subscription := range subscriptions {
		subscriptionIds = append(subscriptionIds, subscription.Id)
	}

	if len(subscriptions) > 0 {
		Log.Error(
			"%v subscriptions have been expired for more than 30 days. %v",
			len(subscriptionIds), subscriptionIds,
		)
	}

	return nil
}

func initializeKiteClient(kloudSecretKey, kloudAddr string) *kite.Client {
	var err error

	// create new kite
	k := kite.New(WorkerName, WorkerVersion)
	config, err := kiteConfig.Get()
	if err != nil {
		Log.Error("%s. initializing kite failed", err.Error())
		return nil
	}

	// set skeleton config
	k.Config = config

	// create a new connection to the cloud
	kiteClient := k.NewClient(kloudAddr)
	kiteClient.Auth = &kite.Auth{
		Type: "kloudctl",
		Key:  kloudSecretKey,
	}

	// dial the kloud address
	if err := kiteClient.DialTimeout(time.Second * 10); err != nil {
		Log.Error("%s. Is kloud/kontrol running?", err.Error())
		return nil
	}

	Log.Info("Connected to klient: %s", kloudAddr)

	return kiteClient
}
{'fraction_non_alphanumeric': 0.1255824790307549, 'fraction_numerical': 0.005125815470643057, 'mean_word_length': 4.178528347406514, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5923621', 'n_tokens_mistral': 1610, 'n_tokens_neox': 1501, 'n_words': 341}