Skip to content

Commit

Permalink
Beta for goschedule-v0.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonjoo2010 committed Jun 29, 2020
1 parent 10ac899 commit 47168eb
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 70 deletions.
2 changes: 1 addition & 1 deletion app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (app *App) UpdateStorageConfig(s types.Storage) error {
if err != nil {
return err
}
f, err := os.OpenFile(app.confFile, os.O_CREATE|os.O_WRONLY, 0644)
f, err := os.OpenFile(app.confFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions console.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func init() {
flag.StringVar(&configFile, "c", "config.yml", "Configuration file")
flag.IntVar(&listenPort, "p", 8000, "Serving port")
flag.BoolVar(&debug, "v", false, "Debug mode")
flag.Parse()
}

func main() {
Expand Down
4 changes: 4 additions & 0 deletions controller/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func saveHandler(c *gin.Context) {
if isTest {
resp["test"] = true
s := utils.CreateStore(storageConfig)
if s == nil {
resp.Err(1, "Fail to create store, please check the configuration or connection")
return
}
list, err := s.GetSchedulers()
if err != nil {
resp.Err(1, "Failed: "+err.Error())
Expand Down
13 changes: 8 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ module github.com/jasonjoo2010/goschedule-console
go 1.14

require (
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
github.com/gin-gonic/gin v1.6.3
github.com/jasonjoo2010/goschedule v0.0.0-20200529095745-5accacc243c2
github.com/jasonjoo2010/goschedule/store/redis v0.0.0-20200529095745-5accacc243c2
github.com/jasonjoo2010/goschedule v0.1.2
github.com/jasonjoo2010/goschedule/store/redis v0.1.2
github.com/jasonjoo2010/goschedule/store/zookeeper v0.1.2
github.com/jasonjoo2010/goschedule/store/database v0.1.2
github.com/jasonjoo2010/goschedule/store/etcdv2 v0.1.2
github.com/jasonjoo2010/goschedule/store/etcdv3 v0.1.2
github.com/robfig/cron v1.2.0
github.com/sirupsen/logrus v1.6.0
github.com/stretchr/testify v1.5.1
golang.org/x/sys v0.0.0-20200511232937-7e40ca221e25 // indirect
github.com/stretchr/testify v1.5.1 // test
gopkg.in/yaml.v3 v3.0.0-20200506231410-2ff61e1afc86
github.com/go-sql-driver/mysql v1.5.0
)
158 changes: 122 additions & 36 deletions go.sum

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion static/css/settings.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,16 @@
font-size: 12px;
margin: 0;
padding: 0;
}
}

.container .tip {
line-height: 20px;
font-size: 13px;
margin-left: 80px;
margin-bottom: 20px;
color: #666;
}

.container .tip b {
color: #0377ca;
}
27 changes: 18 additions & 9 deletions templates/config/modify.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,20 @@ <h2>Storage Settings</h2>
<li>
<label for="selType">Type: </label>
<div class="unit">
<input id="typeMemory" type="radio" name="type" value="memory" /><label for="typeMemory">Memory</label>
<input id="typeRedis" type="radio" name="type" value="redis" /><label for="typeRedis">Redis</label>
<input id="typeZookeeper" type="radio" name="type" value="zookeeper" /><label for="typeZookeeper">ZooKeeper</label>
<input id="typeMysql" type="radio" name="type" value="Mysql" /><label for="typeMysql">Mysql</label>
<input id="typeDatabase" type="radio" name="type" value="database" /><label for="typeDatabase">Database</label>
<input id="typeEtcdv2" type="radio" name="type" value="etcdv2" /><label for="typeEtcdv2">Etcdv2</label>
<input id="typeEtcdv3" type="radio" name="type" value="etcdv3" /><label for="typeEtcdv3">Etcdv3</label>
</div>
<li class="tip">
<b>For Database</b>, the pattern should be <br />
<b>drivername://ip:port/db?info_table=schedule_info</b><br />
And if you take `schedule_info` by default you can write it in short if you use `mysql`:<br />
<b>mysql://ip:port/db</b><br />
Currently only MySQL driver is introduced(in console).<br />
<b>For others</b>, it should be in pattern <b>ip:port</b>
</li>
</li>
<li>
<label for="txtAddress">Adress: </label>
Expand All @@ -44,7 +53,7 @@ <h2>Storage Settings</h2>
<button tpye="button" id="btnTest" class="btn">Test</button>
<button type="button" id="btnSubmit">Save</button>
</li>
<li class="tip"></li>
<li class="tip" id="msg"></li>
</div>
</form>
</div>
Expand All @@ -56,27 +65,27 @@ <h2>Storage Settings</h2>
success: function(data) {
$(".form button").attr("disabled", false);
if (data.errno == 0) {
$(".tip").removeClass("fail");
$("#msg").removeClass("fail");
if (data.test) {
$(".tip").html("Test passed");
$("#msg").html("Test passed");
} else {
document.location.href = '/strategy/index';
}
} else {
$(".tip").html(data.errdesc);
$(".tip").addClass("fail");
$("#msg").html(data.errdesc);
$("#msg").addClass("fail");
}
}
});
$("#btnTest").click(function() {
$(".tip").html("Testing ...");
$("#msg").html("Testing ...");
$(".form button").attr("disabled", true);
$("#frmSettings input[name='test']").val("1");
$("#frmSettings").submit();
return false;
});
$("#btnSubmit").click(function() {
$(".tip").html("Submiting ...");
$("#msg").html("Submiting ...");
$(".form button").attr("disabled", true);
$("#frmSettings input[name='test']").val("0");
$("#frmSettings").submit();
Expand Down
2 changes: 1 addition & 1 deletion types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/sirupsen/logrus"
)

var STORAGE_TYPES [4]string = [...]string{"memory", "redis", "zookeeper", "jdbc"}
var STORAGE_TYPES [6]string = [...]string{"memory", "redis", "zookeeper", "database", "etcdv2", "etcdv3"}

// DashboardConfig respresents structure of config.xml
type DashboardConfig struct {
Expand Down
88 changes: 77 additions & 11 deletions utils/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,100 @@
package utils

import (
"database/sql"
"fmt"
"net/url"
"strconv"
"strings"

_ "github.com/go-sql-driver/mysql"
"github.com/jasonjoo2010/goschedule-console/types"
"github.com/jasonjoo2010/goschedule/store"
"github.com/jasonjoo2010/goschedule/store/database"
"github.com/jasonjoo2010/goschedule/store/etcdv2"
"github.com/jasonjoo2010/goschedule/store/etcdv3"
"github.com/jasonjoo2010/goschedule/store/memory"
"github.com/jasonjoo2010/goschedule/store/redis"
"github.com/jasonjoo2010/goschedule/store/zookeeper"
"github.com/sirupsen/logrus"
)

func parseAddr(address string, default_port int) (string, int, error) {
port := default_port
addr := address
pos := strings.IndexRune(addr, ':')
if pos > 0 {
addr = address[:pos]
var err error
port, err = strconv.Atoi(address[pos+1:])
if err != nil {
logrus.Error("Wrong address format: ", address)
return "", 0, err
}
}
return addr, port, nil
}

func CreateStore(s types.Storage) store.Store {
switch s.Type {
case "memory":
return memory.New()
case "redis":
port := 6379
addr := s.Address
pos := strings.IndexRune(addr, ':')
if pos > 0 {
addr = s.Address[:pos]
var err error
port, err = strconv.Atoi(s.Address[pos+1:])
if err != nil {
logrus.Error("Wrong address format: ", s.Address)
return nil
}
addr, port, err := parseAddr(s.Address, 6379)
if err != nil {
return nil
}
return redis.New(s.Namespace, addr, port)
case "zookeeper":
addr, port, err := parseAddr(s.Address, 2181)
if err != nil {
return nil
}
return zookeeper.New(s.Namespace, addr, port)
case "database":
u, err := url.Parse(s.Address)
if err != nil {
logrus.Warn("incorrect address: ", s.Address)
return nil
}
info_table := u.Query().Get("info_table")
if info_table == "" {
info_table = "schedule_info"
}
db, err := sql.Open(u.Scheme,
fmt.Sprintf("%s:%s@tcp(%s:%s)/%s",
s.Username,
s.Password,
u.Hostname(),
u.Port(),
strings.Trim(u.Path, "/")),
)
if err != nil {
logrus.Warn("Create db instance failed: ", err.Error())
return nil
}
return database.New(
s.Namespace,
db,
database.WithInfoTable(info_table),
)
case "etcdv2":
addr, port, err := parseAddr(s.Address, 2379)
if err != nil {
return nil
}
return etcdv2.New(s.Namespace, []string{fmt.Sprintf("%s:%d", addr, port)})
case "etcdv3":
addr, port, err := parseAddr(s.Address, 2379)
if err != nil {
return nil
}
s, err := etcdv3.New(s.Namespace, []string{fmt.Sprintf("%s:%d", addr, port)})
if err != nil {
logrus.Warn("Create etcdv3 instance failed: ", err.Error())
return nil
}
return s
default:
logrus.Warn("Unknow type of storage: ", s.Type)
return nil
Expand Down
10 changes: 5 additions & 5 deletions utils/task_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
// 2 -> id segment, wait for '}', no reset
// 3 -> wait for ',' to reset
type state struct {
result []*definition.TaskItem
result []definition.TaskItem
step int
id strings.Builder
param strings.Builder
Expand All @@ -31,7 +31,7 @@ func (s *state) Reset() {
}
return
}
item := &definition.TaskItem{Id: s.id.String()}
item := definition.TaskItem{Id: s.id.String()}
if s.param.Len() > 0 {
item.Parameter = s.param.String()
s.param.Reset()
Expand Down Expand Up @@ -61,13 +61,13 @@ func (s *state) Process(b byte) {
}
}

func ParseTaskItems(str string) []*definition.TaskItem {
func ParseTaskItems(str string) []definition.TaskItem {
if str == "" {
return make([]*definition.TaskItem, 0)
return make([]definition.TaskItem, 0)
}
// use state machine
state := &state{
result: make([]*definition.TaskItem, 0, 2),
result: make([]definition.TaskItem, 0, 2),
}
for i := 0; i < len(str); i++ {
state.Process(str[i])
Expand Down
2 changes: 1 addition & 1 deletion utils/task_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func TestParseTaskItems(t *testing.T) {
var list []*definition.TaskItem
var list []definition.TaskItem

list = ParseTaskItems("")
assert.Equal(t, 0, len(list))
Expand Down

0 comments on commit 47168eb

Please sign in to comment.