about summary refs log tree commit diff stats
path: root/html/052tangle.cc.html
Commit message (Expand)AuthorAgeFilesLines
* 3431Kartik K. Agaram2016-09-301-42/+43
* 3395Kartik K. Agaram2016-09-171-58/+63
* 3117Kartik K. Agaram2016-07-201-1/+0
* 3013Kartik K. Agaram2016-05-261-4/+8
* 2996Kartik K. Agaram2016-05-211-2/+33
* 2875Kartik K. Agaram2016-04-271-7/+8
* 2812Kartik K. Agaram2016-03-271-15/+24
* 2745Kartik K. Agaram2016-03-091-1/+1
* 2744Kartik K. Agaram2016-03-091-3/+3
* 2743Kartik K. Agaram2016-03-091-69/+61
* 2706 - update htmlKartik K. Agaram2016-02-251-5/+5
* 2625Kartik K. Agaram2016-02-021-1/+1
* 2605Kartik K. Agaram2016-01-261-7/+1
* 2611Kartik K. Agaram2015-11-291-1/+1
* 2423 - describe shape-shifting in html docsKartik K. Agaram2015-11-101-25/+27
* 2175Kartik K. Agaram2015-09-061-49/+176
* 2062Kartik K. Agaram2015-08-231-23/+22
* 1949Kartik K. Agaram2015-08-061-33/+130
* 1885Kartik K. Agaram2015-07-291-0/+235
0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
package svc // import "github.com/getwtxt/getwtxt/svc"

import (
	"log"
	"time"

	"github.com/fsnotify/fsnotify"
)

// Functions and types in this file pertain
// to periodic, regular actions.

// This is a wrapper for a *time.Ticker
// that adds another channel. It's used
// to signal to the ticker goroutines
// that they should stop the tickers
// and exit.
type tick struct {
	isDB bool
	t    *time.Ticker
	exit chan bool
}

// Creates a new instance of a tick
func initTicker(db bool, interval time.Duration) *tick {
	return &tick{
		isDB: db,
		t:    time.NewTicker(interval),
		exit: make(chan bool, 1),
	}
}

// Sends the signal to stop the tickers
// and for their respective goroutines
// to exit.
func killTickers() {
	ct := <-cTickC
	dt := <-dbTickC
	ct.exit <- true
	dt.exit <- true
}

// Waits for a signal from the database
// *tick. Either stops the ticker and
// kills the goroutine or it will
// update cache / push the DB to disk
func dataTimer(tkr *tick) {
	for {
		select {
		case signal := <-tkr.t.C:
			if tkr.isDB {
				errLog("", pushDB())
				log.Printf("Database push took: %v\n", time.Since(signal))
				continue
			}
			cacheUpdate()
			log.Printf("Cache update took: %v\n", time.Since(signal))
		case <-tkr.exit:
			tkr.t.Stop()
			return
		}
	}
}

// Called when a change is detected in the
// configuration file. Closes log file,
// closes database connection, stops all
// tickers, then binds new configuration
// values, opens new log file, connects to
// new database, and starts new cache and
// database tickers.
func reInit(e fsnotify.Event) {
	log.Printf("%v. Reloading...\n", e.String())

	if !confObj.StdoutLogging {
		closeLog <- true
	}

	killTickers()
	killDB()

	bindConfig()

	initLogging()
	initDatabase()
	initPersistence()
}

// Starts the tickers that periodically:
//  - pull new user statuses into cache
//  - push cached data to disk
func initPersistence() {
	confObj.Mu.RLock()
	cacheTkr := initTicker(false, confObj.CacheInterval)
	dbTkr := initTicker(true, confObj.DBInterval)
	confObj.Mu.RUnlock()

	go dataTimer(cacheTkr)
	go dataTimer(dbTkr)

	dbTickC <- dbTkr
	cTickC <- cacheTkr
}