mirror of
https://github.com/christianselig/apollo-backend
synced 2024-12-22 22:35:27 +00:00
add some precision to last checked
This commit is contained in:
parent
a929de67d4
commit
69e7cfea71
2 changed files with 6 additions and 7 deletions
|
@ -31,7 +31,7 @@ type application struct {
|
||||||
var (
|
var (
|
||||||
workers int = runtime.NumCPU() * 6
|
workers int = runtime.NumCPU() * 6
|
||||||
rate float64 = 0.1
|
rate float64 = 0.1
|
||||||
backoff int64 = 5
|
backoff int = 5
|
||||||
)
|
)
|
||||||
|
|
||||||
func accountWorker(id int, rc *reddit.Client, db *sql.DB, logger *log.Logger, statsd *statsd.Client, quit chan bool) {
|
func accountWorker(id int, rc *reddit.Client, db *sql.DB, logger *log.Logger, statsd *statsd.Client, quit chan bool) {
|
||||||
|
@ -54,8 +54,7 @@ func accountWorker(id int, rc *reddit.Client, db *sql.DB, logger *log.Logger, st
|
||||||
case <-quit:
|
case <-quit:
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
now := time.Now().UTC().Unix()
|
now := float64(time.Now().UTC().UnixNano()/int64(time.Millisecond)) / 1000
|
||||||
|
|
||||||
tx, err := db.Begin()
|
tx, err := db.Begin()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -81,14 +80,14 @@ func accountWorker(id int, rc *reddit.Client, db *sql.DB, logger *log.Logger, st
|
||||||
}
|
}
|
||||||
|
|
||||||
if account.LastCheckedAt > 0 {
|
if account.LastCheckedAt > 0 {
|
||||||
latency := now - account.LastCheckedAt - backoff
|
latency := now - account.LastCheckedAt - float64(backoff)
|
||||||
statsd.Histogram("apollo.queue.delay", float64(latency), []string{}, rate)
|
statsd.Histogram("apollo.queue.delay", float64(latency), []string{}, rate)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = tx.Exec(`UPDATE accounts SET last_checked_at = $1 WHERE id = $2`, now, account.ID)
|
_, err = tx.Exec(`UPDATE accounts SET last_checked_at = $1 WHERE id = $2`, now, account.ID)
|
||||||
|
|
||||||
rac := rc.NewAuthenticatedClient(account.RefreshToken, account.AccessToken)
|
rac := rc.NewAuthenticatedClient(account.RefreshToken, account.AccessToken)
|
||||||
if account.ExpiresAt < now {
|
if account.ExpiresAt < int64(now) {
|
||||||
tokens, _ := rac.RefreshTokens()
|
tokens, _ := rac.RefreshTokens()
|
||||||
tx.Exec(`UPDATE accounts SET access_token = $1, refresh_token = $2, expires_at = $3 WHERE id = $4`,
|
tx.Exec(`UPDATE accounts SET access_token = $1, refresh_token = $2, expires_at = $3 WHERE id = $4`,
|
||||||
tokens.AccessToken, tokens.RefreshToken, now+3500, account.ID)
|
tokens.AccessToken, tokens.RefreshToken, now+3500, account.ID)
|
||||||
|
@ -111,7 +110,7 @@ func accountWorker(id int, rc *reddit.Client, db *sql.DB, logger *log.Logger, st
|
||||||
// Set latest message we alerted on
|
// Set latest message we alerted on
|
||||||
latestMsg := msgs.MessageListing.Messages[0]
|
latestMsg := msgs.MessageListing.Messages[0]
|
||||||
|
|
||||||
latency := float64(now) - latestMsg.CreatedAt
|
latency := now - latestMsg.CreatedAt
|
||||||
statsd.Histogram("apollo.notification.latency", latency, []string{}, rate)
|
statsd.Histogram("apollo.notification.latency", latency, []string{}, rate)
|
||||||
|
|
||||||
_, err = tx.Exec(`UPDATE accounts SET last_message_id = $1 WHERE id = $2`, latestMsg.FullName(), account.ID)
|
_, err = tx.Exec(`UPDATE accounts SET last_message_id = $1 WHERE id = $2`, latestMsg.FullName(), account.ID)
|
||||||
|
|
|
@ -12,7 +12,7 @@ type Account struct {
|
||||||
RefreshToken string
|
RefreshToken string
|
||||||
ExpiresAt int64
|
ExpiresAt int64
|
||||||
LastMessageID string
|
LastMessageID string
|
||||||
LastCheckedAt int64
|
LastCheckedAt float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Account) NormalizedUsername() string {
|
func (a *Account) NormalizedUsername() string {
|
||||||
|
|
Loading…
Reference in a new issue