github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/updater/service/service.go (about)

     1  // Copyright 2016 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package main
     5  
     6  import (
     7  	"github.com/keybase/client/go/updater"
     8  	"github.com/keybase/client/go/updater/util"
     9  )
    10  
    11  // Log is the logging interface for the service package
    12  type Log interface {
    13  	Debug(...interface{})
    14  	Info(...interface{})
    15  	Debugf(s string, args ...interface{})
    16  	Infof(s string, args ...interface{})
    17  	Warningf(s string, args ...interface{})
    18  	Errorf(s string, args ...interface{})
    19  }
    20  
    21  type service struct {
    22  	updater       *updater.Updater
    23  	updateChecker *updater.UpdateChecker
    24  	context       updater.Context
    25  	log           Log
    26  	appName       string
    27  	ch            chan int
    28  }
    29  
    30  func newService(upd *updater.Updater, context updater.Context, log Log, appName string) *service {
    31  	svc := service{
    32  		updater: upd,
    33  		context: context,
    34  		log:     log,
    35  		appName: appName,
    36  		ch:      make(chan int),
    37  	}
    38  	return &svc
    39  }
    40  
    41  func (s *service) Start() {
    42  	if s.updateChecker == nil {
    43  		tickDuration := util.EnvDuration("KEYBASE_UPDATER_DELAY", updater.DefaultTickDuration)
    44  		s.updater.SetTickDuration(tickDuration)
    45  		updateChecker := updater.NewUpdateChecker(s.updater, s.context, tickDuration, s.log)
    46  		s.updateChecker = &updateChecker
    47  	}
    48  	s.updateChecker.Start()
    49  }
    50  
    51  func (s *service) Run() {
    52  	closer, err := s.lockPID()
    53  	if err != nil {
    54  		s.log.Errorf("updater service not starting due to lockPID error: %s", err)
    55  		return
    56  	}
    57  	defer closer.Close()
    58  
    59  	s.Start()
    60  	<-s.ch
    61  }
    62  
    63  func (s *service) Quit() {
    64  	s.ch <- 0
    65  }