diff --git a/server/runner/license/runner.go b/server/runner/license/runner.go new file mode 100644 index 0000000..11fe4bc --- /dev/null +++ b/server/runner/license/runner.go @@ -0,0 +1,47 @@ +package license + +import ( + "context" + "log/slog" + "time" + + "github.com/yourselfhosted/slash/server/service/license" + "github.com/yourselfhosted/slash/store" +) + +type Runner struct { + Store *store.Store + + licenseService *license.LicenseService +} + +func NewRunner(store *store.Store, licenseService *license.LicenseService) *Runner { + return &Runner{ + Store: store, + licenseService: licenseService, + } +} + +// Schedule runner every 12 hours. +const runnerInterval = time.Hour * 12 + +func (r *Runner) Run(ctx context.Context) { + ticker := time.NewTicker(runnerInterval) + defer ticker.Stop() + + for { + select { + case <-ticker.C: + r.RunOnce(ctx) + case <-ctx.Done(): + return + } + } +} + +func (r *Runner) RunOnce(ctx context.Context) { + // Load subscription. + if _, err := r.licenseService.LoadSubscription(ctx); err != nil { + slog.Error("failed to load subscription", slog.Any("error", err)) + } +} diff --git a/server/runner/version/runner.go b/server/runner/version/runner.go new file mode 100644 index 0000000..043d174 --- /dev/null +++ b/server/runner/version/runner.go @@ -0,0 +1,43 @@ +// Package version provides a runner to check the latest version of the application. +package version + +import ( + "context" + "time" + + "github.com/yourselfhosted/slash/server/profile" + "github.com/yourselfhosted/slash/store" +) + +type Runner struct { + Store *store.Store + Profile *profile.Profile +} + +func NewRunner(store *store.Store, profile *profile.Profile) *Runner { + return &Runner{ + Store: store, + Profile: profile, + } +} + +// Schedule checker every 8 hours. +const runnerInterval = time.Hour * 8 + +func (r *Runner) Run(ctx context.Context) { + ticker := time.NewTicker(runnerInterval) + defer ticker.Stop() + + for { + select { + case <-ticker.C: + r.RunOnce(ctx) + case <-ctx.Done(): + return + } + } +} + +func (*Runner) RunOnce(_ context.Context) { + // Implement me. +} diff --git a/server/server.go b/server/server.go index 6d68b77..74d3e18 100644 --- a/server/server.go +++ b/server/server.go @@ -17,6 +17,8 @@ import ( "github.com/yourselfhosted/slash/server/profile" apiv1 "github.com/yourselfhosted/slash/server/route/api/v1" "github.com/yourselfhosted/slash/server/route/frontend" + licensern "github.com/yourselfhosted/slash/server/runner/license" + "github.com/yourselfhosted/slash/server/runner/version" "github.com/yourselfhosted/slash/server/service/license" "github.com/yourselfhosted/slash/store" ) @@ -79,10 +81,7 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store } func (s *Server) Start(ctx context.Context) error { - // Load subscription. - if _, err := s.licenseService.LoadSubscription(ctx); err != nil { - slog.Error("failed to load subscription", slog.Any("error", err)) - } + s.StartBackgroundRunners(ctx) // Start gRPC server. listen, err := net.Listen("tcp", fmt.Sprintf(":%d", s.Profile.Port+1)) if err != nil { @@ -119,6 +118,16 @@ func (s *Server) GetEcho() *echo.Echo { return s.e } +func (s *Server) StartBackgroundRunners(ctx context.Context) { + licenseRunner := licensern.NewRunner(s.Store, s.licenseService) + licenseRunner.RunOnce(ctx) + versionRunner := version.NewRunner(s.Store, s.Profile) + versionRunner.RunOnce(ctx) + + go licenseRunner.Run(ctx) + go versionRunner.Run(ctx) +} + func (s *Server) getSecretSession(ctx context.Context) (string, error) { workspaceGeneralSetting, err := s.Store.GetWorkspaceGeneralSetting(ctx) if err != nil {