-
Notifications
You must be signed in to change notification settings - Fork 19
/
agent.go
112 lines (96 loc) · 2.42 KB
/
agent.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
// Copyright 2020 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package upload
import (
"context"
"fmt"
"sync"
"github.com/moov-io/achgateway/internal/service"
"github.com/moov-io/base/admin"
"github.com/moov-io/base/log"
)
// Agent represents an interface for uploading and retrieving ACH files from a remote service.
type Agent interface {
ID() string
GetInboundFiles(ctx context.Context) ([]string, error)
GetReconciliationFiles(ctx context.Context) ([]string, error)
GetReturnFiles(ctx context.Context) ([]string, error)
UploadFile(ctx context.Context, f File) error
Delete(ctx context.Context, path string) error
ReadFile(ctx context.Context, path string) (*File, error)
InboundPath() string
OutboundPath() string
ReconciliationPath() string
ReturnPath() string
Hostname() string
Ping() error
Close() error
}
var (
createdAgents = &CreatedAgents{}
)
func New(logger log.Logger, cfg service.UploadAgents, id string) (Agent, error) {
createdAgents.mu.Lock()
defer createdAgents.mu.Unlock()
// lookup cached
for i := range createdAgents.agents {
if createdAgents.agents[i].ID() == id {
return createdAgents.agents[i], nil
}
}
// Create the new agent
var agent Agent
if conf := cfg.Find(id); conf != nil {
if conf.FTP != nil {
aa, err := newFTPTransferAgent(logger, conf)
if err != nil {
return nil, err
}
agent = aa
}
if conf.SFTP != nil {
aa, err := newSFTPTransferAgent(logger, conf)
if err != nil {
return nil, err
}
agent = aa
}
if conf.Mock != nil {
agent = &MockAgent{}
}
}
if agent == nil {
return nil, fmt.Errorf("upload: unknown Agent ID=%s", id)
}
if cfg.Retry != nil {
retr, err := newRetryAgent(logger, agent, cfg.Retry)
if err != nil {
return nil, err
}
agent = retr
}
createdAgents.register(agent)
return agent, nil
}
type CreatedAgents struct {
mu sync.Mutex
agents []Agent
adminServer *admin.Server
}
func RegisterAdminServer(svc *admin.Server) {
createdAgents.mu.Lock()
defer createdAgents.mu.Unlock()
if createdAgents.adminServer == nil && svc != nil {
createdAgents.adminServer = svc
}
}
func (as *CreatedAgents) register(agent Agent) {
// track agent
as.agents = append(as.agents, agent)
// register liveness probe
if as.adminServer != nil {
kind := fmt.Sprintf("%T", agent)
as.adminServer.AddLivenessCheck(kind, agent.Ping)
}
}