This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
client_internal.go
100 lines (85 loc) · 2.94 KB
/
client_internal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package bux
import (
"context"
"github.com/BuxOrg/bux/cachestore"
"github.com/BuxOrg/bux/chainstate"
"github.com/BuxOrg/bux/datastore"
"github.com/BuxOrg/bux/taskmanager"
"github.com/tonicpow/go-paymail"
)
// loadCache will load caching configuration and start the Cachestore client
func (c *Client) loadCache(ctx context.Context) (err error) {
// Load if not set by the user
if c.options.cacheStore.ClientInterface == nil {
c.options.cacheStore.ClientInterface, err = cachestore.NewClient(ctx, c.options.cacheStore.options...)
}
return
}
// loadChainstate will load chainstate configuration and start the Chainstate client
func (c *Client) loadChainstate(ctx context.Context) (err error) {
// Load if not set by the user
if c.options.chainstate.ClientInterface == nil {
c.options.chainstate.ClientInterface, err = chainstate.NewClient(ctx, c.options.chainstate.options...)
}
return
}
// loadDatastore will load the Datastore and start the Datastore client
//
// NOTE: this will run database migrations if the options was set
func (c *Client) loadDatastore(ctx context.Context) (err error) {
// Add the models to migrate (after loading the client options)
if len(c.options.models.migrateModelNames) > 0 {
c.options.dataStore.options = append(
c.options.dataStore.options,
datastore.WithAutoMigrate(c.options.models.migrateModels...),
)
}
// Load client (runs ALL options, IE: auto migrate models)
c.options.dataStore.ClientInterface, err = datastore.NewClient(
ctx, c.options.dataStore.options...,
)
return
}
// loadPaymailClient will load the Paymail client
func (c *Client) loadPaymailClient() (err error) {
// Only load if it's not set (the client can be overloaded)
if c.options.paymail.client == nil {
c.options.paymail.client, err = paymail.NewClient()
}
return
}
// loadTaskmanager will load the TaskManager and start the TaskManager client
func (c *Client) loadTaskmanager(ctx context.Context) (err error) {
// Load if not set by the user
if c.options.taskManager.ClientInterface == nil {
c.options.taskManager.ClientInterface, err = taskmanager.NewClient(
ctx, c.options.taskManager.options...,
)
}
return
}
// runModelMigrations will run the model Migrate() method for all models
func (c *Client) runModelMigrations(models ...interface{}) (err error) {
d := c.Datastore()
for _, model := range models {
if err = model.(ModelInterface).Migrate(d); err != nil {
return
}
}
return
}
// runModelRegisterTasks will run the model RegisterTasks() method for all models
func (c *Client) runModelRegisterTasks(models ...interface{}) (err error) {
for _, model := range models {
model.(ModelInterface).SetOptions(c.DefaultModelOptions()...)
if err = model.(ModelInterface).RegisterTasks(); err != nil {
return
}
}
return
}
// registerAllTasks will register all tasks for all models
func (c *Client) registerAllTasks() error {
c.Taskmanager().ResetCron()
return c.runModelRegisterTasks(c.options.models.models...)
}