Skip to content

Commit

Permalink
Fix: wg.wait should wait at start of the function and also addSession.
Browse files Browse the repository at this point in the history
refresh shells every 45mins
  • Loading branch information
xeome committed Jul 24, 2023
1 parent e51a5c0 commit dd9fe75
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
12 changes: 10 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (p *program) Start(s service.Service) error {

func (p *program) run() {
c := utilities.ParseConfig()
rest.StartServer(c.Port, "1.2.0")
rest.StartServer(c.Port, "1.3.0")
}

func (p *program) Stop(s service.Service) error {
Expand All @@ -58,7 +58,15 @@ func main() {
for {
hyperv.Refresh()
logger.Info("Hyper-V module reinitialized.")
time.Sleep(600 * time.Second)
time.Sleep(660 * time.Second)
}
}()

go func() {
for {
utilities.RefreshShellQueue()
logger.Info("Shell queue reinitialized.")
time.Sleep(2700 * time.Second)
}
}()

Expand Down
7 changes: 4 additions & 3 deletions utilities/commandline.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func Init() {
}

func addSession() {
wg.Wait()
if shellQueue.Len() < maxQueueSize {
shell, err := powershell.New(&backend.Local{})
if err != nil {
Expand All @@ -54,6 +55,7 @@ func rotateQueue() {
}

func CommandLine(command string) ([]byte, error) {
wg.Wait()
for shellQueue.Len() < maxQueueSize {
addSession()
}
Expand All @@ -65,7 +67,6 @@ func CommandLine(command string) ([]byte, error) {
errChan := make(chan error)
result := make(chan []byte)

wg.Wait()
<-taskQueue

for {
Expand Down Expand Up @@ -96,7 +97,7 @@ func CommandLine(command string) ([]byte, error) {
select {
case <-time.After(300 * time.Second):
s.busy = false
go refreshShellQueue()
go RefreshShellQueue()
return nil, fmt.Errorf("timeout")
case err := <-errChan:
return nil, err
Expand All @@ -107,7 +108,7 @@ func CommandLine(command string) ([]byte, error) {
return nil, fmt.Errorf("no session available")
}

func refreshShellQueue() {
func RefreshShellQueue() {
wg.Add(1)
defer wg.Done()
for shellQueue.Len() > 0 {
Expand Down

0 comments on commit dd9fe75

Please sign in to comment.