forked from mcuadros/ofelia
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
middlewares: save middleware, save output and execution to a file
- Loading branch information
Showing
8 changed files
with
186 additions
and
11 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
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
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,102 @@ | ||
package middlewares | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/mcuadros/ofelia/core" | ||
) | ||
|
||
// SaveConfig configuration for the Save middleware | ||
type SaveConfig struct { | ||
SaveFolder string `gcfg:"save-folder"` | ||
SaveOnlyOnError bool `gcfg:"save-only-on-error"` | ||
} | ||
|
||
// NewSave returns a Save middleware if the given configuration is not empty | ||
func NewSave(c *SaveConfig) core.Middleware { | ||
var m core.Middleware | ||
if !IsEmpty(c) { | ||
m = &Save{*c} | ||
} | ||
|
||
return m | ||
} | ||
|
||
// Save the save middleware saves to disk a dump of the stdout and stderr after | ||
// every execution of the process | ||
type Save struct { | ||
SaveConfig | ||
} | ||
|
||
// ContinueOnStop return allways true, we want always report the final status | ||
func (m *Save) ContinueOnStop() bool { | ||
return true | ||
} | ||
|
||
// Run save the result of the execution to disk | ||
func (m *Save) Run(ctx *core.Context) error { | ||
err := ctx.Next() | ||
ctx.Stop(err) | ||
|
||
if ctx.Execution.Failed || !m.SaveOnlyOnError { | ||
err := m.saveToDisk(ctx) | ||
if err != nil { | ||
ctx.Logger.Error("Save error: %q", err) | ||
} | ||
} | ||
|
||
return err | ||
} | ||
|
||
func (m *Save) saveToDisk(ctx *core.Context) error { | ||
root := filepath.Join(m.SaveFolder, fmt.Sprintf( | ||
"%s_%s", | ||
ctx.Execution.Date.Format("20060102_150405"), ctx.Job.GetName(), | ||
)) | ||
|
||
e := ctx.Execution | ||
err := m.saveReaderToDisk(e.ErrorStream, fmt.Sprintf("%s.stderr.log", root)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = m.saveReaderToDisk(e.OutputStream, fmt.Sprintf("%s.stdout.log", root)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = m.saveContextToDisk(ctx, fmt.Sprintf("%s.json", root)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *Save) saveContextToDisk(ctx *core.Context, filename string) error { | ||
js, _ := json.MarshalIndent(map[string]interface{}{ | ||
"Job": ctx.Job, | ||
"Execution": ctx.Execution, | ||
}, "", " ") | ||
|
||
return m.saveReaderToDisk(bytes.NewBuffer(js), filename) | ||
} | ||
|
||
func (m *Save) saveReaderToDisk(r io.Reader, filename string) error { | ||
f, err := os.Create(filename) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer f.Close() | ||
if _, err := io.Copy(f, r); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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,60 @@ | ||
package middlewares | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
. "gopkg.in/check.v1" | ||
) | ||
|
||
type SuiteSave struct { | ||
BaseSuite | ||
} | ||
|
||
var _ = Suite(&SuiteSave{}) | ||
|
||
func (s *SuiteSave) TestNewSlackEmpty(c *C) { | ||
c.Assert(NewSave(&SaveConfig{}), IsNil) | ||
} | ||
|
||
func (s *SuiteSave) TestRunSuccess(c *C) { | ||
dir, err := ioutil.TempDir("/tmp", "save") | ||
c.Assert(err, IsNil) | ||
|
||
s.ctx.Start() | ||
s.ctx.Stop(nil) | ||
|
||
s.job.Name = "foo" | ||
s.ctx.Execution.Date = time.Time{} | ||
|
||
m := NewSave(&SaveConfig{SaveFolder: dir}) | ||
c.Assert(m.Run(s.ctx), IsNil) | ||
|
||
_, err = os.Stat(filepath.Join(dir, "00010101_000000_foo.json")) | ||
c.Assert(err, IsNil) | ||
|
||
_, err = os.Stat(filepath.Join(dir, "00010101_000000_foo.stdout.log")) | ||
c.Assert(err, IsNil) | ||
|
||
_, err = os.Stat(filepath.Join(dir, "00010101_000000_foo.stderr.log")) | ||
c.Assert(err, IsNil) | ||
} | ||
|
||
func (s *SuiteSave) TestRunSuccessOnError(c *C) { | ||
dir, err := ioutil.TempDir("/tmp", "save") | ||
c.Assert(err, IsNil) | ||
|
||
s.ctx.Start() | ||
s.ctx.Stop(nil) | ||
|
||
s.job.Name = "foo" | ||
s.ctx.Execution.Date = time.Time{} | ||
|
||
m := NewSave(&SaveConfig{SaveFolder: dir, SaveOnlyOnError: true}) | ||
c.Assert(m.Run(s.ctx), IsNil) | ||
|
||
_, err = os.Stat(filepath.Join(dir, "00010101_000000_foo.json")) | ||
c.Assert(err, Not(IsNil)) | ||
} |
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