Skip to content

Use as Separate Service

huddedar34 edited this page Jun 25, 2023 · 2 revisions

Client Onboarding

For any schedule creation, you need to register the app associated with it first. Additionally, when creating Cron Schedules, you need to register the Athena app (default app, which can be changed from the configuration). Use the following API to create an app:

curl --location 'http://localhost:8080/myss/app' \
--header 'Content-Type: application/json' \
--data '{
    "appId": "test",
    "partitions": 5,
    "active": true
}'

Request Body The request body should be a JSON object with the following fields:

  • appId (string): The ID of the app to create.
  • partitions (integer): The number of partitions for the app.
  • active (boolean): Specifies if the app is active or not.

Response The API will respond with the created app's details in JSON format.

{
    "status": {
        "statusCode": 201,
        "statusMessage": "Success",
        "statusType": "Success",
        "totalCount": 1
    },
    "data": {
        "appId": "test",
        "partitions": 5,
        "active": true
    }
}

Schedule Creation

Create One Time Schedule

curl --location 'http://localhost:8080/myss/schedule' \
--header 'Content-Type: application/json' \
--data '{
    "appId": "test",
    "payload": "{}",
    "scheduleTime": 1686676947,
    "callback": {
        "type": "http",
        "details": {
            "url": "http://127.0.0.1:8080/myss/healthcheck",
            "method": "GET",
            "headers": {
                "Content-Type": "application/json",
                "Accept": "application/json"
            }
        }
    }
}'

The request body should be a JSON object with the following fields:

  • appId (string): The ID of the app for which the schedule is created.
  • payload (string): The payload or data associated with the schedule. It can be an empty string or any valid JSON data.
  • scheduleTime (integer): The timestamp representing the schedule time.
  • callback (object): The callback configuration for the schedule.
    • type (string): The type of callback. In this example, it is set to "http".
    • details (object): The details specific to the callback type. For the "http" callback, it includes the URL, HTTP method, and headers.

The API will respond with the created schedule's details in JSON format.

Example response body:

{
    "status": {
        "statusCode": 201,
        "statusMessage": "Success",
        "statusType": "Success",
        "totalCount": 1
    },
    "data": {
        "scheduleId": "2358e5b6-09f3-11ee-a704-acde48001122",
        "appId": "test",
        "payload": "{}",
        "scheduleTime": 1686676947,
        "callback": {
            "type": "http",
            "details": {
                "url": "http://127.0.0.1:8080/myss/healthcheck",
                "method": "GET",
                "headers": {
                    "Content-Type": "application/json",
                    "Accept": "application/json"
                }
            }
        }
    }
}

Create Cron Schedule

curl --location 'http://localhost:8080/myss/schedule' \
--header 'Content-Type: application/json' \
--data '{
    "appId": "test",
    "payload": "{}",
    "cronExpression": "*/5 * * * *",
    "callback": {
        "type": "http",
        "details": {
            "url": "http://127.0.0.1:8080/myss/healthcheck",
            "method": "GET",
            "headers": {
                "Content-Type": "application/json",
                "Accept": "application/json"
            }
        }
    }
}'

The request body should be a JSON object with the following fields:

  • appId (string): The ID of the app for which the cron schedule is created.
  • payload (string): The payload or data associated with the cron schedule. It can be an empty string or any valid JSON data.
  • cronExpression (string): The cron expression representing the schedule pattern.
  • callback (object): The callback configuration for the schedule.
    • type (string): The type of callback. In this example, it is set to "http".
    • details (object): The details specific to the callback type. For the "http" callback, it includes the URL, HTTP method, and headers.

Supported Cron Expression

Go Scheduler supports standard UNIX cron expression of the following pattern.

* * * * *
| | | | |
| | | | └─ Day of the week (0 - 6, where 0 represents Sunday)
| | | └── Month (1 - 12)
| | └──── Day of the month (1 - 31)
| └────── Hour (0 - 23)
└──────── Minute (0 - 59)

The Cron Expression consists of five fields, each separated by a space:

Field Required Allowed Values Symbols
Minutes Yes 0-59 * ,/-
Hours Yes 0-23 * ,/-
Day of month Yes 1-31 * ,/-
Month Yes 1-12 or JAN-DEC * ,-
Day of week Yes 0-6 or SUN-SAT * ,-

The supported symbols and their meanings are as follows:

  • *: Matches all possible values.
  • ,: Specifies multiple values.
  • /: Specifies stepping values.
  • -: Specifies a range of values.

Examples

  • 0 0 * * *: Executes a task at midnight every day.
  • 0 12 * * MON-FRI: Executes a task at 12 PM (noon) from Monday to Friday.
  • */15 * * * *: Executes a task every 15 minutes.
  • 0 8,12,18 * * *: Executes a task at 8 AM, 12 PM (noon), and 6 PM every day.

The API will respond with the created cron schedule's details in JSON format.

Example response body:

{
    "status": {
        "statusCode": 201,
        "statusMessage": "Success",
        "statusType": "Success",
        "totalCount": 1
    },
    "data": {
        "schedule": {
            "scheduleId": "35b5e19e-0aa4-11ee-8563-acde48001122",
            "payload": "{}",
            "appId": "test",
            "partitionId": 3,
            "callback": {
                "type": "http",
                "details": {
                    "url": "http://127.0.0.1:8080/myss/healthcheck",
                    "method": "GET",
                    "headers": {
                        "Content-Type": "application/json",
                        "Accept": "application/json"
                    }
                }
            },
            "cronExpression": "*/5 * * * *",
            "status": "SCHEDULED"
        }
    }
}

Check Schedule Status

curl --location 'http://localhost:8080/myss/schedule/a675115c-0a0e-11ee-bebb-acde48001122' \
--header 'Accept: application/json'

{scheduleId} is the actual UUID of the schedule you want to retrieve.

Example response body:

{
    "status": {
        "statusCode": 200,
        "statusMessage": "Success",
        "statusType": "Success",
        "totalCount": 1
    },
    "data": {
        "schedule": {
            "scheduleId": "a675115c-0a0e-11ee-bebb-acde48001122",
            "payload": "{}",
            "appId": "test",
            "scheduleTime": 1686676947,
            "partitionId": 4,
            "scheduleGroup": 1686676920,
            "callback": {
                "type": "http",
                "details": {
                    "url": "http://127.0.0.1:8080/myss/healthcheck",
                    "method": "GET",
                    "headers": {
                        "Accept": "application/json",
                        "Content-Type": "application/json"
                    }
                }
            },
            "status": "SUCCESS"
        }
    }
}

Customizable Callback

GoScheduler's Callback feature is designed to be extensible, enabling users to define and utilize their own custom callbacks. The Callback structure serves as the foundation for creating customized callbacks tailored to specific requirements. By implementing the methods defined in the Callback interface, users can extend the functionality of the GoScheduler with their own callback implementations.

The Callback structure in GoScheduler consists of the following methods:

type Callback interface {
	GetType() string
	GetDetails() (string, error)
	Marshal(map[string]interface{}) error
	Invoke(wrapper ScheduleWrapper) error
	Validate() error 
	json.Unmarshaler 
}

The methods in the Callback interface provide the necessary functionality for integrating customized callbacks into the GoScheduler.

  • GetType() string: Returns the type or identifier of the callback.
  • GetDetails() (string, error): Retrieves the details of the callback, typically in JSON format.
  • Marshal(map[string]interface{}) error: Deserializes the callback details from a map or JSON representation.
  • Invoke(wrapper ScheduleWrapper) error: Executes the logic associated with the callback when it is triggered.
  • Validate() error: Performs validation checks on the callback's details to ensure they are properly configured.

Sample Example

type FooBarCallback struct {
	Type    string `json:"type"`
        Details string `json:"details"`
}

func (f *FooBarCallback) GetType() string {
	return f.Type
}

func (f *FooBarCallback) GetDetails() (string, error) {
	detailsJSON, err := json.Marshal(details)
	if err != nil {
		return "", err
	}
	return string(detailsJSON), nil
}

func (f *FooBarCallback) Marshal(m map[string]interface{}) error {
	// Sample implementation: Unmarshal the provided map to set the fields of FooBarCallback
	typeAlias := struct {
		Type string `json:"type"`
	}{}
	if err := mapstructure.Decode(m, &typeAlias); err != nil {
		return err
	}
	f.Type = typeAlias.Type
	return nil
}

func (f *FooBarCallback) Invoke(wrapper ScheduleWrapper) error {
    // Invoke the FooBar callback logic
    err := invokeFooBarCallback()

    if err != nil {
        wrapper.Schedule.Status = Failure
        wrapper.Schedule.ErrorMessage = err.Error()
    } else {
        wrapper.Schedule.Status = Success
    }

    // Push the updated schedule to the Aggregate Channel
    AggregateChannel <- wrapper

    return nil
}

func (f *FooBarCallback) Validate() error {
	// Sample implementation: Perform validation logic for FooBar callback
	if f.Type == "" {
		return errors.New("FooBar callback type is required")
	}

	// Additional validation logic specific to FooBar callback

	return nil
}

In this example, FooBarCallback is a custom callback implementation that defines the specific behavior for the Callback interface methods. Customize the fields and logic according to your requirements.

Usage in startup file

To incorporate the custom callback implementation in the GoScheduler, you need to make changes in the startup file (main function) as follows:

func main() {
	// Load all the configs
	config := conf.InitConfig()

	// Create the custom callback factories map
	customCallbackFactories := map[string]store.Factory{
		"foo-bar": func() store.Callback { return &foobar.FooBarCallback{} },
		// Add more custom callback factories as needed
	}


	// Create the scheduler with the custom callback factories
	s := scheduler.New(config, customCallbackFactories)

	// Wait for the termination signal
	s.Supervisor.WaitForTermination()
}