forked from aerokube/selenoid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.go
54 lines (47 loc) · 1.42 KB
/
metadata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// +build metadata
package main
import (
"encoding/json"
"io/ioutil"
"log"
"path/filepath"
"time"
"github.com/aerokube/selenoid/event"
"github.com/aerokube/selenoid/session"
)
const metadataFileExtension = ".json"
func init() {
mp := &MetadataProcessor{}
event.AddSessionStoppedListener(mp)
// log.Infof("Will save sessions metadata")
}
type MetadataProcessor struct {
}
func (mp *MetadataProcessor) OnSessionStopped(stoppedSession event.StoppedSession) {
if logOutputDir != "" {
meta := session.Metadata{
ID: stoppedSession.SessionId,
Started: stoppedSession.Session.Started,
Finished: time.Now(),
Capabilities: stoppedSession.Session.Caps,
}
data, err := json.MarshalIndent(meta, "", " ")
if err != nil {
log.Printf("[%d] [METADATA] [%s] [Failed to marshal: %v]", stoppedSession.RequestId, stoppedSession.SessionId, err)
return
}
filename := filepath.Join(logOutputDir, stoppedSession.SessionId+metadataFileExtension)
err = ioutil.WriteFile(filename, data, 0644)
if err != nil {
log.Printf("[%d] [METADATA] [%s] [Failed to save to %s: %v]", stoppedSession.RequestId, stoppedSession.SessionId, filename, err)
return
}
log.Printf("[%d] [METADATA] [%s] [%s]", stoppedSession.RequestId, stoppedSession.SessionId, filename)
createdFile := event.CreatedFile{
Event: stoppedSession.Event,
Name: filename,
Type: "metadata",
}
event.FileCreated(createdFile)
}
}