grim/convey

da41a2bdcd7c
Parents 178aec6c4f86
Children 2cf80db56edb
Only create the runtime once instead of once for each plan
--- a/runner/cmd.go Thu Dec 23 09:31:26 2021 -0600
+++ b/runner/cmd.go Thu Dec 23 10:03:40 2021 -0600
@@ -1,3 +1,19 @@
+// Convey
+// Copyright 2016-2021 Gary Kramlich <grim@reaperworld.com>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
package runner
import (
@@ -30,37 +46,13 @@
Plans []string `kong:"arg,help='The names of the plans to run',default='default'"`
}
-func (c *RunnerCmd) runPlan(name string, cfg *config.Config, plan runtime.Plan) error {
- configPath, _ := filepath.Split(c.ConfigFile)
- if configPath == "" {
- cwd, err := os.Getwd()
- if err != nil {
- return err
- }
-
- configPath = cwd
- }
-
- env := environment.New(c.Environment...)
- rt := runtime.New(env, configPath, c.ForceSequential, c.KeepWorkspace, c.Timeout)
- rt.Tasks = cfg.Tasks
- rt.MetaPlans = cfg.MetaPlans
- defer rt.Shutdown()
-
- if c.CpuShares != "" {
- rt.CpuShares = c.CpuShares
- }
-
- if c.Memory != "" {
- rt.Memory = c.Memory
- }
-
+func (c *RunnerCmd) runPlan(name string, cfg *config.Config, plan runtime.Plan, rt *runtime.Runtime) error {
configEnv := environment.New(cfg.Environment...)
return plan.Execute(name, configEnv, rt)
}
-func (c *RunnerCmd) runPlans(ctx context.Context, cfg *config.Config) error {
+func (c *RunnerCmd) runPlans(ctx context.Context, cfg *config.Config, rt *runtime.Runtime) error {
errChan := make(chan error, 1)
cancelled := false
@@ -69,7 +61,7 @@
for _, name := range c.Plans {
plan := cfg.Plans[name]
- if err := c.runPlan(name, cfg, plan); err != nil {
+ if err := c.runPlan(name, cfg, plan, rt); err != nil {
errChan <- err
return
}
@@ -109,6 +101,19 @@
return err
}
+ configPath, _ := filepath.Split(c.ConfigFile)
+ if configPath == "" {
+ cwd, err := os.Getwd()
+ if err != nil {
+ return err
+ }
+
+ configPath = cwd
+ }
+
+ rt := c.Runtime(configPath, cfg)
+ defer rt.Shutdown()
+
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
@@ -117,7 +122,7 @@
ctx, cancel := context.WithCancel(context.Background())
go func(ctx context.Context, cfg *config.Config) {
- errChan <- c.runPlans(ctx, cfg)
+ errChan <- c.runPlans(ctx, cfg, rt)
}(ctx, cfg)
for {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/runner/runtime.go Thu Dec 23 10:03:40 2021 -0600
@@ -0,0 +1,43 @@
+// Convey
+// Copyright 2016-2021 Gary Kramlich <grim@reaperworld.com>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+package runner
+
+import (
+ "keep.imfreedom.org/grim/convey/config"
+ "keep.imfreedom.org/grim/convey/environment"
+ "keep.imfreedom.org/grim/convey/runtime"
+)
+
+func (c *RunnerCmd) Runtime(configPath string, cfg *config.Config) *runtime.Runtime {
+ env := environment.New(c.Environment...)
+
+ rt := runtime.New(env, configPath, c.ForceSequential, c.KeepWorkspace, c.Timeout)
+
+ rt.Tasks = cfg.Tasks
+ rt.Plans = cfg.Plans
+ rt.MetaPlans = cfg.MetaPlans
+
+ if c.CpuShares != "" {
+ rt.CpuShares = c.CpuShares
+ }
+
+ if c.Memory != "" {
+ rt.Memory = c.Memory
+ }
+
+ return rt
+}
--- a/runtime/runtime.go Thu Dec 23 09:31:26 2021 -0600
+++ b/runtime/runtime.go Thu Dec 23 10:03:40 2021 -0600
@@ -38,6 +38,7 @@
Timeout time.Duration
MetaPlans map[string]MetaPlan
+ Plans map[string]Plan
Tasks map[string]Task
workspace *fs.Directory
@@ -52,6 +53,7 @@
Timeout: timeout,
MetaPlans: map[string]MetaPlan{},
+ Plans: map[string]Plan{},
Tasks: map[string]Task{},
}