-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move worker to a function called execute
- Loading branch information
Showing
2 changed files
with
68 additions
and
64 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,67 @@ | ||
package core | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/pterm/pterm" | ||
) | ||
|
||
// Initialize the cli arguments | ||
var args Arguments = NewArgs() | ||
|
||
// Initialize our statistics | ||
var stats TotalStatistics = NewStatistics() | ||
|
||
func Execute() { | ||
// Let us start the timer for how long the workers are running | ||
start := NowUnixMilli() | ||
end := FutureUnixMilli(args.Interval()) | ||
|
||
ParseHeaders(args) | ||
|
||
area, _ := pterm.DefaultArea.Start() | ||
|
||
var wg sync.WaitGroup | ||
|
||
// Start the wait groups | ||
for i := 0; i < args.Concurrent(); i++ { | ||
wg.Add(1) | ||
|
||
Request(args.Hosts(), args, &stats) | ||
} | ||
|
||
// This loop will run until the end is reached | ||
// then it will close the wait groups and break the loop | ||
for { | ||
// Run the for loop once a second | ||
time.Sleep(time.Second * time.Duration(1)) | ||
|
||
area.Update(Logo() + PrintStats(&stats)) | ||
|
||
if NowUnixMilli() < end { | ||
continue | ||
} | ||
|
||
// Close all the concurrent wait groups | ||
for i := 0; i < args.Concurrent(); i++ { | ||
wg.Done() | ||
} | ||
|
||
break | ||
} | ||
|
||
// Block until wait groups has been closed | ||
wg.Wait() | ||
|
||
// End the timer for how long the workers are running | ||
stop := NowUnixMilli() | ||
|
||
stats.SetElapsedTime(stop - start) | ||
|
||
// Final update | ||
area.Update(Logo() + PrintStats(&stats)) | ||
|
||
// stop the area update | ||
area.Stop() | ||
} |
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 |
---|---|---|
@@ -1,77 +1,14 @@ | ||
package main | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/bjarneo/rip/core" | ||
"github.com/pterm/pterm" | ||
) | ||
|
||
// Initialize the cli arguments | ||
var args core.Arguments = core.NewArgs() | ||
|
||
// Initialize our statistics | ||
var stats core.TotalStatistics = core.NewStatistics() | ||
|
||
func workers(concurrent int, interval int, hosts []string) { | ||
// Let us start the timer for how long the workers are running | ||
start := core.NowUnixMilli() | ||
end := core.FutureUnixMilli(interval) | ||
|
||
core.ParseHeaders(args) | ||
|
||
area, _ := pterm.DefaultArea.Start() | ||
|
||
var wg sync.WaitGroup | ||
|
||
// Start the wait groups | ||
for i := 0; i < concurrent; i++ { | ||
wg.Add(1) | ||
|
||
core.Request(hosts, args, &stats) | ||
} | ||
|
||
// This loop will run until the end is reached | ||
// then it will close the wait groups and break the loop | ||
for { | ||
// Run the for loop once a second | ||
time.Sleep(time.Second * time.Duration(1)) | ||
|
||
area.Update(core.Logo() + core.PrintStats(&stats)) | ||
|
||
if core.NowUnixMilli() < end { | ||
continue | ||
} | ||
|
||
// Close all the concurrent wait groups | ||
for i := 0; i < concurrent; i++ { | ||
wg.Done() | ||
} | ||
|
||
break | ||
} | ||
|
||
// Block until wait groups has been closed | ||
wg.Wait() | ||
|
||
// End the timer for how long the workers are running | ||
stop := core.NowUnixMilli() | ||
|
||
stats.SetElapsedTime(stop - start) | ||
|
||
// Final update | ||
area.Update(core.Logo() + core.PrintStats(&stats)) | ||
|
||
// stop the area update | ||
area.Stop() | ||
} | ||
|
||
func main() { | ||
// Initialize the logger | ||
logger := core.NewLogger() | ||
defer logger.Close() | ||
|
||
// Run until the interval is done | ||
workers(args.Concurrent(), args.Interval(), args.Hosts()) | ||
core.Execute() | ||
} |