-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch.go
155 lines (144 loc) · 4.15 KB
/
batch.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright 2022-2025 Thorsten A. Knieling
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*/
package clu
import (
"os"
"sync"
"time"
"github.com/tknie/errorrepo"
"github.com/tknie/flynn"
"github.com/tknie/flynn/common"
"github.com/tknie/log"
"github.com/tknie/services"
)
// BatchEntry entry of batch repository
type BatchEntry struct {
Name string
Query string `flynn:":BLOB"`
Database string
ParamCount int
}
var batchDbRef *common.Reference
var batchDbPassword = ""
var batchtablename = ""
var batchLock sync.Mutex
var batchStoreOnline = false
func openBatchRepository() (common.RegDbID, error) {
var err error
if userDbPassword == "" {
userDbPassword = os.Getenv("REST_BATCH_PASS")
}
sessionStoreID, err := flynn.Handler(batchDbRef, batchDbPassword)
if err != nil {
services.ServerMessage("Register error log: %v", err)
return 0, err
}
return sessionStoreID, nil
}
// InitBatchWatcherThread initialize batch watcher
func InitBatchWatcherThread() {
// dm := Viewer.Database.BatchRepository
// if dm != nil {
// r, err := Handles(dm)
// if err == nil {
// clu.InitBatchRepository(r, os.ExpandEnv(dm.Password), os.ExpandEnv(dm.Table))
// } else {
// log.Fatal("batch repository store not being able to start:", err)
// }
// }
dm := Viewer.Database.BatchRepository
if dm != nil {
for {
r, err := dm.Handles()
if err == nil {
if InitBatchRepository(r, os.ExpandEnv(dm.Password), os.ExpandEnv(dm.Table)) {
break
}
time.Sleep(5 * time.Minute)
} else {
services.ServerMessage("Batch repository store not being able to start: %v", err)
}
}
}
}
// InitBatchRepository init batch repository
func InitBatchRepository(dbRef *common.Reference, dbPassword, tablename string) bool {
batchDbRef = dbRef
batchDbPassword = dbPassword
batchStoreID, err := openBatchRepository()
if err != nil {
services.ServerMessage("Register error log: %v", err)
return false
}
log.Log.Debugf("Receive batch store handler %s", batchStoreID)
defer batchStoreID.FreeHandler()
defer batchStoreID.Close()
dbTables := flynn.Maps()
for _, d := range dbTables {
if d == tablename {
batchtablename = tablename
batchStoreOnline = true
log.Log.Debugf("batch store online = %v", batchStoreOnline)
services.ServerMessage("Using batch repository on table '%s'", batchtablename)
return true
}
}
su := &BatchEntry{}
err = batchStoreID.CreateTable(tablename, su)
if err != nil {
services.ServerMessage("Database batch store creating failed: %v", err)
return false
}
batchtablename = tablename
batchStoreOnline = true
services.ServerMessage("Database batch store '%s' created successfully", batchtablename)
return true
}
// BatchSelect search for batchname in an batch repository
func BatchSelect(batchname string) (*BatchEntry, error) {
log.Log.Debugf("batch select online = %v", batchStoreOnline)
if !batchStoreOnline {
log.Log.Debugf("batch rep disabled online = %v", batchStoreOnline)
return nil, errorrepo.NewError("REST00003")
}
batchLock.Lock()
defer batchLock.Unlock()
batchStoreID, err := openBatchRepository()
if err != nil {
services.ServerMessage("Register error log: %v", err)
return nil, err
}
log.Log.Debugf("Receive batch store handler %s", batchStoreID)
defer batchStoreID.FreeHandler()
defer batchStoreID.Close()
var b *BatchEntry
q := &common.Query{TableName: batchtablename,
Search: "name='" + batchname + "'",
DataStruct: &BatchEntry{},
Fields: []string{"*"}}
_, err = batchStoreID.Query(q, func(search *common.Query, result *common.Result) error {
if b == nil {
b = result.Data.(*BatchEntry)
}
return nil
})
if err != nil {
log.Log.Debugf("Failure batch select online = %v", batchStoreOnline)
log.Log.Errorf("Query batch store failure: %v", err)
return nil, err
}
if b == nil {
log.Log.Debugf("Fail batch select online = %v", batchStoreOnline)
return nil, errorrepo.NewError("REST00004")
}
log.Log.Debugf("Return batch select online = %v", batchStoreOnline)
return b, nil
}