Skip to content

Commit e0db1ea

Browse files
MarcosNicolauJuArceuri-99
authored
fix: don't start operator if last_processed_batch_filepath isn't provided (#1211)
Co-authored-by: JuArce <[email protected]> Co-authored-by: Urix <[email protected]>
1 parent 73e0481 commit e0db1ea

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

operator/pkg/operator.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"log"
1111
"os"
12+
"path/filepath"
1213
"sync"
1314
"time"
1415

@@ -110,6 +111,10 @@ func NewOperatorFromConfig(configuration config.OperatorConfig) (*Operator, erro
110111
address := configuration.Operator.Address
111112
lastProcessedBatchLogFile := configuration.Operator.LastProcessedBatchFilePath
112113

114+
if lastProcessedBatchLogFile == "" {
115+
logger.Fatalf("Config file field: `last_processed_batch_filepath` not provided.")
116+
}
117+
113118
// Metrics
114119
reg := prometheus.NewRegistry()
115120
operatorMetrics := metrics.NewMetrics(configuration.Operator.MetricsIpPortAddress, reg, logger)
@@ -136,7 +141,10 @@ func NewOperatorFromConfig(configuration config.OperatorConfig) (*Operator, erro
136141
// Socket
137142
}
138143

139-
_ = operator.LoadLastProcessedBatch()
144+
err = operator.LoadLastProcessedBatch()
145+
if err != nil {
146+
logger.Fatalf("Error while loading last process batch: %v. This is probably related to the `last_processed_batch_filepath` field passed in the config file", err)
147+
}
140148

141149
return operator, nil
142150
}
@@ -155,16 +163,26 @@ type OperatorLastProcessedBatch struct {
155163
}
156164

157165
func (o *Operator) LoadLastProcessedBatch() error {
166+
// check if the directory exist
167+
folderPath := filepath.Dir(o.lastProcessedBatchLogFile)
168+
_, err := os.Stat(folderPath)
169+
170+
if os.IsNotExist(err) {
171+
return err
172+
}
173+
158174
file, err := os.ReadFile(o.lastProcessedBatchLogFile)
159175

176+
// if the file does not exist, we don't return an err, as it will get created later
177+
// that is why we check of the directory exist in the first place
160178
if err != nil {
161-
return fmt.Errorf("failed read from file: %v", err)
179+
return nil
162180
}
163181

164182
err = json.Unmarshal(file, &o.lastProcessedBatch)
165183

166184
if err != nil {
167-
return fmt.Errorf("failed to unmarshal batch: %v", err)
185+
return err
168186
}
169187

170188
return nil

0 commit comments

Comments
 (0)