Skip to content

Commit

Permalink
goworker: Added a function 'Closed()' that will return when the proce…
Browse files Browse the repository at this point in the history
…ss fully closed

It's useful when you want to exactly know when the worker has fully stopped and cleaned.

This can be useful in cases in which you have to block something until the worker is closed for example
if the worker is in goroutines and the main process is killed you could end up with workers not beeing
cleaned so using this function would avoid this.
  • Loading branch information
xescugc committed Jun 11, 2021
1 parent d4866ce commit 6739e0f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## [Unreleased]

### Added

- Closed function to be able to wait for the workers to fully finish
([PR #6](https://github.com/cycloidio/goworker/issues/6))

## [0.1.7] _2021-06-09_

### Fixed
Expand Down
12 changes: 12 additions & 0 deletions goworker.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type WorkerSettings struct {
UseNumber bool
SkipTLSVerify bool
TLSCertPath string

closed chan struct{}
}

func SetSettings(settings WorkerSettings) {
Expand Down Expand Up @@ -88,6 +90,8 @@ func Init() error {
return err
}

workerSettings.closed = make(chan struct{})

initialized = true
}

Expand Down Expand Up @@ -123,11 +127,19 @@ func Close() error {
return err
}
initialized = false
close(workerSettings.closed)
}

return nil
}

// Closed will return a channel that will be
// closed once the full process is done closing
// and cleaning all the workers
func Closed() <-chan struct{} {
return workerSettings.closed
}

// Work starts the goworker process. Check for errors in
// the return value. Work will take over the Go executable
// and will run until a QUIT, INT, or TERM signal is
Expand Down

0 comments on commit 6739e0f

Please sign in to comment.