Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to add timezone and extra user payload to calendar gateway #164

Merged
merged 1 commit into from
Feb 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions gateways/core/calendar/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package calendar

import (
"time"

"github.com/ghodss/yaml"
"github.com/rs/zerolog"
)
Expand All @@ -43,6 +45,23 @@ type calSchedule struct {
// the combination of these rules and dates combine to form a set of date times.
// NOTE: functionality currently only supports EXDATEs, but in the future could be expanded.
Recurrence []string `json:"recurrence,omitempty"`

// Timezone in which to run the schedule
// +optional
Timezone string `json:"timezone,omitempty"`

// UserPayload will be sent to sensor as extra data once the event is triggered
// +optional
UserPayload string `json:"userPayload,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason this is a string and not []byte ?

Copy link
Contributor Author

@VaibhavPage VaibhavPage Feb 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most probably user will put JSON string as UserPayload because it is the easiest way to parse it in workflow

}

// calResponse is the event payload that is sent as response to sensor
type calResponse struct {
// EventTime is time at which event occurred
EventTime time.Time `json:"eventTime"`

// UserPayload if any
UserPayload string `json:"userPayload"`
}

func parseEventSource(eventSource string) (*calSchedule, error) {
Expand Down
19 changes: 15 additions & 4 deletions gateways/core/calendar/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ limitations under the License.
package calendar

import (
"encoding/json"
"fmt"
"time"

"github.com/argoproj/argo-events/common"
"github.com/argoproj/argo-events/gateways"
cronlib "github.com/robfig/cron"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// Next is a function to compute the next event time from a given time
Expand Down Expand Up @@ -98,17 +98,28 @@ func (ese *CalendarEventSourceExecutor) listenEvents(cal *calSchedule, eventSour
}
return nextT
}

lastT := time.Now()
if cal.Timezone != "" {
location, err := time.LoadLocation(cal.Timezone)
if err != nil {
errorCh <- err
return
}
lastT = lastT.In(location)
}

for {
t := next(lastT)
timer := time.After(time.Until(t))
ese.Log.Info().Str("event-source-name", eventSource.Name).Str("time", t.String()).Msg("expected next calendar event")
select {
case tx := <-timer:
lastT = tx
event := metav1.Time{Time: t}
payload, err := event.Marshal()
response := &calResponse{
EventTime: tx,
UserPayload: cal.UserPayload,
}
payload, err := json.Marshal(response)
if err != nil {
errorCh <- err
return
Expand Down