cron_gql

package module
v0.0.0-...-1941f42 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2019 License: MIT Imports: 17 Imported by: 0

README

Welcome to Cron-GQL 👋

Go-based cron scheduler wrapped in a GraphQL interface

How to use

1. Download code

Clone the repository:

git clone git@github.com:henry74/cron-gql.git

2. Start the GraphQL server

go run server/server.go

OR

go build -o bin/cron-gql server/server.go
./bin/cron-gql

3. Using the GraphQL API

Navigate to http://localhost:8080 in your browser to explore the API of your GraphQL server in a GraphQL Playground.

The schema that specifies the API operations of your GraphQL server is defined in ./schema.graphql. Below are a number of operations that you can send to the API using the GraphQL Playground.

Feel free to adjust any operation by adding or removing fields. The GraphQL Playground helps you with its auto-completion and query validation features.

Add a new scheduled job
mutation {
  addJob(
    input: {
      cronExp: "0 * * * *"
      rootDir: "/home/henry"
      cmd: "ls"
      args: "-alh"
      tags: ["directory", "hourly"]
    }
  ) {
    jobID
    cronExp
    cmd
    args
    tags
  }
}
See more API operations
List all scheduled jobs
query {
  jobs {
    jobID
    cronExp
    cmd
    args
    tags
    lastScheduledRun
    nextScheduledRun
  }
}

Note: You can filter jobs by a specific jobID or list of tags e.g. using jobs(input: { tags:["hourly"] }) {...}.

Force a scheduled job to run immediately
mutation {
  runJob(JobID: 1) {
    jobID
    cronExp
    cmd
    args
    tags
    lastScheduledRun
    nextScheduledRun
    lastForcedRun
  }
}
Remove a scheduled job
mutation {
  removeJob(JobID: 1) {
    jobID
    cronExp
    cmd
    args
    tags
    lastScheduledRun
    nextScheduledRun
    lastForcedRun
  }
}

4. Changing the GraphQL schema

After you made changes to schema.graphql, you need to update the generated types in ./generated.go and potentially also adjust the resolver implementations in ./resolver.go:

go generate ./... # from root

This updates ./generated.go to incorporate the schema changes in your Go type definitions.

Author

👤 Henry H

Show your support

Give a ⭐️ if this project helped you!


MIT License

Copyright (c) 2019 Henry Hwangbo

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewExecutableSchema

func NewExecutableSchema(cfg Config) graphql.ExecutableSchema

NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface.

Types

type AddJobInput

type AddJobInput struct {
	// Cron expression for scheduling e.g. '0 * * * *'
	CronExp string `json:"cronExp"`
	// Root directory to run the command
	RootDir string `json:"rootDir"`
	// Terminal-based command
	Cmd string `json:"cmd"`
	// Command arguments
	Args []*string `json:"args"`
	// Tags for easier job retrieval
	Tags []*string `json:"tags"`
}

type ComplexityRoot

type ComplexityRoot struct {
	Job struct {
		Args              func(childComplexity int) int
		Cmd               func(childComplexity int) int
		CronExp           func(childComplexity int) int
		JobID             func(childComplexity int) int
		LastForcedRun     func(childComplexity int) int
		LastForcedTime    func(childComplexity int) int
		LastScheduledRun  func(childComplexity int) int
		LastScheduledTime func(childComplexity int) int
		NextScheduledRun  func(childComplexity int) int
		NextScheduledTime func(childComplexity int) int
		RootDir           func(childComplexity int) int
		Tags              func(childComplexity int) int
	}

	Mutation struct {
		AddJob    func(childComplexity int, input AddJobInput) int
		RemoveJob func(childComplexity int, jobID int) int
		RunJob    func(childComplexity int, jobID int) int
	}

	Query struct {
		Jobs func(childComplexity int, input *JobsInput) int
	}
}

type Config

type Config struct {
	Resolvers  ResolverRoot
	Directives DirectiveRoot
	Complexity ComplexityRoot
}

type DirectiveRoot

type DirectiveRoot struct {
}

type Job

type Job struct {
	// Unique ID for the job (generated)
	JobID int `json:"jobID"`
	// Cron expression used for scheduling e.g. '0 * * * *'
	CronExp string `json:"cronExp"`
	// Root directory to run the command
	RootDir string `json:"rootDir"`
	// Terminal-based command
	Cmd string `json:"cmd"`
	// Command arguments
	Args []*string `json:"args"`
	// Tags for easier job retrieval
	Tags []*string `json:"tags"`
	// Last scheduled execution time (human friendly)
	LastScheduledRun *string `json:"lastScheduledRun"`
	// Next scheduled execution time (human friendly)
	NextScheduledRun *string `json:"nextScheduledRun"`
	// Last forced execution time (human friendly)
	LastForcedRun *string `json:"lastForcedRun"`
	// Last scheduled execution time (seconds)
	LastScheduledTime *int `json:"lastScheduledTime"`
	// Next scheduled execution time (seconds)
	NextScheduledTime *int `json:"nextScheduledTime"`
	// Last forced execution time (seconds)
	LastForcedTime *int `json:"lastForcedTime"`
}

type JobsInput

type JobsInput struct {
	// Return job with unique jobID
	JobID *int `json:"jobID"`
	// Return all jobs which match at least one of the tags
	Tags []*string `json:"tags"`
}

type MutationResolver

type MutationResolver interface {
	AddJob(ctx context.Context, input AddJobInput) (*Job, error)
	RemoveJob(ctx context.Context, jobID int) (*Job, error)
	RunJob(ctx context.Context, jobID int) (*Job, error)
}

type QueryResolver

type QueryResolver interface {
	Jobs(ctx context.Context, input *JobsInput) ([]*Job, error)
}

type Resolver

type Resolver struct {
	Cron        *cron.Cron
	RunningJobs map[int]Job
}

Resolver is...

func (*Resolver) Mutation

func (r *Resolver) Mutation() MutationResolver

Mutation is...

func (*Resolver) Query

func (r *Resolver) Query() QueryResolver

Query is...

type ResolverRoot

type ResolverRoot interface {
	Mutation() MutationResolver
	Query() QueryResolver
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL