Exemple #1
0
func NewTestServer() *httptest.Server {
	r := mux.NewRouter()
	db := &job.MockDB{}
	cache := job.NewMemoryJobCache(db, time.Hour)
	SetupApiRoutes(r, cache, db)
	return httptest.NewServer(r)
}
Exemple #2
0
func TestDeleteJob(t *testing.T) {
	setupTest(t)

	db := GetBoltDB(testDbPath)
	cache := job.NewMemoryJobCache(db)
	defer db.Close()

	genericMockJob := job.GetMockJobWithGenericSchedule()
	genericMockJob.Init(cache)
	db.Save(genericMockJob)

	// Make sure its there
	j, err := db.Get(genericMockJob.Id)
	assert.Nil(t, err)
	assert.Equal(t, j.Name, genericMockJob.Name)
	retrievedJob, err := cache.Get(genericMockJob.Id)
	assert.NoError(t, err)
	assert.NotNil(t, retrievedJob)

	// Delete it
	genericMockJob.Delete(cache, db)

	k, err := db.Get(genericMockJob.Id)
	assert.Error(t, err)
	assert.Nil(t, k)
	retrievedJobTwo, err := cache.Get(genericMockJob.Id)
	assert.Error(t, err)
	assert.Nil(t, retrievedJobTwo)

	genericMockJob.Delete(cache, db)
}
Exemple #3
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	app := cli.NewApp()
	app.Name = "Kala"
	app.Usage = "Modern job scheduler"
	app.Version = "0.1"
	app.Commands = []cli.Command{
		{
			Name:  "run",
			Usage: "run kala",
			Flags: []cli.Flag{
				cli.IntFlag{
					Name:  "port, p",
					Value: 8000,
					Usage: "port for Kala to run on",
				},
				cli.StringFlag{
					Name:  "interface, i",
					Value: "",
					Usage: "Interface to listen on, default is all",
				},
				cli.StringFlag{
					Name:  "boltpath",
					Value: "",
					Usage: "Path to the bolt database file, default is current directory.",
				},
			},
			Action: func(c *cli.Context) {
				var parsedPort string
				port := c.Int("port")
				if port != 0 {
					parsedPort = fmt.Sprintf(":%d", port)
				} else {
					parsedPort = ":8000"
				}

				var connectionString string
				if c.String("interface") != "" {
					connectionString = c.String("interface") + parsedPort
				} else {
					connectionString = parsedPort
				}

				db := job.GetBoltDB(c.String("boltpath"))

				// Create cache
				cache := job.NewMemoryJobCache(db, DefaultPersistEvery)
				cache.Start()

				log.Info("Starting server on port %s...", connectionString)
				log.Fatal(api.StartServer(connectionString, cache, db))
			},
		},
	}

	app.Run(os.Args)
}
Exemple #4
0
func TestSaveAndGetJob(t *testing.T) {
	db := GetBoltDB(testDbPath)
	cache := job.NewMemoryJobCache(db, time.Second*60)
	defer db.Close()

	genericMockJob := job.GetMockJobWithGenericSchedule()
	genericMockJob.Init(cache)
	db.Save(genericMockJob)

	j, err := db.Get(genericMockJob.Id)
	assert.Nil(t, err)

	assert.WithinDuration(t, j.NextRunAt, genericMockJob.NextRunAt, 100*time.Microsecond)
	assert.Equal(t, j.Name, genericMockJob.Name)
	assert.Equal(t, j.Id, genericMockJob.Id)
	assert.Equal(t, j.Command, genericMockJob.Command)
	assert.Equal(t, j.Schedule, genericMockJob.Schedule)
	assert.Equal(t, j.Owner, genericMockJob.Owner)
	assert.Equal(t, j.SuccessCount, genericMockJob.SuccessCount)
}
Exemple #5
0
func TestSaveAndGetAllJobs(t *testing.T) {
	setupTest(t)

	db := GetBoltDB(testDbPath)
	cache := job.NewMemoryJobCache(db)
	defer db.Close()

	genericMockJobOne := job.GetMockJobWithGenericSchedule()
	genericMockJobOne.Init(cache)
	err := db.Save(genericMockJobOne)
	assert.NoError(t, err)

	genericMockJobTwo := job.GetMockJobWithGenericSchedule()
	genericMockJobTwo.Init(cache)
	err = db.Save(genericMockJobTwo)
	assert.NoError(t, err)

	jobs, err := db.GetAll()
	assert.Nil(t, err)
	assert.Equal(t, len(jobs), 2)
}
Exemple #6
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	app := cli.NewApp()
	app.Name = "Kala"
	app.Usage = "Modern job scheduler"
	app.Version = "0.1"
	app.Commands = []cli.Command{
		{
			Name:  "run",
			Usage: "run kala",
			Flags: []cli.Flag{
				cli.IntFlag{
					Name:  "port, p",
					Value: 8000,
					Usage: "Port for Kala to run on.",
				},
				cli.BoolFlag{
					Name:  "no-persist, np",
					Usage: "No Persistence Mode - In this mode no data will be saved to the database. Perfect for testing.",
				},
				cli.StringFlag{
					Name:  "interface, i",
					Value: "",
					Usage: "Interface to listen on, default is all.",
				},
				cli.StringFlag{
					Name:  "default-owner, do",
					Value: "",
					Usage: "Default owner. The inputted email will be attached to any job missing an owner",
				},
				cli.StringFlag{
					Name:  "jobDB",
					Value: "boltdb",
					Usage: "Implementation of job database, either 'boltdb' or 'redis'.",
				},
				cli.StringFlag{
					Name:  "boltpath",
					Value: "",
					Usage: "Path to the bolt database file, default is current directory.",
				},
				cli.StringFlag{
					Name:  "jobDBAddress",
					Value: "127.0.0.1:6379",
					Usage: "Network address for the job database, in 'host:port' format.",
				},
				cli.BoolFlag{
					Name:  "verbose, v",
					Usage: "Set for verbose logging.",
				},
			},
			Action: func(c *cli.Context) {
				if c.Bool("v") {
					log.SetLevel(log.DebugLevel)
				}

				var parsedPort string
				port := c.Int("port")
				if port != 0 {
					parsedPort = fmt.Sprintf(":%d", port)
				} else {
					parsedPort = ":8000"
				}

				var connectionString string
				if c.String("interface") != "" {
					connectionString = c.String("interface") + parsedPort
				} else {
					connectionString = parsedPort
				}

				switch c.String("jobDB") {
				case "boltdb":
					db = boltdb.GetBoltDB(c.String("boltpath"))
				case "redis":
					db = redis.New(c.String("jobDBAddress"))
				default:
					log.Fatalf("Unknown Job DB implementation '%s'", c.String("jobDB"))
				}

				if c.Bool("no-persist") {
					db = &job.MockDB{}
				}

				// Create cache
				cache := job.NewMemoryJobCache(db)
				cache.Start(DefaultPersistEvery)

				log.Infof("Starting server on port %s...", connectionString)
				log.Fatal(api.StartServer(connectionString, cache, db, c.String("default-owner")))
			},
		},
	}

	app.Run(os.Args)
}
Exemple #7
0
	"time"

	"github.com/ajvb/kala/job"
	"github.com/rafaeljusto/redigomock"
	"github.com/stretchr/testify/assert"
)

type testJob struct {
	Job   *job.Job
	Bytes []byte
}

var (
	conn     = redigomock.NewConn()
	db       = mockRedisDB()
	cache    = job.NewMemoryJobCache(db)
	testJobs = initTestJobs(3)
)

// mockRedisDB returns a new DB with a mock Redis connection.
func mockRedisDB() DB {
	return DB{
		conn: conn,
	}
}

// testJobs initializes n testJobs
func initTestJobs(n int) []testJob {
	testJobs := []testJob{}

	for i := 0; i < n; i++ {
Exemple #8
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	app := cli.NewApp()
	app.Name = "Kala"
	app.Usage = "Modern job scheduler"
	app.Version = "0.1"
	app.Commands = []cli.Command{
		{
			Name:  "run",
			Usage: "run kala",
			Flags: []cli.Flag{
				cli.IntFlag{
					Name:  "port, p",
					Value: 8000,
					Usage: "Port for Kala to run on.",
				},
				cli.StringFlag{
					Name:  "interface, i",
					Value: "",
					Usage: "Interface to listen on, default is all.",
				},
				cli.StringFlag{
					Name:  "jobDB",
					Value: "boltdb",
					Usage: "Implementation of job database, either 'boltdb' or 'redis'.",
				},
				cli.StringFlag{
					Name:  "boltpath",
					Value: "",
					Usage: "Path to the bolt database file, default is current directory.",
				},
				cli.StringFlag{
					Name:  "jobDBAddress",
					Value: "127.0.0.1:6379",
					Usage: "Network address for the job database, in 'host:port' format.",
				},
			},
			Action: func(c *cli.Context) {
				var parsedPort string
				port := c.Int("port")
				if port != 0 {
					parsedPort = fmt.Sprintf(":%d", port)
				} else {
					parsedPort = ":8000"
				}

				var connectionString string
				if c.String("interface") != "" {
					connectionString = c.String("interface") + parsedPort
				} else {
					connectionString = parsedPort
				}

				switch c.String("jobDB") {
				case "boltdb":
					db = boltdb.GetBoltDB(c.String("boltpath"))
				case "redis":
					db = redis.New(c.String("jobDBAddress"))
				default:
					log.Fatalf("Unknown Job DB implementation '%s'", c.String("jobDB"))
				}

				// Create cache
				cache := job.NewMemoryJobCache(db)
				cache.Start(DefaultPersistEvery)

				log.Info("Starting server on port %s...", connectionString)
				log.Fatal(api.StartServer(connectionString, cache, db))
			},
		},
	}

	app.Run(os.Args)
}