Skip to content

Commit

Permalink
refactoring, adding tests, wip
Browse files Browse the repository at this point in the history
  • Loading branch information
patinthehat committed Aug 26, 2023
1 parent f43e741 commit 35f209a
Show file tree
Hide file tree
Showing 13 changed files with 368 additions and 212 deletions.
75 changes: 75 additions & 0 deletions lib/app/AppFlags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package app

import (
"flag"
"fmt"
"os"
"reflect"

"github.com/stackup-app/stackup/lib/version"
)

type AppFlags struct {
DisplayHelp *bool
DisplayVersion *bool
NoUpdateCheck *bool
ConfigFile *string
app *Application
}

func (af *AppFlags) Get(name string) any {
var result *any = reflect.ValueOf(af).FieldByName(name).Interface().(*any)
if result == nil {
return nil
}
to := reflect.TypeOf(*result)
if to.Kind() == reflect.Bool {
return (*result).(bool)
}
if to.Kind() == reflect.String {
return (*result).(string)
}

return *result
}

func (af *AppFlags) GetString(name string) string {
if result := af.Get(name); result != nil {
return result.(string)
}
return ""
}

func (af *AppFlags) GetBool(name string) bool {
if result := af.Get(name); result != nil {
return result.(bool)
}
return false
}

func (af *AppFlags) Parse() {
flag.Parse()

if af.ConfigFile != nil && *af.ConfigFile != "" {
af.app.ConfigFilename = af.Get("ConfigFile").(string)
}

af.handle()
}

func (af *AppFlags) handle() {
if *af.DisplayHelp {
flag.Usage()
os.Exit(0)
}

if *af.DisplayVersion {
fmt.Println("StackUp version " + version.APP_VERSION)
os.Exit(0)
}

if len(os.Args) > 1 && os.Args[1] == "init" {
af.app.createNewConfigFile()
os.Exit(0)
}
}
92 changes: 59 additions & 33 deletions lib/app/WorkflowPrecondition.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package app

import (
"reflect"

"github.com/stackup-app/stackup/lib/consts"
"github.com/stackup-app/stackup/lib/scripting"
"github.com/stackup-app/stackup/lib/support"
)
Expand All @@ -16,61 +19,84 @@ type WorkflowPrecondition struct {
Workflow *StackupWorkflow
}

func (p *WorkflowPrecondition) Initialize(workflow *StackupWorkflow, engine *scripting.JavaScriptEngine) {
func (p *WorkflowPrecondition) Initialize(workflow *StackupWorkflow) {
p.Workflow = workflow
p.JsEngine = engine
p.JsEngine = workflow.JsEngine
p.Attempts = 0
p.MaxRetries = 99999999999
p.MaxRetries = consts.MAX_TASK_RUNS
}

func (p *WorkflowPrecondition) HandleOnFailure() bool {
result := true

if p.OnFail == "" {
return result
}

if p.JsEngine.IsEvaluatableScriptString(p.OnFail) {
return p.JsEngine.Evaluate(p.OnFail).(bool)
}

task, found := (*p.Workflow).FindTaskById(p.OnFail)

if found {
task.RunSync()
if task, found := p.Workflow.FindTaskById(p.OnFail); found {
return task.RunSync()
}

return result
return true
}

func (wp *WorkflowPrecondition) CanRun() bool {
return wp.Attempts < wp.MaxRetries
}

func (wp *WorkflowPrecondition) Run() bool {
result := true
if wp.Check == "" {
return true
}

result := wp.CanRun()
if !result {
support.FailureMessageWithXMark(wp.Name)
return result
}

wp.Attempts++

if wp.Check != "" {
if wp.Attempts >= wp.MaxRetries {
support.FailureMessageWithXMark(wp.Name)
return false
scriptResult := wp.JsEngine.Evaluate(wp.Check).(any)
resultType, resultValue, _ := wp.JsEngine.ResultType(scriptResult)

if resultType == reflect.String && resultValue != "" {
return wp.JsEngine.Evaluate(resultValue.(string)).(bool)
}

if resultType == reflect.Bool && resultValue == false {
if wp.handleOnFail() {
return result
}
support.FailureMessageWithXMark(wp.Name)
}

wp.Attempts++
// if result.(bool) == false {
// if result = wp.handleOnFail(); result {
// return result
// }
// support.FailureMessageWithXMark(wp.Name)
// }

result = wp.JsEngine.Evaluate(wp.Check).(bool)
return result
}

if !result && len(wp.OnFail) > 0 {
support.FailureMessageWithXMark(wp.Name)
func (wp *WorkflowPrecondition) handleOnFail() bool {
if len(wp.OnFail) == 0 {
return false
}

if wp.HandleOnFailure() {
return wp.Run()
}
support.FailureMessageWithXMark(wp.Name)

result = false
}
if wp.JsEngine.IsEvaluatableScriptString(wp.OnFail) {
return wp.JsEngine.Evaluate(wp.OnFail).(bool)
}

if !result {
support.FailureMessageWithXMark(wp.Name)
return false
}
if task, found := wp.Workflow.FindTaskById(wp.OnFail); found {
return task.RunSync()
}

return result
if wp.HandleOnFailure() {
return wp.Run()
}

return false
}
74 changes: 23 additions & 51 deletions lib/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/robfig/cron/v3"
"github.com/stackup-app/stackup/lib/cache"
"github.com/stackup-app/stackup/lib/consts"
"github.com/stackup-app/stackup/lib/debug"
"github.com/stackup-app/stackup/lib/gateway"
"github.com/stackup-app/stackup/lib/scripting"
"github.com/stackup-app/stackup/lib/support"
Expand All @@ -29,18 +30,11 @@ import (

var App *Application

type AppFlags struct {
DisplayHelp *bool
DisplayVersion *bool
NoUpdateCheck *bool
ConfigFile *string
}

type Application struct {
Workflow *StackupWorkflow
JsEngine *scripting.JavaScriptEngine
cronEngine *cron.Cron
scheduledTaskMap *sync.Map
Workflow *StackupWorkflow
JsEngine *scripting.JavaScriptEngine
cronEngine *cron.Cron
// scheduledTaskMap *sync.Map
ProcessMap *sync.Map
Vars *sync.Map
flags AppFlags
Expand All @@ -53,9 +47,8 @@ type Application struct {

func NewApplication() *Application {
result := &Application{
scheduledTaskMap: &sync.Map{},
ProcessMap: &sync.Map{},
Vars: &sync.Map{},
ProcessMap: &sync.Map{},
Vars: &sync.Map{},
flags: AppFlags{
DisplayHelp: flag.Bool("help", false, "Display help"),
DisplayVersion: flag.Bool("version", false, "Display version"),
Expand All @@ -66,7 +59,7 @@ func NewApplication() *Application {
Gateway: gateway.New(nil),
cronEngine: cron.New(cron.WithChain(cron.SkipIfStillRunning(cron.DiscardLogger))),
}

result.flags.app = result
result.Workflow = CreateWorkflow(result.Gateway, result.ProcessMap)

return result
Expand Down Expand Up @@ -96,18 +89,17 @@ func (a *Application) loadWorkflowFile(filename string, wf *StackupWorkflow) {
}

wf.ConfigureDefaultSettings()

if !wf.Debug {
wf.Debug = os.Getenv("DEBUG") == "true" || os.Getenv("DEBUG") == "1"
}
}

// parse command-line flags, load the workflow file, load .env files,
// initialize the workflow, gateway and js engine
func (a *Application) Initialize() {
utils.EnsureConfigDirExists(utils.GetDefaultConfigurationBasePath("~", "."), consts.APP_CONFIG_PATH_BASE_NAME)

flag.Parse()
if a.flags.ConfigFile != nil && *a.flags.ConfigFile != "" {
a.ConfigFilename = *a.flags.ConfigFile
}

a.flags.Parse()
a.JsEngine = scripting.CreateNewJavascriptEngine(
a.Vars,
a.Gateway,
Expand All @@ -117,29 +109,26 @@ func (a *Application) Initialize() {
},
a.GetApplicationIconPath,
)

a.handleFlagOptions()

a.loadWorkflowFile(a.ConfigFilename, a.Workflow)

if !a.Workflow.Debug {
a.Workflow.Debug = os.Getenv("DEBUG") == "true" || os.Getenv("DEBUG") == "1"
}

a.Analytics = telemetry.New(a.Workflow.Settings.AnonymousStatistics, a.Gateway)
debug.Dbg.SetEnabled(a.Workflow.Debug)
godotenv.Load(a.Workflow.Settings.DotEnvFiles...)

a.Analytics = telemetry.New(a.Workflow.Settings.AnonymousStatistics, a.Gateway)
a.Gateway.Initialize(a.Workflow.Settings, a.JsEngine.AsContract(), nil)
a.Workflow.Cache = cache.New("stackup", a.GetConfigurationPath(), a.Workflow.Settings.Cache.TtlMinutes)
a.Gateway.Cache = a.Workflow.Cache
a.initializeCache()
a.Workflow.Initialize(a.JsEngine, a.GetConfigurationPath())
a.JsEngine.Initialize(a.Vars, os.Environ())
a.Gateway.Debug = a.Workflow.Debug


a.Analytics.EventOnly("app.start")
a.checkForApplicationUpdates(!*a.flags.NoUpdateCheck)
}

func (a *Application) initializeCache() {
a.Workflow.Cache = cache.New("stackup", a.GetConfigurationPath(), a.Workflow.Settings.Cache.TtlMinutes)
a.Gateway.Cache = a.Workflow.Cache
}

func (a *Application) hookSignals() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGSEGV, syscall.SIGQUIT, syscall.SIGHUP)
Expand Down Expand Up @@ -209,7 +198,7 @@ func (a *Application) createScheduledTasks() {
}
})

a.scheduledTaskMap.Store(def.TaskId(), &def)
// a.scheduledTaskMap.Store(def.TaskId(), &def)
}

a.cronEngine.Start()
Expand Down Expand Up @@ -325,23 +314,6 @@ func (a *Application) checkForApplicationUpdates(canCheck bool) {
}
}

func (a *Application) handleFlagOptions() {
if *a.flags.DisplayHelp {
flag.Usage()
os.Exit(0)
}

if *a.flags.DisplayVersion {
fmt.Println("StackUp version " + version.APP_VERSION)
os.Exit(0)
}

if len(os.Args) > 1 && os.Args[1] == "init" {
a.createNewConfigFile()
os.Exit(0)
}
}

func (a *Application) GetConfigurationPath() string {
pathname, _ := utils.EnsureConfigDirExists(
utils.GetDefaultConfigurationBasePath("~", "."),
Expand Down
Loading

0 comments on commit 35f209a

Please sign in to comment.