-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: use fetcher controller * chore: moved state to a separate module * chore: moved controller to a separate module * chore: add controller tests and fetcher stubs * chore: fixed typo * chore: use mutex in state * chore: add state test
- Loading branch information
1 parent
aa542a9
commit c262e48
Showing
48 changed files
with
455 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"main/pkg/constants" | ||
fetchersPkg "main/pkg/fetchers" | ||
statePkg "main/pkg/state" | ||
"main/pkg/types" | ||
"sync" | ||
|
||
"github.com/rs/zerolog" | ||
) | ||
|
||
type FetchersStatuses map[constants.FetcherName]bool | ||
|
||
func (s FetchersStatuses) IsAllDone(fetcherNames []constants.FetcherName) bool { | ||
for _, fetcherName := range fetcherNames { | ||
if _, ok := s[fetcherName]; !ok { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
type Controller struct { | ||
Fetchers fetchersPkg.Fetchers | ||
Logger zerolog.Logger | ||
} | ||
|
||
func NewController( | ||
fetchers fetchersPkg.Fetchers, | ||
logger *zerolog.Logger, | ||
) *Controller { | ||
return &Controller{ | ||
Logger: logger.With(). | ||
Str("component", "controller"). | ||
Logger(), | ||
Fetchers: fetchers, | ||
} | ||
} | ||
|
||
func (c *Controller) Fetch(ctx context.Context) ( | ||
*statePkg.State, | ||
[]*types.QueryInfo, | ||
) { | ||
data := statePkg.NewState() | ||
queries := []*types.QueryInfo{} | ||
fetchersStatus := FetchersStatuses{} | ||
|
||
var mutex sync.Mutex | ||
var wg sync.WaitGroup | ||
|
||
processFetcher := func(fetcher fetchersPkg.Fetcher) { | ||
defer wg.Done() | ||
|
||
c.Logger.Trace().Str("name", string(fetcher.Name())).Msg("Processing fetcher...") | ||
|
||
mutex.Lock() | ||
fetcherDependenciesData := data.GetData(fetcher.Dependencies()) | ||
mutex.Unlock() | ||
|
||
fetcherData, fetcherQueries := fetcher.Fetch(ctx, fetcherDependenciesData...) | ||
|
||
mutex.Lock() | ||
data.Set(fetcher.Name(), fetcherData) | ||
queries = append(queries, fetcherQueries...) | ||
fetchersStatus[fetcher.Name()] = true | ||
mutex.Unlock() | ||
|
||
c.Logger.Trace(). | ||
Str("name", string(fetcher.Name())). | ||
Msg("Processed fetcher") | ||
} | ||
|
||
for { | ||
c.Logger.Trace().Msg("Processing all pending fetchers...") | ||
|
||
if fetchersStatus.IsAllDone(c.Fetchers.GetNames()) { | ||
c.Logger.Trace().Msg("All fetchers are fetched.") | ||
break | ||
} | ||
|
||
fetchersToStart := fetchersPkg.Fetchers{} | ||
|
||
for _, fetcher := range c.Fetchers { | ||
if _, ok := fetchersStatus[fetcher.Name()]; ok { | ||
c.Logger.Trace(). | ||
Str("name", string(fetcher.Name())). | ||
Msg("Fetcher is already being processed or is processed, skipping.") | ||
continue | ||
} | ||
|
||
if !fetchersStatus.IsAllDone(fetcher.Dependencies()) { | ||
c.Logger.Trace(). | ||
Str("name", string(fetcher.Name())). | ||
Msg("Fetcher's dependencies are not yet processed, skipping for now.") | ||
continue | ||
} | ||
|
||
fetchersToStart = append(fetchersToStart, fetcher) | ||
} | ||
|
||
c.Logger.Trace(). | ||
Strs("names", fetchersToStart.GetNamesAsString()). | ||
Msg("Starting the following fetchers") | ||
|
||
wg.Add(len(fetchersToStart)) | ||
|
||
for _, fetcher := range fetchersToStart { | ||
go processFetcher(fetcher) | ||
} | ||
|
||
wg.Wait() | ||
} | ||
|
||
return data, queries | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
fetchersPkg "main/pkg/fetchers" | ||
loggerPkg "main/pkg/logger" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestControllerFetcherEnabled(t *testing.T) { | ||
t.Parallel() | ||
|
||
logger := loggerPkg.GetNopLogger() | ||
controller := NewController(fetchersPkg.Fetchers{ | ||
&fetchersPkg.StubFetcher1{}, | ||
&fetchersPkg.StubFetcher2{}, | ||
}, logger) | ||
|
||
data, queryInfos := controller.Fetch(context.Background()) | ||
assert.Empty(t, queryInfos) | ||
assert.Equal(t, 2, data.Length()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.