-
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Exits the scraper smarter when all jobs are done
- Loading branch information
Showing
8 changed files
with
201 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package exiter | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type Exiter interface { | ||
SetSeedCount(int) | ||
SetCancelFunc(context.CancelFunc) | ||
IncrSeedCompleted(int) | ||
IncrPlacesFound(int) | ||
IncrPlacesCompleted(int) | ||
Run(context.Context) | ||
} | ||
|
||
type exiter struct { | ||
seedCount int | ||
seedCompleted int | ||
placesFound int | ||
placesCompleted int | ||
|
||
mu *sync.Mutex | ||
cancelFunc context.CancelFunc | ||
} | ||
|
||
func New() Exiter { | ||
return &exiter{ | ||
mu: &sync.Mutex{}, | ||
} | ||
} | ||
|
||
func (e *exiter) SetSeedCount(val int) { | ||
e.mu.Lock() | ||
defer e.mu.Unlock() | ||
|
||
e.seedCount = val | ||
} | ||
|
||
func (e *exiter) SetCancelFunc(fn context.CancelFunc) { | ||
e.mu.Lock() | ||
defer e.mu.Unlock() | ||
|
||
e.cancelFunc = fn | ||
} | ||
|
||
func (e *exiter) IncrSeedCompleted(val int) { | ||
e.mu.Lock() | ||
defer e.mu.Unlock() | ||
|
||
e.seedCompleted += val | ||
} | ||
|
||
func (e *exiter) IncrPlacesFound(val int) { | ||
e.mu.Lock() | ||
defer e.mu.Unlock() | ||
|
||
e.placesFound += val | ||
} | ||
|
||
func (e *exiter) IncrPlacesCompleted(val int) { | ||
e.mu.Lock() | ||
defer e.mu.Unlock() | ||
|
||
e.placesCompleted += val | ||
} | ||
|
||
func (e *exiter) Run(ctx context.Context) { | ||
ticker := time.NewTicker(time.Second * 5) | ||
defer ticker.Stop() | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-ticker.C: | ||
if e.isDone() { | ||
e.cancelFunc() | ||
|
||
return | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (e *exiter) isDone() bool { | ||
e.mu.Lock() | ||
defer e.mu.Unlock() | ||
|
||
fmt.Println("seedCount:", e.seedCount) | ||
fmt.Println("seedCompleted:", e.seedCompleted) | ||
fmt.Println("placesFound:", e.placesFound) | ||
fmt.Println("placesCompleted:", e.placesCompleted) | ||
|
||
if e.seedCompleted != e.seedCount { | ||
return false | ||
} | ||
|
||
if e.placesFound != e.placesCompleted { | ||
return false | ||
} | ||
|
||
return true | ||
} |
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.