Skip to content

Commit d21c82d

Browse files
committed
add scheduled task to dashboard
improve canonical url for /docs/resources
1 parent 81c6239 commit d21c82d

24 files changed

Lines changed: 434 additions & 87 deletions

File tree

api/handler_metrics_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestHandler_Metrics(t *testing.T) {
3030
h,
3131
try.HasStatusCode(200),
3232
try.HasHeader("Content-Type", "application/json"),
33-
try.HasBody(fmt.Sprintf(`[{"name":"app_start_timestamp","value":%v},{"name":"app_memory_usage_bytes","value":0}]`, int64(app.Monitor.StartTime.Value()))))
33+
try.HasBody(fmt.Sprintf(`[{"name":"app_start_timestamp","value":%v},{"name":"app_memory_usage_bytes","value":0},{"name":"app_job_run_total","value":0}]`, int64(app.Monitor.StartTime.Value()))))
3434
},
3535
},
3636
{

cmd/mokapi/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ func main() {
100100
func createServer(cfg *static.Config) (*server.Server, error) {
101101
events.SetStore(int(cfg.Event.Store["default"].Size), events.NewTraits().WithNamespace("http"))
102102
events.SetStore(int(cfg.Event.Store["default"].Size), events.NewTraits().WithNamespace("kafka"))
103+
events.SetStore(int(cfg.Event.Store["default"].Size), events.NewTraits().WithNamespace("job"))
103104

104105
pool := safe.NewPool(context.Background())
105106
app := runtime.New(cfg)

docs/config.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
"component": "examples",
148148
"hideNavigation": true,
149149
"hideInNavigation": true,
150-
"canonical": "/docs/resources",
150+
"canonical": "https://mokapi.io/docs/resources",
151151
"title": "Explore Mokapi Resources: Tutorials, Examples, and Blog Articles",
152152
"description": "Explore Mokapi's resources including tutorials, examples, and blog articles. Learn to mock APIs, validate schemas, and streamline your development."
153153
},
@@ -157,7 +157,7 @@
157157
"component": "examples",
158158
"hideNavigation": true,
159159
"hideInNavigation": true,
160-
"canonical": "/docs/resources",
160+
"canonical": "https://mokapi.io/docs/resources",
161161
"title": "Explore Mokapi Resources: Tutorials, Examples, and Blog Articles",
162162
"description": "Explore Mokapi's resources including tutorials, examples, and blog articles. Learn to mock APIs, validate schemas, and streamline your development."
163163
},
@@ -179,7 +179,7 @@
179179
"component": "examples",
180180
"hideNavigation": true,
181181
"hideInNavigation": true,
182-
"canonical": "/docs/resources",
182+
"canonical": "https://mokapi.io/docs/resources",
183183
"title": "Explore Mokapi Resources: Tutorials, Examples, and Blog Articles",
184184
"description": "Explore Mokapi's resources including tutorials, examples, and blog articles. Learn to mock APIs, validate schemas, and streamline your development."
185185
},
@@ -193,7 +193,7 @@
193193
"component": "examples",
194194
"hideNavigation": true,
195195
"hideInNavigation": true,
196-
"canonical": "/docs/resources",
196+
"canonical": "https://mokapi.io/docs/resources",
197197
"title": "Explore Mokapi Resources: Tutorials, Examples, and Blog Articles",
198198
"description": "Explore Mokapi's resources including tutorials, examples, and blog articles. Learn to mock APIs, validate schemas, and streamline your development."
199199
},

engine/common/host.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ type Log struct {
116116
Message string `json:"message"`
117117
}
118118

119+
type JobExecution struct {
120+
Schedule string `json:"scheduled"`
121+
122+
Duration int64 `json:"duration"`
123+
Tags map[string]string `json:"tags"`
124+
Logs []Log `json:"logs"`
125+
Error *Error `json:"error"`
126+
}
127+
119128
type Error struct {
120129
Message string `json:"message"`
121130
}
@@ -143,6 +152,10 @@ func (a *Action) AppendLog(level, message string) {
143152
a.Logs = append(a.Logs, Log{Level: level, Message: message})
144153
}
145154

155+
func (e *JobExecution) AppendLog(level, message string) {
156+
e.Logs = append(e.Logs, Log{Level: level, Message: message})
157+
}
158+
146159
type FakerNode interface {
147160
Name() string
148161
Fake(r *generator.Request) (interface{}, error)

engine/engine.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"mokapi/config/static"
88
"mokapi/engine/common"
99
"mokapi/runtime"
10+
"mokapi/runtime/metrics"
1011
"sync"
1112
)
1213

@@ -21,6 +22,8 @@ type Engine struct {
2122
m sync.Mutex
2223
loader ScriptLoader
2324
parallel bool
25+
cfgEvent static.Event
26+
jobCounter *metrics.Counter
2427
}
2528

2629
func New(reader dynamic.Reader, app *runtime.App, config *static.Config, parallel bool) *Engine {
@@ -32,6 +35,8 @@ func New(reader dynamic.Reader, app *runtime.App, config *static.Config, paralle
3235
kafkaClient: NewKafkaClient(app),
3336
parallel: parallel,
3437
loader: NewDefaultScriptLoader(config),
38+
cfgEvent: config.Event,
39+
jobCounter: app.Monitor.JobCounter,
3540
}
3641
}
3742

engine/engine_test.go

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package engine_test
2+
3+
import (
4+
r "github.com/stretchr/testify/require"
5+
"mokapi/engine"
6+
"mokapi/engine/common"
7+
"mokapi/engine/enginetest"
8+
"mokapi/runtime/events"
9+
"mokapi/runtime/metrics"
10+
"mokapi/runtime/runtimetest"
11+
"testing"
12+
"time"
13+
)
14+
15+
func TestEngine_Scheduler(t *testing.T) {
16+
testcases := []struct {
17+
name string
18+
test func(t *testing.T, e *engine.Engine, c *metrics.Counter)
19+
}{
20+
{
21+
name: "run job",
22+
test: func(t *testing.T, e *engine.Engine, c *metrics.Counter) {
23+
err := e.AddScript(newScript("test.js", `
24+
import mokapi from 'mokapi'
25+
export default function() {
26+
mokapi.every('1s', function() { mokapi.sleep(1); });
27+
}
28+
`))
29+
r.NoError(t, err)
30+
31+
time.Sleep(300 * time.Millisecond)
32+
33+
evts := events.GetEvents(events.NewTraits().WithNamespace("job").WithName("test.js"))
34+
r.Len(t, evts, 1)
35+
exec := evts[0].Data.(common.JobExecution)
36+
r.Equal(t, "test.js", exec.Tags["name"])
37+
r.Greater(t, exec.Duration, int64(0))
38+
r.Equal(t, "1s", exec.Schedule)
39+
40+
r.Equal(t, float64(1), c.Value())
41+
},
42+
},
43+
{
44+
name: "run job with name",
45+
test: func(t *testing.T, e *engine.Engine, c *metrics.Counter) {
46+
err := e.AddScript(newScript("test.js", `
47+
import mokapi from 'mokapi'
48+
export default function() {
49+
mokapi.every('1s', function() {}, { tags: { name: 'foo' } });
50+
}
51+
`))
52+
r.NoError(t, err)
53+
54+
time.Sleep(300 * time.Millisecond)
55+
56+
evts := events.GetEvents(events.NewTraits().WithNamespace("job"))
57+
r.Len(t, evts, 1)
58+
exec := evts[0].Data.(common.JobExecution)
59+
r.Equal(t, "foo", exec.Tags["name"])
60+
},
61+
},
62+
{
63+
name: "run job with custom tag",
64+
test: func(t *testing.T, e *engine.Engine, c *metrics.Counter) {
65+
err := e.AddScript(newScript("test.js", `
66+
import mokapi from 'mokapi'
67+
export default function() {
68+
mokapi.every('1s', function() {}, { tags: { foo: 'bar' } });
69+
}
70+
`))
71+
r.NoError(t, err)
72+
73+
time.Sleep(300 * time.Millisecond)
74+
75+
evts := events.GetEvents(events.NewTraits().WithNamespace("job"))
76+
r.Len(t, evts, 1)
77+
exec := evts[0].Data.(common.JobExecution)
78+
r.Equal(t, "bar", exec.Tags["foo"])
79+
},
80+
},
81+
{
82+
name: "run job with logs",
83+
test: func(t *testing.T, e *engine.Engine, c *metrics.Counter) {
84+
err := e.AddScript(newScript("test.js", `
85+
import mokapi from 'mokapi'
86+
export default function() {
87+
mokapi.every('1s', function() {
88+
console.log('a log message');
89+
});
90+
}
91+
`))
92+
r.NoError(t, err)
93+
94+
time.Sleep(300 * time.Millisecond)
95+
96+
evts := events.GetEvents(events.NewTraits().WithNamespace("job"))
97+
r.Len(t, evts, 1)
98+
exec := evts[0].Data.(common.JobExecution)
99+
r.Len(t, exec.Logs, 1)
100+
r.Equal(t, "a log message", exec.Logs[0].Message)
101+
},
102+
},
103+
{
104+
name: "run job script error",
105+
test: func(t *testing.T, e *engine.Engine, c *metrics.Counter) {
106+
err := e.AddScript(newScript("test.js", `
107+
import mokapi from 'mokapi'
108+
export default function() {
109+
mokapi.every('1s', function() {
110+
throw new Error('script error');
111+
});
112+
}
113+
`))
114+
r.NoError(t, err)
115+
116+
time.Sleep(300 * time.Millisecond)
117+
118+
evts := events.GetEvents(events.NewTraits().WithNamespace("job"))
119+
r.Len(t, evts, 1)
120+
exec := evts[0].Data.(common.JobExecution)
121+
r.NotNil(t, exec.Error)
122+
r.Equal(t, "Error: script error at test.js:5:13(3)", exec.Error.Message)
123+
},
124+
},
125+
}
126+
127+
for _, tc := range testcases {
128+
t.Run(tc.name, func(t *testing.T) {
129+
app := runtimetest.NewApp()
130+
app.Monitor.JobCounter = &metrics.Counter{}
131+
e := enginetest.NewEngine(
132+
engine.WithScheduler(engine.NewDefaultScheduler()),
133+
engine.WithApp(app),
134+
)
135+
defer e.Close()
136+
e.Start()
137+
138+
events.SetStore(10, events.NewTraits().WithNamespace("job"))
139+
140+
tc.test(t, e, app.Monitor.JobCounter)
141+
142+
events.Reset()
143+
})
144+
}
145+
}

0 commit comments

Comments
 (0)