-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
334 additions
and
121 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 |
---|---|---|
@@ -1,18 +1,30 @@ | ||
{ | ||
"Comment": "Batch processing stories for a landing page", | ||
"StartAt": "BatchStoriesFetchParse", | ||
"States": { | ||
"BatchStoriesFetchParse":{ | ||
"Comment": "Fetch and parse all stories", | ||
"Type":"Task", | ||
"Resource": "arn:aws:states:::lambda:invoke", | ||
"Parameters":{ | ||
"FunctionName": "${LANDING_PARSE_METADATA_LAMBDA_ARN}", | ||
"Payload": { | ||
"landingS3Key.$": "$.landingS3Key" | ||
"Comment": "Batch processing stories for a landing page", | ||
"StartAt": "Fetch-All", | ||
"States": { | ||
"Fetch-All": { | ||
"Type": "Map", | ||
"InputPath": "$", | ||
"ItemsPath": "$.stories", | ||
"MaxConcurrency": 1, | ||
"ResultPath": "$.stories", | ||
"Comment": "`Parameters` replaces `InputPath`, see https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-map-state.html ", | ||
"Parameters": { | ||
"story.$": "$$.Map.Item.Value", | ||
"newsSiteAlias.$": "$.newsSiteAlias", | ||
"landingPageTimeStamp.$": "$.landingPageTimeStamp" | ||
}, | ||
"Iterator": { | ||
"StartAt": "Fetch", | ||
"States": { | ||
"Fetch": { | ||
"Type": "Task", | ||
"Resource": "${FETCH_STORY_LAMBDA_ARN}", | ||
"End": true | ||
} | ||
}, | ||
"End":true | ||
} | ||
} | ||
}, | ||
"End": true | ||
} | ||
} | ||
} |
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,117 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/aws/aws-lambda-go/events" | ||
"github.com/aws/aws-lambda-go/lambda" | ||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/sfn" | ||
|
||
"context" | ||
"math/rand" | ||
"time" | ||
|
||
"github.com/rivernews/GoTools" | ||
"github.com/rivernews/media-literacy/pkg/cloud" | ||
"github.com/rivernews/media-literacy/pkg/newssite" | ||
) | ||
|
||
func main() { | ||
rand.Seed(time.Now().UnixNano()) | ||
lambda.Start(HandleRequest) | ||
} | ||
|
||
type LambdaResponse struct { | ||
OK bool `json:"OK:"` | ||
Message string `json:"message:"` | ||
} | ||
|
||
var sfnClient *sfn.Client | ||
|
||
// SQS event | ||
// refer to https://github.com/aws/aws-lambda-go/blob/v1.26.0/events/README_SQS.md | ||
func HandleRequest(ctx context.Context, S3Event events.S3Event) (LambdaResponse, error) { | ||
GoTools.Logger("INFO", "Fetch stories lambda launched ... triggered by metadata.json creation.") | ||
|
||
for _, record := range S3Event.Records { | ||
|
||
GoTools.Logger("INFO", fmt.Sprintf("S3 event ``` %s ```\n ", newssite.AsJson(record))) | ||
|
||
metadataS3KeyTokens := strings.Split(record.S3.Object.URLDecodedKey, "/") | ||
newsSiteAlias := metadataS3KeyTokens[0] | ||
landingPageTimeStamp := metadataS3KeyTokens[len(metadataS3KeyTokens)-2] | ||
|
||
metadataJSONString := cloud.Pull(record.S3.Object.URLDecodedKey) | ||
var metadata newssite.LandingPageMetadata | ||
newssite.FromJson([]byte(metadataJSONString), &metadata) | ||
|
||
GoTools.Logger("INFO", fmt.Sprintf("Test first story: %d:%d", len(metadata.Stories), len(metadata.UntitledStories))) | ||
|
||
// TODO: move below into sfn map lambda | ||
story := metadata.Stories[0] | ||
storyHtmlBodyText := newssite.Fetch(story.URL) | ||
cloud.Archive(cloud.ArchiveArgs{ | ||
BodyText: storyHtmlBodyText, | ||
Key: fmt.Sprintf("%s/stories/%s-%s/story.html", newsSiteAlias, landingPageTimeStamp, story.Name), | ||
}) | ||
|
||
// fire step function, input = {stories: [{}, {}, {}...], newsSiteAlias:"", landingPageTimeStamp:""} | ||
sfnInput := newssite.AsJson(&newssite.StepFunctionInput{ | ||
Stories: metadata.Stories, | ||
NewsSiteAlias: newsSiteAlias, | ||
LandingPageTimeStamp: landingPageTimeStamp, | ||
}) | ||
executionName := strings.ReplaceAll(fmt.Sprintf("%s--%s", landingPageTimeStamp, time.Now().Format(time.RFC3339)), ":", "") | ||
sfnArn := GoTools.GetEnvVarHelper("SFN_ARN") | ||
GoTools.Logger("INFO", fmt.Sprintf("execName: `%s`\nSfnArn: `%s`\nInput:``` %s ```\n", executionName, sfnArn, sfnInput)) | ||
|
||
sfnOutput := cloud.SfnStartExecution( | ||
ctx, | ||
&sfn.StartExecutionInput{ | ||
Input: aws.String(sfnInput), | ||
Name: aws.String(executionName), | ||
StateMachineArn: aws.String(sfnArn), | ||
}, | ||
) | ||
|
||
GoTools.Logger("INFO", fmt.Sprintf("Sfn output ``` %s ```\n", newssite.AsJson(sfnOutput))) | ||
|
||
/* | ||
storyChunk := message.Body | ||
GoTools.Logger("INFO", fmt.Sprintf("Story consumer! story chunk: %s", storyChunk)) | ||
// TODO: fetch and archive for the chunk of storyURLs | ||
storyURLs := strings.Split(storyChunk, " ") | ||
for _, storyURL := range storyURLs { | ||
// TODO: randomized interval | ||
time.Sleep(time.Duration(rand.Intn(5)+5) * time.Second) | ||
storyS3Path := fmt.Sprintf("story/%s", storyURL) | ||
if !cloud.IsDuplicated(storyS3Path) { | ||
GoTools.Logger("INFO", fmt.Sprintf("Archiving story %s", storyURL)) | ||
} else { | ||
GoTools.Logger("DEBUG", fmt.Sprintf("Skip archiving story %s", storyURL)) | ||
} | ||
// _, resMessage, err := GoTools.Fetch(GoTools.FetchOption{ | ||
// Method: "GET", | ||
// URL: "https://ipv4bot.whatismyipaddress.com", | ||
// }) | ||
// if err != nil { | ||
// GoTools.Logger("ERROR", err.Error()) | ||
// } | ||
// GoTools.Logger("INFO", fmt.Sprintf("%s-%s ip: %s", storyChunkId, storyURL, resMessage)) | ||
} | ||
*/ | ||
} | ||
|
||
return LambdaResponse{ | ||
OK: true, | ||
Message: "Story consumer fetch parse ok", | ||
}, nil | ||
} |
Oops, something went wrong.