-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add cron handler for sfn (#713)
# Description Adding `SetCronHandler` api for sfn, call it like below: ```go sfn.SetCronHandler("@every 1s", func(ctx serverless.CronContext) { ctx.Write(0x22, []byte("message from cron sfn")) ctx.WriteWithTarget(0x23, []byte("message from cron sfn"), "target-id") }) ```
- Loading branch information
Showing
10 changed files
with
188 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package serverless | ||
|
||
import ( | ||
"github.com/yomorun/yomo/core/frame" | ||
"github.com/yomorun/yomo/core/metadata" | ||
) | ||
|
||
// CronContext sfn cron handler context | ||
type CronContext struct { | ||
writer frame.Writer | ||
md metadata.M | ||
mdBytes []byte | ||
} | ||
|
||
// NewCronContext creates a new serverless CronContext | ||
func NewCronContext(writer frame.Writer, md metadata.M) *CronContext { | ||
mdBytes, _ := md.Encode() | ||
|
||
return &CronContext{ | ||
writer: writer, | ||
md: md, | ||
mdBytes: mdBytes, | ||
} | ||
} | ||
|
||
// Write writes the data to next sfn instance. | ||
func (c *CronContext) Write(tag uint32, data []byte) error { | ||
if data == nil { | ||
return nil | ||
} | ||
|
||
dataFrame := &frame.DataFrame{ | ||
Tag: tag, | ||
Metadata: c.mdBytes, | ||
Payload: data, | ||
} | ||
|
||
return c.writer.WriteFrame(dataFrame) | ||
} | ||
|
||
// WriteWithTarget writes the data to next sfn instance with specified target. | ||
func (c *CronContext) WriteWithTarget(tag uint32, data []byte, target string) error { | ||
if data == nil { | ||
return nil | ||
} | ||
|
||
if target == "" { | ||
return c.Write(tag, data) | ||
} | ||
|
||
c.md.Set(metadata.TargetKey, target) | ||
|
||
mdBytes, err := c.md.Encode() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dataFrame := &frame.DataFrame{ | ||
Tag: tag, | ||
Metadata: mdBytes, | ||
Payload: data, | ||
} | ||
|
||
return c.writer.WriteFrame(dataFrame) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters