Skip to content

Commit

Permalink
add sql source provider and vendor updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
nirnanaaa committed Aug 22, 2018
1 parent 46c0738 commit 4133f3b
Show file tree
Hide file tree
Showing 302 changed files with 268,418 additions and 9,160 deletions.
24 changes: 21 additions & 3 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions conf/config.sample.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[meta]
debug = true
logging-enabled = true
log-level = "debug"

[metrics]
enable-metrics = false
[metrics.slack]
enabled = false
webhook-url = ""
message-format = ""
reporting-interval = "24h0m0s"
[metrics.console]
enabled = false
reporting-interval = "24h0m0s"
[metrics.cloudwatch]
enabled = false
webook-url = ""
message-format = ""
reporting-interval = "24h0m0s"
namespace = ""
reset-counters-after-report = false
[metrics.cloudwatch.static-dimensions]

[scheduler]
enabled = true
tick-duration = "1s"
log-task-detection = false
num-workers = 10
[scheduler.executor-http]
enabled = true
debug-response = false
log-http-status = false
use-jwt-signing = true
jwt-issuer = "issuer"
jwt-expires = "10m0s"
jwt-subject = "0"
jwt-secret = ""
[scheduler.provider-crontab]
enabled = false
source = "/etc/asparagus/crontab"
[scheduler.executor-local]
enabled = true
enable-output = true
[scheduler.provider-etcd]
enabled = false
registry-url = ["http://127.0.0.1:4001"]
discovery-enabled = false
discovery-domain = "example.com"
connect-ca-cert = ""
source-folder = "/cron/Jobs"
json-single-depth = true
[scheduler.provider-sql]
enabled = false
uri = "/tmp/test.db"
driver = "sqlite3"
# uri = "root:password@tcp(127.0.0.1:3306)/database?parseTime=true"
# driver = "mysql"
13 changes: 13 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@
[scheduler.executor-local]
enabled = false
enable-output = false
[scheduler.provider-sql]
# Required: Enable the SQL provider.
enabled = true

# for sqlite3
driver = "sqlite3"
uri = "/tmp/test.db"

# for mysql
driver = "mysql"

# make sure to suffix ?parseTime=true otherwise asparagus will complain about string to time conversion.
uri = "root:pass@tcp(127.0.0.1:3306)/database?parseTime=true"
# The ETCD source provider
[scheduler.provider-etcd]

Expand Down
40 changes: 40 additions & 0 deletions docs/source/05-sql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# SQL Source Provider

Asparagus may be used alongside a database service. Supported databases are:

- sqlite3
- mysql

Asparagus will create a database table for you, providing you specify a correct database.

## Installation

This module is included in the core of asparagus. As such, you can just go to your
`asparagus.conf` and enable it.

```toml
[scheduler.provider-sql]
# Required: Enable the SQL provider.
enabled = true

# for sqlite3
driver = "sqlite3"
uri = "/tmp/test.db"

# for mysql
driver = "mysql"

# make sure to suffix ?parseTime=true otherwise asparagus will complain about string to time conversion.
uri = "root:pass@tcp(127.0.0.1:3306)/database?parseTime=true"
```

## Job Configuration

All jobs must be stored under an unique name inside the database.


### Example

```bash
insert into cronjobs values ('test', 'test', '0', '* * * * *', NOW(), '', 'http', '{\"URL\": \"https://httpbin.org/get\", \"Method\": \"GET\"}', '', '10', '0');
```
17 changes: 15 additions & 2 deletions reflection/map_to_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,22 @@ func MapToStruct(t interface{}, values map[string]interface{}) error {
f.SetString(value.(string))
case bool:
f.SetBool(value.(string) == "true")
case []string:
input := reflect.ValueOf(value.([]string))
case []interface{}:
input := reflect.ValueOf(value)
f.Set(input)
case []string:
switch value.(type) {
case []string:
input := reflect.ValueOf(value.([]string))
f.Set(input)
case []interface{}:
target := []string{}
for _, v := range value.([]interface{}) {
target = append(target, v.(string))
}
input := reflect.ValueOf(target)
f.Set(input)
}
default:
return fmt.Errorf("i don't know how to parse type %T", v)
}
Expand Down
3 changes: 3 additions & 0 deletions scheduler/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/nirnanaaa/asparagus/scheduler/provider/etcd"
"github.com/nirnanaaa/asparagus/scheduler/provider/http"
"github.com/nirnanaaa/asparagus/scheduler/provider/local"
"github.com/nirnanaaa/asparagus/scheduler/provider/sql"
"github.com/nirnanaaa/asparagus/toml"
)

Expand All @@ -24,6 +25,7 @@ type Config struct {
LocalExecutor local.Config `toml:"executor-local"`
CrontabSource crontab.Config `toml:"provider-crontab"`
ETCDSource etcd.Config `toml:"provider-etcd"`
SQLSource sql.Config `toml:"provider-sql"`
NumWorkers int `toml:"num-workers"`
}

Expand All @@ -36,6 +38,7 @@ func NewConfig() *Config {
LocalExecutor: local.NewConfig(),
CrontabSource: crontab.NewConfig(),
ETCDSource: etcd.NewConfig(),
SQLSource: sql.NewConfig(),
NumWorkers: 10,
}
}
Expand Down
5 changes: 5 additions & 0 deletions scheduler/provider/crontab/source_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ func NewSourceProvider(config Config) *SourceProvider {
}
}

// String returns the Providers identity
func (p SourceProvider) String() string {
return fmt.Sprintf("Crontab Provider, enabled: %t", p.Config.Enabled)
}

// OnTaskUpdate runs when a task gets updated
func (p SourceProvider) OnTaskUpdate(fn func(*provider.Task) error) {
go func() {
Expand Down
6 changes: 6 additions & 0 deletions scheduler/provider/etcd/source_provider.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package etcd

import (
"fmt"
"time"

"github.com/nirnanaaa/asparagus/scheduler/provider"
Expand All @@ -25,6 +26,11 @@ func NewSourceProvider(config Config) *SourceProvider {
}
}

// String returns the Providers identity
func (p SourceProvider) String() string {
return fmt.Sprintf("ETCD Provider, enabled: %t", p.Config.Enabled)
}

// TaskError will be called when an error occured
func (p SourceProvider) TaskError(t *provider.Task, err error) error {
t.Running = false
Expand Down
2 changes: 1 addition & 1 deletion scheduler/provider/http/execution_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestHTTPNoMap(t *testing.T) {
t.Fatal("should throw an error, because a map was required.")
}
if err.Error() != "unknown input type on executor: string" {
t.Fatal("Error message isn't correct: %s", err.Error())
t.Fatalf("Error message isn't correct: %s", err.Error())
}
}

Expand Down
6 changes: 3 additions & 3 deletions scheduler/provider/local/execution_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestLocal_RequestNonExistingPath(t *testing.T) {
t.Fatal("no error was thrown, but executor was disabled")
}
if err.Error() != "exec: \"txbawquasiudasojdi\": executable file not found in $PATH" {
t.Fatal("unexpected error message. Want \"exec: \"txbawquasiudasojdi\": executable file not found in $PATH\". Got \"%s\"", err.Error())
t.Fatalf("unexpected error message. Want \"exec: \"txbawquasiudasojdi\": executable file not found in $PATH\". Got \"%s\"", err.Error())
}
}

Expand All @@ -82,7 +82,7 @@ func TestLocal_WrongInputType(t *testing.T) {
t.Fatal("no error was thrown, but wrong input type used")
}
if err.Error() != "unknown input type on executor: []string" {
t.Fatal("unexpected error thrown. expected unknown input type on executor: []string got %s", err.Error())
t.Fatalf("unexpected error thrown. expected unknown input type on executor: []string got %s", err.Error())
}
}

Expand All @@ -94,6 +94,6 @@ func TestLocal_WrongInputTypeString(t *testing.T) {
t.Fatal("no error was thrown, but wrong input type used")
}
if err.Error() != "command has no parts" {
t.Fatal("unexpected error thrown. expected command has no parts got %s", err.Error())
t.Fatalf("unexpected error thrown. expected command has no parts got %s", err.Error())
}
}
28 changes: 28 additions & 0 deletions scheduler/provider/sql/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package sql

const (
// DefaultEnabled defines a state for default config
DefaultEnabled = false

// DefaultDatabaseURI defines the default contact point towards the mysql server instance
DefaultDatabaseURI = "/tmp/asparagus.db"

// DefaultDriver defines a default driver for the sql package
DefaultDriver = "sqlite3"
)

// Config represents a configuration for a HTTP service.
type Config struct {
Enabled bool `toml:"enabled"`
DatabaseURI string `toml:"uri"`
DatabaseDriver string `toml:"driver"`
}

// NewConfig returns a new Config with default settings.
func NewConfig() Config {
return Config{
Enabled: DefaultEnabled,
DatabaseURI: DefaultDatabaseURI,
DatabaseDriver: DefaultDriver,
}
}
29 changes: 29 additions & 0 deletions scheduler/provider/sql/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package sql_test

import (
"testing"

"github.com/BurntSushi/toml"
"github.com/nirnanaaa/asparagus/scheduler/provider/sql"
)

func TestConfig_Parse(t *testing.T) {
// Parse configuration.
c := sql.NewConfig()
if _, err := toml.Decode(`
enabled = false
uri = "/tmp/test.db"
driver = "test"
`, &c); err != nil {
t.Fatal(err)
}

// Validate configuration.
if c.Enabled {
t.Fatalf("unexpected enabled: %v", c.Enabled)
} else if c.DatabaseURI != "/tmp/test.db" {
t.Fatalf("unexpected database uri: %v", c.DatabaseURI)
} else if c.DatabaseDriver != "test" {
t.Fatalf("unexpected database driver: %v", c.DatabaseDriver)
}
}
Loading

0 comments on commit 4133f3b

Please sign in to comment.