Skip to content

Commit

Permalink
refactor: move worker to a function called execute
Browse files Browse the repository at this point in the history
  • Loading branch information
bjarneo committed Mar 23, 2022
1 parent 9956ce3 commit ed549af
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 64 deletions.
67 changes: 67 additions & 0 deletions core/worker.go
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()
}
65 changes: 1 addition & 64 deletions main.go
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()
}

0 comments on commit ed549af

Please sign in to comment.