This repository has been archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create runner to run task in application
- Loading branch information
Showing
4 changed files
with
54 additions
and
45 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
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,44 @@ | ||
package application | ||
|
||
import ( | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typictx" | ||
"github.com/urfave/cli" | ||
"go.uber.org/dig" | ||
) | ||
|
||
type runner struct { | ||
*typictx.Context | ||
action interface{} | ||
} | ||
|
||
func (a runner) Run(ctx *cli.Context) (err error) { | ||
log.Info("------------- Application Start -------------") | ||
defer log.Info("-------------- Application End --------------") | ||
gracefulStop := make(chan os.Signal) | ||
signal.Notify(gracefulStop, syscall.SIGTERM) | ||
signal.Notify(gracefulStop, syscall.SIGINT) | ||
di := dig.New() | ||
if err = a.Construct(di); err != nil { | ||
return | ||
} | ||
// TODO: create prepare function | ||
for _, initiation := range a.Initiations { | ||
if err = di.Invoke(initiation); err != nil { | ||
return | ||
} | ||
} | ||
go func() { // gracefull shutdown | ||
<-gracefulStop | ||
log.Println("\nGraceful Shutdown...") | ||
err = a.Destruct(di) | ||
}() | ||
if err = di.Invoke(a.action); err != nil { | ||
return | ||
} | ||
return | ||
} |
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