Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
create runner to run task in application
Browse files Browse the repository at this point in the history
  • Loading branch information
imantung committed Oct 26, 2019
1 parent 4ecbd0a commit 6d4f0db
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 45 deletions.
12 changes: 10 additions & 2 deletions EXPERIMENTAL/typicmd/application/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Run(c *typictx.Context) {
app.Usage = ""
app.Description = c.Description
app.Version = c.Version
app.Action = typictx.ActionCommandFunction(c, c.Application)
app.Action = action(c, c.StartFunc)
app.Before = func(ctx *cli.Context) error {
return typienv.LoadEnvFile()
}
Expand All @@ -26,11 +26,19 @@ func Run(c *typictx.Context) {
Name: cmd.Name,
ShortName: cmd.ShortName,
Usage: cmd.Usage,
Action: cmd.ActionFunc.CommandFunction(c),
Action: action(c, cmd.ActionFunc),
})
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err.Error())
}
}

// ActionCommandFunction to get command function fo action
func action(ctx *typictx.Context, action interface{}) interface{} {
return runner{
Context: ctx,
action: action,
}.Run
}
44 changes: 44 additions & 0 deletions EXPERIMENTAL/typicmd/application/runner.go
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
}
10 changes: 0 additions & 10 deletions EXPERIMENTAL/typictx/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,3 @@ type ActionContext struct {
*Context
Cli *cli.Context
}

// ActionCommandFunction to get command function fo action
func ActionCommandFunction(context *Context, action Action) interface{} {
return func(ctx *cli.Context) error {
return action.Start(&ActionContext{
Cli: ctx,
Context: context,
})
}
}
33 changes: 0 additions & 33 deletions EXPERIMENTAL/typictx/application.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
package typictx

import (
"fmt"
"os"
"os/signal"
"syscall"

log "github.com/sirupsen/logrus"
)

// Application is represent the application
type Application struct {
Config Config
Expand All @@ -18,30 +9,6 @@ type Application struct {
Initiations []interface{}
}

// Start the action
func (a Application) Start(ctx *ActionContext) (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)
for _, initiation := range a.Initiations {
if err = ctx.Invoke(initiation); err != nil {
return
}
}
go func() { // gracefull shutdown
<-gracefulStop
fmt.Println() // NOTE: intentionally print new line after "^C"
fmt.Println("Graceful Shutdown...")
err = ctx.Destruct(ctx.Container)
}()
if a.StartFunc != nil {
err = ctx.Invoke(a.StartFunc)
}
return
}

// Configure return configuration
func (a Application) Configure() Config {
return a.Config
Expand Down

0 comments on commit 6d4f0db

Please sign in to comment.