Skip to content

Commit

Permalink
v1.3.0
Browse files Browse the repository at this point in the history
Features:

- Added support to allow Day Of Month and Day Of Week parts of the cron expressions to both be enforced
- Additional parsing of the cron schedules, better logging when incorrect expression provided
  • Loading branch information
SteveGoldthorpe committed Mar 27, 2019
1 parent 64027b0 commit 96125b9
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 49 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,12 @@
# CHANGELOG

## 1.3.0 (March 27th 2019)

Features:

- Added support to allow Day Of Month and Day Of Week parts of expression to both be enforced
- Additional parsing of the cron schedules, better logging when incorrect expression provided

## 1.2.1 (March 12th 2019)

Defect Fix:
Expand Down
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -4,8 +4,6 @@ The utility provides a quick and easy way to schedule the running of Hornbill AP

## Installation

### Windows

* Download the OS-specific ZIP archive containing the executables, configuration file and license;
* Extract the ZIP archive into a folder you would like the application to run from e.g. 'C:\hornbill_scheduler\'.

Expand All @@ -21,6 +19,7 @@ Example JSON File:
{
"Enabled":true,
"CronSchedule":"0 1 23 * * 1-6",
"DayOfMonthANDDayOfWeek": false,
"ScheduleFrom":"2016-11-12T00:00:00.000Z",
"ScheduleTo":"2017-01-01T00:00:00.000Z",
"Service":"apps/com.hornbill.servicemanager/Incidents",
Expand Down Expand Up @@ -49,6 +48,7 @@ Example JSON File:
{
"Enabled":true,
"CronSchedule":"* * * * * 1-5",
"DayOfMonthANDDayOfWeek": false,
"ScheduleFrom":"2016-11-12T00:00:00.000Z",
"ScheduleTo":"2017-01-01T00:00:00.000Z",
"Service":"apps/com.hornbill.servicemanager/Requests",
Expand Down Expand Up @@ -77,6 +77,8 @@ Example JSON File:
* "Schedule" - A JSON array, where each object within this array contains the configuration for one scheduled and repeatable task:
* "Enabled" - set to true to enable the schedule item
* "CronSchedule" - A Cron compatible schedule expression to schedule the API call by
* "DayOfMonthANDDayOfWeek" - Boolean true or false. When true, the content of BOTH Day of Week and Day of Month parts of the expression will be enforced, rather than the crontab standard of either.
* NOTE: the following special characters are supported in these parts of the expression when this is set to true `* , - ?`
* "ScheduleFrom" - An RFC3339 formatted time string, to specify the date & time to start running any instances of the particular schedule entry. This can contain an empty string to allow you to not specify a date/time to start the schedule from
* "ScheduleTo" - An RFC3339 formatted time string, to specify the date & time to stop running any more instances of the particular schedule entry. This can contain an empty string, should you wish the schedule to run indefinitely
* "Service" - The Hornbill Service that contains the API you wish to running
Expand Down
69 changes: 32 additions & 37 deletions conf.json
@@ -1,40 +1,35 @@
{
"APIKey": "API Key goes here",
"InstanceID": "Instance name goes here",
"Schedule": [
{
"Enabled":true,
"CronSchedule":"0 0 9 * * 1-6",
"ScheduleFrom":"",
"ScheduleTo":"",
"Service":"",
"API":"",
"APIParams":{
"0":
{
"Type":"Content",
"Parameter":"paramName",
"Content":"parameter content"
},
"1":
{
"Type":"Open",
"Parameter":"complexParamName",
"Content":""
},
"2":
{
"Type":"Content",
"Parameter":"paramName",
"Content":"parameter content"
},
"3":
{
"Type":"Close",
"Parameter":"complexParamName",
"Content":""
}
"APIKey": "apikeygoeshere",
"InstanceID": "instanceIDgoeshere",
"Schedule": [{
"Enabled": true,
"CronSchedule": "0 1 23 * * 1-6",
"DayOfMonthANDDayOfWeek":false,
"ScheduleFrom": "",
"ScheduleTo": "",
"Service": "",
"API": "",
"APIParams": {
"0": {
"Type": "Content",
"Parameter": "paramName",
"Content": "parameter content"
},
"1": {
"Type": "Open",
"Parameter": "complexParamName",
"Content": ""
},
"2": {
"Type": "Content",
"Parameter": "paramName",
"Content": "parameter content"
},
"3": {
"Type": "Close",
"Parameter": "complexParamName",
"Content": ""
}
}
]
}
}]
}
62 changes: 62 additions & 0 deletions golang-crosscompile-build.bash
@@ -0,0 +1,62 @@
#!/bin/bash
# Orignal https://gist.github.com/jmervine/7d3f455e923cf2ac3c9e
# usage: ./golang-crosscompile-build.bash

#Get current working directory
currentdir=`pwd`

#Clear Sceeen
printf "\033c"

# Get Version out of target then replace . with _
versiond=$(go run *.go -version)
version=${versiond//./_}
#Remove White Space
version=${version// /}
versiond=${versiond// /}
#platforms="darwin/386 darwin/amd64 freebsd/386 freebsd/amd64 freebsd/arm linux/386 linux/amd64 linux/arm windows/386 windows/amd64"
platforms="windows/386 windows/amd64 linux/386 linux/amd64 linux/arm darwin/386 darwin/amd64"
printf " ---- Building API Scheduler $versiond ---- \n"
printf "\n"
for platform in ${platforms}
do
split=(${platform//\// })
goos=${split[0]}
os=${split[0]}
goarch=${split[1]}
arch=${split[1]}
output=goAPIScheduler
package=goAPIScheduler
# add exe to windows output
[[ "windows" == "$goos" ]] && output="$output.exe"
[[ "windows" == "$goos" ]] && os="win"
[[ "386" == "$goarch" ]] && arch="x86"
[[ "amd64" == "$goarch" ]] && arch="x64"

printf "Platform: $goos - $goarch \n"

destination="builds/$goos/$goarch/$output"

printf "Go Build\n"
GOOS=$goos GOARCH=$goarch go build -o $destination
# $target

printf "Copy Source Files\n"
#Copy Source to Build Dir
cp *.md "builds/$goos/$goarch/"
cp conf*.json "builds/$goos/$goarch/"

printf "Build Zip \n"
cd "builds/$goos/$goarch/"
if [ $os == "darwin" ]; then
os="osx"
fi
zip -r "${package}_${os}_${arch}_v${version}.zip" $output *.md conf*.json > /dev/null
cp "${package}_${os}_${arch}_v${version}.zip" "../../../${package}_${os}_${arch}_v${version}.zip"
cd $currentdir
printf "\n"
done
printf "Clean Up \n"
rm -rf "builds/"
printf "Build Complete \n"
printf "\n"
97 changes: 96 additions & 1 deletion main.go
Expand Up @@ -25,6 +25,15 @@ func main() {
//Time for CLI output & Log File Content
currTime := time.Now().Format(cliTimeLayout)

//Build map of days of week
dowMap["SUN"] = 0
dowMap["MON"] = 1
dowMap["TUE"] = 2
dowMap["WED"] = 3
dowMap["THU"] = 4
dowMap["FRI"] = 5
dowMap["SAT"] = 6

flag.StringVar(&configFileName, "file", "conf.json", "Name of the configuration file to load")
flag.BoolVar(&configDebug, "debug", false, "Set to true to run scheduler in debug mode, where API call request and reponse XML payload will be written to the log")
flag.BoolVar(&configDryRun, "dryrun", false, "Set to true to run scheduler in dryrun mode. No API calls will be made")
Expand Down Expand Up @@ -58,7 +67,12 @@ func main() {
logger(logEntryType, "Cron Schedule: "+scheduleEntryUnique.CronSchedule, true)
logger(logEntryType, "Service: "+scheduleEntryUnique.Service, true)
logger(logEntryType, "API: "+scheduleEntryUnique.API, true)
c.AddFunc(scheduleEntryUnique.CronSchedule, func() { apiRequest(scheduleEntryUnique) })
_, err := cron.Parse(scheduleEntryUnique.CronSchedule)
if err != nil {
logger(4, "Could not Parse CronSchedule value ["+scheduleEntryUnique.CronSchedule+"] : "+fmt.Sprintf("%v", err), true)
} else {
c.AddFunc(scheduleEntryUnique.CronSchedule, func() { apiRequest(scheduleEntryUnique) })
}
}
}

Expand All @@ -75,7 +89,88 @@ func main() {

}

func checkSchedule(schedule string) bool {
boolDOW := false
boolDOM := false

schedule = strings.Trim(schedule, " ")
schedArr := strings.Split(schedule, " ")
DOW := schedArr[len(schedArr)-1]
DOM := schedArr[len(schedArr)-3]

//Get current time and dates
currDOWNum := strconv.Itoa(int(time.Now().Weekday()))
currDOWStr := strings.ToUpper(time.Now().Format("Mon"))
currDOMNum := strings.TrimLeft(time.Now().Format("02"), "0")

//Check Day Of Week
//--Check if DOW is wildcard or ignore
if DOW == "*" || DOW == "?" {
boolDOW = true
} else {

//--Check if DOW is list or single value
listArr := strings.Split(DOW, ",")
for _, v := range listArr {
if v == currDOWNum || strings.ToUpper(v) == currDOWStr {
boolDOW = true
}
}

//Check if DOW is range
listArr = strings.Split(DOW, "-")
if len(listArr) == 2 {
//Are values int (0-6) or string (SUN-SAT)
min, err1 := strconv.Atoi(listArr[0])
max, err2 := strconv.Atoi(listArr[1])
if err1 != nil && err2 != nil {
//Couldn't convert, assume range is string SUN to SAT type
min = dowMap[strings.ToUpper(listArr[0])]
max = dowMap[strings.ToUpper(listArr[1])]
}
intCurrDOWNum, _ := strconv.Atoi(currDOWNum)
if intCurrDOWNum >= min && intCurrDOWNum <= max {
boolDOW = true
}
}
}

//Check Day Of Month
//--Check if DOM is wildcard or ignore
if DOM == "*" || DOM == "?" {
boolDOM = true
} else {

//--Check if DOM is list or single value
listArr := strings.Split(DOM, ",")
for _, v := range listArr {
if v == currDOMNum {
boolDOM = true
}
}

//Check if DOM is range
listArr = strings.Split(DOM, "-")
if len(listArr) == 2 {
min, _ := strconv.Atoi(listArr[0])
max, _ := strconv.Atoi(listArr[1])
intCurrDOMNum, _ := strconv.Atoi(currDOMNum)
if intCurrDOMNum >= min && intCurrDOMNum <= max {
boolDOM = true
}
}
}

return (boolDOW && boolDOM)
}

func apiRequest(scheduleEntry apiSchedStruct) {
if scheduleEntry.DayOfMonthANDDayOfWeek {
ok := checkSchedule(scheduleEntry.CronSchedule)
if !ok {
return
}
}
espXmlmc := apiLib.NewXmlmcInstance(apiCallConfig.InstanceID)
espXmlmc.SetAPIKey(apiCallConfig.APIKey)

Expand Down
19 changes: 10 additions & 9 deletions main.structs.go
@@ -1,7 +1,7 @@
package main

const (
version = "1.2.1"
version = "1.3.0"
timeLayout = "2006-01-02T15:04:05.000Z"
cliTimeLayout = "2006/01/02 15:04:05"
)
Expand All @@ -13,8 +13,8 @@ var (
configVersion bool
logEntryType = 3
apiCallConfig apiCallStruct
boolConfLoaded bool
timeNow string
dowMap = make(map[string]int)
)

//----- Config Data Structs
Expand All @@ -26,13 +26,14 @@ type apiCallStruct struct {
}

type apiSchedStruct struct {
Enabled bool
CronSchedule string
ScheduleFrom string
ScheduleTo string
Service string
API string
APIParams map[string]apiParamStruct
Enabled bool
CronSchedule string
DayOfMonthANDDayOfWeek bool
ScheduleFrom string
ScheduleTo string
Service string
API string
APIParams map[string]apiParamStruct
}

type apiParamStruct struct {
Expand Down

0 comments on commit 96125b9

Please sign in to comment.