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

Session 4 : Step Functions #6

Open
wants to merge 1 commit into
base: session-4-v2
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"eslint.workingDirectories": [{ "mode": "auto"}]
}
5 changes: 3 additions & 2 deletions backend/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
},
"rules": {
"prettier/prettier": "error",
"@typescript-eslint/no-var-requires": "off"
},
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/ban-ts-ignore": "off"
},
"plugins": ["prettier"]
}
83 changes: 69 additions & 14 deletions backend/serverless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as AwsConfig from 'serverless/aws';

import ApiGatewayErrors from './resources/apiGatewayErrors';
import DojoServerlessTable from './resources/dynamodb';
// import ApplicationEventBus from './resources/eventBridge';
import ApplicationEventBus from './resources/eventBridge';

const serverlessConfiguration: AwsConfig.Serverless = {
service: 'dojo-serverless-backend',
Expand Down Expand Up @@ -30,7 +30,11 @@ const serverlessConfiguration: AwsConfig.Serverless = {
],
Resource: { 'Fn::GetAtt': ['DojoServerlessTable', 'Arn'] },
},
// { Effect: 'Allow', Action: ['events:PutEvents'], Resource: '*' },
{
Effect: 'Allow',
Action: ['events:PutEvents'],
Resource: '*',
},
],
usagePlan: {
quota: {
Expand Down Expand Up @@ -97,7 +101,11 @@ const serverlessConfiguration: AwsConfig.Serverless = {
// --- WEBSOCKET ---
connectWebsocket: {
handler: 'src/handlers/real-time/connect.main',
events: [{ websocket: { route: '$connect' } }],
events: [
{
websocket: { route: '$connect' },
},
],
},
disconnectWebsocket: {
handler: 'src/handlers/real-time/disconnect.main',
Expand Down Expand Up @@ -131,37 +139,84 @@ const serverlessConfiguration: AwsConfig.Serverless = {
'arn:aws:events:#{AWS::Region}:#{AWS::AccountId}:event-bus/dojo-serverless',
pattern: {
source: ['dojo-serverless'],
'detail-type': ['LAZYNESS_DETECTED'],
'detail-type': ['LAZYNESS_DETECTED'], // Je comprend pas quand cet evenement va etre detecte (par qui il est envoyé)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Andon Thomas ;)

},
},
},
],
},

spreadVirus: {
handler: 'src/handlers/stateMachine/spreadVirus.main',
events: [
{
schedule: {
rate: 'rate(1 minute)',
},
},
],
},

chooseWaitTime: {
handler: 'src/handlers/stateMachine/chooseWaitTime.main',
},
initiateMultipleViruses: {
handler: 'src/handlers/stateMachine/initiateMultipleViruses.main',
},
},
stepFunctions: {
stateMachines: {
wait10SecondsAndDoNothing: {
generateMultipleViruses: {
events: [
{
cloudwatchEvent: {
eventBridge: {
eventBusName:
'arn:aws:events:#{AWS::Region}:#{AWS::AccountId}:event-bus/dojo-serverless',
event: {
source: ['dojo-serverless'],
'detail-type': ['NOTHING_REQUESTED'],
'detail-type': ['VIRUS_CREATION_REQUESTED'],
},
},
},
],
definition: {
StartAt: 'Wait10Sec',
StartAt: 'InitiateMultipleViruses',
States: {
Wait10Sec: {
Type: 'Wait',
Seconds: 10,
Next: 'DoNothing',
InitiateMultipleViruses: {
Type: 'Task',
Resource: {
'Fn::GetAtt': ['initiateMultipleViruses', 'Arn'],
},
Next: 'MapVirusCreation',
},
MapVirusCreation: {
Type: 'Map',
Iterator: {
StartAt: 'ChooseWaitTime',
States: {
ChooseWaitTime: {
Type: 'Task',
Resource: {
'Fn::GetAtt': ['chooseWaitTime', 'Arn'],
},
Next: 'WaitXSeconds',
},
WaitXSeconds: {
Type: 'Wait',
SecondsPath: '$.timeInSeconds',
Next: 'CreateVirus',
},
CreateVirus: {
Type: 'Task',
Resource: {
'Fn::GetAtt': ['createVirus', 'Arn'],
},
End: true,
},
},
},
End: true,
},
DoNothing: { Type: 'Succeed' },
},
},
},
Expand All @@ -171,7 +226,7 @@ const serverlessConfiguration: AwsConfig.Serverless = {
Resources: {
...ApiGatewayErrors,
DojoServerlessTable,
// ApplicationEventBus,
ApplicationEventBus,
},
},
};
Expand Down
3 changes: 3 additions & 0 deletions backend/src/handlers/stateMachine/chooseWaitTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const main = async (): Promise<{ timeInSeconds: number }> => ({
timeInSeconds: Math.ceil(Math.random() * 60),
});
11 changes: 11 additions & 0 deletions backend/src/handlers/stateMachine/initiateMultipleViruses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const maxViruses = 4;
const minViruses = 2;

export const main = async (): Promise<{ virusTaskId: number }[]> => {
const numberOfViruses =
minViruses + Math.floor(Math.random() * (maxViruses - minViruses + 1));
const data = Array.from({ length: numberOfViruses }, (_, i) => ({
virusTaskId: i + 1,
}));
return data;
};
2 changes: 1 addition & 1 deletion backend/src/handlers/stateMachine/requestNothing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import EventBridge from 'aws-sdk/clients/eventbridge';

const eventBridge = new EventBridge();

export const main = async () => {
export const main = async (): Promise<void> => {
await eventBridge
.putEvents({
Entries: [
Expand Down
18 changes: 18 additions & 0 deletions backend/src/handlers/stateMachine/spreadVirus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import EventBridge from 'aws-sdk/clients/eventbridge';

const eventBridge = new EventBridge();

export const main = async (): Promise<void> => {
await eventBridge
.putEvents({
Entries: [
{
Source: 'dojo-serverless',
DetailType: 'VIRUS_CREATION_REQUESTED',
Detail: JSON.stringify({}),
EventBusName: 'dojo-serverless',
},
],
})
.promise();
};