go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/nodes/worker/main.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package main
     9  
    10  import (
    11  	"context"
    12  	"os"
    13  
    14  	"go.charczuk.com/sdk/apputil"
    15  	"go.charczuk.com/sdk/db"
    16  	"go.charczuk.com/sdk/db/dbutil"
    17  	"go.charczuk.com/sdk/db/migration"
    18  	"go.charczuk.com/sdk/logutil"
    19  	"go.charczuk.com/sdk/slant"
    20  	"go.temporal.io/sdk/worker"
    21  
    22  	"go.charczuk.com/projects/nodes/pkg/config"
    23  	"go.charczuk.com/projects/nodes/pkg/dbmodel"
    24  	"go.charczuk.com/projects/nodes/pkg/temporalutil"
    25  
    26  	"go.charczuk.com/projects/nodes/worker/activities"
    27  	"go.charczuk.com/projects/nodes/worker/workflows"
    28  )
    29  
    30  var entrypoint = apputil.DBEntryPoint[config.Config]{
    31  	Setup: func(ctx context.Context, cfg config.Config) error {
    32  		return dbutil.CreateDatabaseIfNotExists(ctx, cfg.DB.Database, db.OptLog(logutil.GetLogger(ctx)))
    33  	},
    34  	Migrate: func(ctx context.Context, cfg config.Config, dbc *db.Connection) error {
    35  		return dbmodel.Migrations(migration.OptLog(logutil.GetLogger(ctx))).Apply(ctx, dbc)
    36  	},
    37  	Start: func(ctx context.Context, cfg config.Config, dbc *db.Connection) error {
    38  		logger := logutil.GetLogger(ctx)
    39  		logutil.Debugf(logger, "using temporal hostport: %s", cfg.Temporal.HostPort)
    40  
    41  		slant.Print(os.Stdout, "nodes-wrk")
    42  
    43  		client, err := temporalutil.TryConnect(ctx, cfg.Temporal)
    44  		if err != nil {
    45  			return err
    46  		}
    47  		defer client.Close()
    48  
    49  		dbmodelMgr := dbmodel.Manager{BaseManager: dbutil.NewBaseManager(dbc)}
    50  
    51  		w := worker.New(client, "default", worker.Options{})
    52  		w.RegisterWorkflow(workflows.Stabilize)
    53  		w.RegisterActivity(&activities.Activities{
    54  			Model: dbmodelMgr,
    55  		})
    56  		err = w.Run(worker.InterruptCh())
    57  		if err != nil {
    58  			logutil.Debugf(logger, "temporal worker error: %v", err)
    59  			return err
    60  		}
    61  		return nil
    62  	},
    63  }
    64  
    65  func init() {
    66  	entrypoint.Init()
    67  }
    68  
    69  func main() {
    70  	entrypoint.Main()
    71  }