mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-19 00:27:06 +00:00
feat: send heartbeat ASAP on each new calendar day
This commit is contained in:
@ -19,6 +19,7 @@
|
||||
package bridge
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@ -72,7 +73,10 @@ func New(
|
||||
}
|
||||
|
||||
if s.GetBool(settings.FirstStartKey) {
|
||||
b.SendMetric(metrics.New(metrics.Setup, metrics.FirstStart, metrics.Label(constants.Version)))
|
||||
if err := b.SendMetric(metrics.New(metrics.Setup, metrics.FirstStart, metrics.Label(constants.Version))); err != nil {
|
||||
logrus.WithError(err).Error("Failed to send metric")
|
||||
}
|
||||
|
||||
s.SetBool(settings.FirstStartKey, false)
|
||||
}
|
||||
|
||||
@ -83,19 +87,25 @@ func New(
|
||||
|
||||
// heartbeat sends a heartbeat signal once a day.
|
||||
func (b *Bridge) heartbeat() {
|
||||
ticker := time.NewTicker(1 * time.Minute)
|
||||
|
||||
for range ticker.C {
|
||||
next, err := strconv.ParseInt(b.settings.Get(settings.NextHeartbeatKey), 10, 64)
|
||||
for range time.Tick(time.Minute) {
|
||||
lastHeartbeatDay, err := strconv.ParseInt(b.settings.Get(settings.LastHeartbeatKey), 10, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
nextTime := time.Unix(next, 0)
|
||||
if time.Now().After(nextTime) {
|
||||
b.SendMetric(metrics.New(metrics.Heartbeat, metrics.Daily, metrics.NoLabel))
|
||||
nextTime = nextTime.Add(24 * time.Hour)
|
||||
b.settings.Set(settings.NextHeartbeatKey, strconv.FormatInt(nextTime.Unix(), 10))
|
||||
|
||||
// If we're still on the same day, don't send a heartbeat.
|
||||
if time.Now().YearDay() == int(lastHeartbeatDay) {
|
||||
continue
|
||||
}
|
||||
|
||||
// We're on the next (or a different) day, so send a heartbeat.
|
||||
if err := b.SendMetric(metrics.New(metrics.Heartbeat, metrics.Daily, metrics.NoLabel)); err != nil {
|
||||
logrus.WithError(err).Error("Failed to send heartbeat")
|
||||
continue
|
||||
}
|
||||
|
||||
// Heartbeat was sent successfully so update the last heartbeat day.
|
||||
b.settings.Set(settings.LastHeartbeatKey, fmt.Sprintf("%v", time.Now().YearDay()))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -29,7 +29,7 @@ import (
|
||||
const (
|
||||
FirstStartKey = "first_time_start"
|
||||
FirstStartGUIKey = "first_time_start_gui"
|
||||
NextHeartbeatKey = "next_heartbeat"
|
||||
LastHeartbeatKey = "last_heartbeat"
|
||||
APIPortKey = "user_port_api"
|
||||
IMAPPortKey = "user_port_imap"
|
||||
SMTPPortKey = "user_port_smtp"
|
||||
@ -71,7 +71,7 @@ const (
|
||||
func (s *Settings) setDefaultValues() {
|
||||
s.setDefault(FirstStartKey, "true")
|
||||
s.setDefault(FirstStartGUIKey, "true")
|
||||
s.setDefault(NextHeartbeatKey, fmt.Sprintf("%v", time.Now().Unix()))
|
||||
s.setDefault(LastHeartbeatKey, fmt.Sprintf("%v", time.Now().YearDay()))
|
||||
s.setDefault(AllowProxyKey, "true")
|
||||
s.setDefault(AutostartKey, "true")
|
||||
s.setDefault(AutoUpdateKey, "true")
|
||||
|
||||
@ -21,6 +21,7 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/ProtonMail/proton-bridge/internal/metrics"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type metricsManager struct {
|
||||
@ -44,21 +45,31 @@ func newExportMetricsManager(ie *ImportExport) *metricsManager {
|
||||
|
||||
func (m *metricsManager) Load(numberOfMailboxes int) {
|
||||
label := strconv.Itoa(numberOfMailboxes)
|
||||
m.ie.SendMetric(metrics.New(m.category, metrics.TransferLoad, metrics.Label(label)))
|
||||
if err := m.ie.SendMetric(metrics.New(m.category, metrics.TransferLoad, metrics.Label(label))); err != nil {
|
||||
logrus.WithError(err).Error("Failed to send metric")
|
||||
}
|
||||
}
|
||||
|
||||
func (m *metricsManager) Start() {
|
||||
m.ie.SendMetric(metrics.New(m.category, metrics.TransferStart, metrics.NoLabel))
|
||||
if err := m.ie.SendMetric(metrics.New(m.category, metrics.TransferStart, metrics.NoLabel)); err != nil {
|
||||
logrus.WithError(err).Error("Failed to send metric")
|
||||
}
|
||||
}
|
||||
|
||||
func (m *metricsManager) Complete() {
|
||||
m.ie.SendMetric(metrics.New(m.category, metrics.TransferComplete, metrics.NoLabel))
|
||||
if err := m.ie.SendMetric(metrics.New(m.category, metrics.TransferComplete, metrics.NoLabel)); err != nil {
|
||||
logrus.WithError(err).Error("Failed to send metric")
|
||||
}
|
||||
}
|
||||
|
||||
func (m *metricsManager) Cancel() {
|
||||
m.ie.SendMetric(metrics.New(m.category, metrics.TransferCancel, metrics.NoLabel))
|
||||
if err := m.ie.SendMetric(metrics.New(m.category, metrics.TransferCancel, metrics.NoLabel)); err != nil {
|
||||
logrus.WithError(err).Error("Failed to send metric")
|
||||
}
|
||||
}
|
||||
|
||||
func (m *metricsManager) Fail() {
|
||||
m.ie.SendMetric(metrics.New(m.category, metrics.TransferFail, metrics.NoLabel))
|
||||
if err := m.ie.SendMetric(metrics.New(m.category, metrics.TransferFail, metrics.NoLabel)); err != nil {
|
||||
logrus.WithError(err).Error("Failed to send metric")
|
||||
}
|
||||
}
|
||||
|
||||
@ -324,7 +324,9 @@ func (u *Users) addNewUser(apiUser *pmapi.User, auth *pmapi.Auth, hashedPassphra
|
||||
return errors.Wrap(err, "failed to initialise user")
|
||||
}
|
||||
|
||||
u.SendMetric(metrics.New(metrics.Setup, metrics.NewUser, metrics.NoLabel))
|
||||
if err := u.SendMetric(metrics.New(metrics.Setup, metrics.NewUser, metrics.NoLabel)); err != nil {
|
||||
log.WithError(err).Error("Failed to send metric")
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
@ -444,13 +446,13 @@ func (u *Users) DeleteUser(userID string, clearStore bool) error {
|
||||
}
|
||||
|
||||
// SendMetric sends a metric. We don't want to return any errors, only log them.
|
||||
func (u *Users) SendMetric(m metrics.Metric) {
|
||||
func (u *Users) SendMetric(m metrics.Metric) error {
|
||||
c := u.clientManager.GetAnonymousClient()
|
||||
defer c.Logout()
|
||||
|
||||
cat, act, lab := m.Get()
|
||||
if err := c.SendSimpleMetric(string(cat), string(act), string(lab)); err != nil {
|
||||
log.Error("Sending metric failed: ", err)
|
||||
return err
|
||||
}
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
@ -458,6 +460,8 @@ func (u *Users) SendMetric(m metrics.Metric) {
|
||||
"act": act,
|
||||
"lab": lab,
|
||||
}).Debug("Metric successfully sent")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AllowProxy instructs the app to use DoH to access an API proxy if necessary.
|
||||
|
||||
@ -22,6 +22,11 @@ Changelog [format](http://keepachangelog.com/en/1.0.0/)
|
||||
* GODT-806 Changed GUI dialog on manual update. Added autoupdates checkbox. Simplifyed installation process GUI.
|
||||
* Bump gopenpgp dependency to v2.1.3 for improved memory usage.
|
||||
* GODT-912 Changed scroll bar behaviour in settings tab
|
||||
* GODT-858 Bump go-rfc5322 dependency to v0.5.0 to handle some invalid RFC5322 groups and add support for semicolon delimiter in address-list.
|
||||
* GODT-923 Fix listener locking.
|
||||
* GODT-389 Prefer `From` header instead of `MAIL FROM` address.
|
||||
* GODT-898 Only set ContentID for inline attachments.
|
||||
* GODT-149 Send heartbeat ASAP on each new calendar day.
|
||||
|
||||
### Removed
|
||||
* GODT-208 Remove deprecated use of BuildNameToCertificate.
|
||||
@ -31,3 +36,9 @@ Changelog [format](http://keepachangelog.com/en/1.0.0/)
|
||||
|
||||
### Fixed
|
||||
* GODT-979 Fix panic when trying to parse a multipart/alternative section that has no child sections.
|
||||
### Changed
|
||||
* GODT-389 Prefer `From` header instead of `MAIL FROM` address.
|
||||
* GODT-898 Only set ContentID for inline attachments.
|
||||
* GODT-773 Replace `INTERNALDATE` older than birthday of RFC822 by birthday of RFC822 to not crash Apple Mail.
|
||||
* GODT-927 Avoid to call API with empty label name.
|
||||
* GODT-732 Fix usage of fontawesome
|
||||
|
||||
Reference in New Issue
Block a user