-
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
10 changed files
with
230 additions
and
66 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,2 +1,169 @@ | ||
# video8 | ||
Code samples for video #8 on my Youtube channel | ||
# WakeUp | ||
|
||
Hotel wake up service. Serverless. | ||
|
||
Customer asks to wake them up at xx:yy and this what this service does. | ||
|
||
Technologies used: Google Cloud Scheduler, Cloud SQL, Cloud Functions. | ||
|
||
# Service Diagram | ||
|
||
 | ||
|
||
# Deploy | ||
|
||
1. Setup Google Cloud Project and Cloud PostgreSQL. | ||
2. Put your gcloud serviceaccount.json (with editor permissions for Cloud Scheduler and Cloud SQL) file into `cmd` directory. | ||
3. Update environment variables in `cmd/env.sh` file: | ||
|
||
``` | ||
export GOOGLE_APPLICATION_CREDENTIALS=./serviceaccount.json | ||
# Local postgres | ||
# export WAKEUP_PG_PROTO=tcp | ||
# export WAKEUP_PG_ADDR=localhost:5432 | ||
# export WAKEUP_PG_DB=wakeup | ||
# export WAKEUP_PG_USER=postgres | ||
# export WAKEUP_PG_PASSWORD=postgres | ||
# Google Cloud Postgres | ||
export WAKEUP_PG_PROTO=unix | ||
export WAKEUP_PG_ADDR=/cloudsql/wakeup-278716:europe-west1:wakeup-postgres/.s.PGSQL.5432 | ||
export WAKEUP_PG_DB=wakeup | ||
export WAKEUP_PG_USER=wakeup | ||
export WAKEUP_PG_PASSWORD=wakeup | ||
export WAKEUP_GC_PROJECT=wakeup-278716 | ||
export WAKEUP_GC_PROJECT_LOCATION=europe-west1 | ||
export WAKEUP_CALL_ROOM_ENDPOINT=https://$WAKEUP_GC_PROJECT_LOCATION-$WAKEUP_GC_PROJECT.cloudfunctions.net/CallRoom | ||
export WAKEUP_SCHEDULER_LOCATION=projects/$WAKEUP_GC_PROJECT/locations/$WAKEUP_GC_PROJECT_LOCATION | ||
export WAKEUP_SCHEDULER_TIMEZONE=Europe/Moscow | ||
export WAKEUP_SCHEDULER_MAX_RETRY_COUNT=2 | ||
export WAKEUP_SCHEDULER_RETRY_PERIOD=1m | ||
export WAKEUP_CALL_ENDPOINT=https://www.google.com | ||
``` | ||
|
||
4. Deploy Google cloud functions with `cmd/deploy.all.sh` script as follows: | ||
|
||
``` | ||
macbook:cmd jt$ ./deploy.all.sh | ||
Deploying function (may take a while - up to 2 minutes)...done. | ||
availableMemoryMb: 128 | ||
entryPoint: CallRoom | ||
environmentVariables: | ||
WAKEUP_CALL_ENDPOINT: https://www.google.com | ||
WAKEUP_CALL_ROOM_ENDPOINT: https://europe-west1-wakeup-278716.cloudfunctions.net/CallRoom | ||
WAKEUP_GC_PROJECT: wakeup-278716 | ||
WAKEUP_GC_PROJECT_LOCATION: europe-west1 | ||
WAKEUP_PG_ADDR: /cloudsql/wakeup-278716:europe-west1:wakeup-postgres/.s.PGSQL.5432 | ||
WAKEUP_PG_DB: wakeup | ||
WAKEUP_PG_PASSWORD: xxxxxx | ||
WAKEUP_PG_PROTO: unix | ||
WAKEUP_PG_USER: wakeup | ||
WAKEUP_SCHEDULER_LOCATION: projects/wakeup-278716/locations/europe-west1 | ||
WAKEUP_SCHEDULER_TIMEZONE: Europe/Moscow | ||
httpsTrigger: | ||
url: https://europe-west1-wakeup-278716.cloudfunctions.net/CallRoom | ||
ingressSettings: ALLOW_ALL | ||
labels: | ||
deployment-tool: cli-gcloud | ||
maxInstances: 10 | ||
name: projects/wakeup-278716/locations/europe-west1/functions/CallRoom | ||
runtime: go113 | ||
serviceAccountEmail: wakeup-278716@appspot.gserviceaccount.com | ||
sourceUploadUrl: https://storage.googleapis.com/gcf-upload-europe-west1-41ca9e21-11e3-4dc4-b7c6-5f495480bf26/813e4536-15fb-4bc4-bb8a-9b1a0a3d7306.zip?GoogleAccessId=service-956281435571@gcf-admin-robot.iam.gserviceaccount.com&Expires=1590917346&Signature=odpVW7iSw9TNgi8H0qR2OpJxqs%2Bgn3Rv2fJJ2AOHEBNtH5F6GEHwJNaO%2BhUez%2BHSzwwBS2OjIeWm5F%2BcDXO8Wz0k36QU4gq75omqAgzgRHUN4pp3EFUJBUICX99VawBcRZK6TNwQl4L5s6L8XWS86UWEizP7ObuCZ7YARU5eVeqVpM%2F4OVNf%2FDgGGXvnI7AL%2FyDit3u24ZIrlGiJN0zyO5SydRUnvaRKGS7sfLgQ2hMSXgR1mjpwaCqVZIk810WoYh%2BCzUqaC7Ij%2BZqRv1vgNcJdJHvEy%2FW72lIKaCIto0MerEScu3Icy6OCN%2BccGWLbQAvjhX8QZFlBuQxn9ggdSw%3D%3D | ||
status: ACTIVE | ||
timeout: 60s | ||
updateTime: '2020-05-31T09:01:01.877Z' | ||
versionId: '8' | ||
Deploying function (may take a while - up to 2 minutes)...done. | ||
availableMemoryMb: 128 | ||
entryPoint: ScheduleCall | ||
environmentVariables: | ||
WAKEUP_CALL_ENDPOINT: https://www.google.com | ||
WAKEUP_CALL_ROOM_ENDPOINT: https://europe-west1-wakeup-278716.cloudfunctions.net/CallRoom | ||
WAKEUP_GC_PROJECT: wakeup-278716 | ||
WAKEUP_GC_PROJECT_LOCATION: europe-west1 | ||
WAKEUP_PG_ADDR: /cloudsql/wakeup-278716:europe-west1:wakeup-postgres/.s.PGSQL.5432 | ||
WAKEUP_PG_DB: wakeup | ||
WAKEUP_PG_PASSWORD: xxxxxx | ||
WAKEUP_PG_PROTO: unix | ||
WAKEUP_PG_USER: wakeup | ||
WAKEUP_SCHEDULER_LOCATION: projects/wakeup-278716/locations/europe-west1 | ||
WAKEUP_SCHEDULER_TIMEZONE: Europe/Moscow | ||
httpsTrigger: | ||
url: https://europe-west1-wakeup-278716.cloudfunctions.net/ScheduleCall | ||
ingressSettings: ALLOW_ALL | ||
labels: | ||
deployment-tool: cli-gcloud | ||
maxInstances: 10 | ||
name: projects/wakeup-278716/locations/europe-west1/functions/ScheduleCall | ||
runtime: go113 | ||
serviceAccountEmail: wakeup-278716@appspot.gserviceaccount.com | ||
sourceUploadUrl: https://storage.googleapis.com/gcf-upload-europe-west1-41ca9e21-11e3-4dc4-b7c6-5f495480bf26/221ff1fb-664c-443e-93e8-f959abac9cff.zip?GoogleAccessId=service-956281435571@gcf-admin-robot.iam.gserviceaccount.com&Expires=1590917464&Signature=pHIUVHzbJmGUEnVm121bdkftd7J8nwBpM66ICzVaDRWVXxCpFIkP7CrLApO2KeDS%2F0TD%2F1AYjFZ%2F5Hh3S8vgv8mdLzGoSA93FCr%2FUBFlwQ4vQW2ExTkv%2B2948nzKdocJXLOMXm7%2FeNQaNhaieQg%2BaLlK2QnUnAeQLsZMnYaPxQ3YoszVb4Q0gGdMfvUAVCGzQymtXQ%2FwprCqg3XsGSwcWStyzKNSUyjN7smNxGTOKki8yBHp2N6WTER0K93LRhxI9TvVxv3XL0OAkaKRzgQEsvGO7KlK14M6UtssxB71edMPpYpLZi%2BwkrowQS6kvEBFbEv5ZOyTDnYRVdiyO7jEUg%3D%3D | ||
status: ACTIVE | ||
timeout: 60s | ||
updateTime: '2020-05-31T09:03:04.348Z' | ||
versionId: '7' | ||
``` | ||
|
||
5. Simulate guest "wake me up" calls: | ||
|
||
``` | ||
macbook:cmd jt$ curl --location --request POST 'https://europe-west1-wakeup-278716.cloudfunctions.net/ScheduleCall' \ | ||
> --header 'Content-Type: application/json' \ | ||
> --data-raw '[ | ||
> { | ||
> "firstname": "Eugene", | ||
> "lastname": "Toropov", | ||
> "call_time": "20:15", | ||
> "room_number": 1 | ||
> }, | ||
> { | ||
> "firstname": "Olga", | ||
> "lastname": "Toropova", | ||
> "call_time": "20:16", | ||
> "room_number": 2 | ||
> } | ||
> ]' | ||
[ | ||
{ | ||
"room_number": 1, | ||
"firstname": "Eugene", | ||
"lastname": "Toropov", | ||
"call_time": "20:15", | ||
"retry_count": 0, | ||
"created": "2020-05-31T17:49:09.190929Z" | ||
}, | ||
{ | ||
"room_number": 2, | ||
"firstname": "Olga", | ||
"lastname": "Toropova", | ||
"call_time": "20:16", | ||
"retry_count": 0, | ||
"created": "2020-05-31T17:49:09.190929Z" | ||
} | ||
] | ||
``` | ||
|
||
6. Make sure jobs scheduled: | ||
|
||
 | ||
|
||
7. Make sure rooms called at xx:yy: | ||
|
||
 | ||
|
||
8. Make sure rooms rescheduled in case of call error (see WAKEUP_SCHEDULER_MAX_RETRY_COUNT and WAKEUP_SCHEDULER_RETRY_PERIOD in `cmd/env.sh`): | ||
|
||
 | ||
|
||
9. Have a nice day :) | ||
|
||
# TODO | ||
|
||
- Move to interfaces | ||
- Add unit tests with https://github.com/stretchr/testify | ||
- Improve deploy time | ||
- More QA as obviously some bugs missed :) | ||
- Full-featured admin panel to view and manage guests/rooms to call, passed/future calls, call statuses, etc. |
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,20 +1,15 @@ | ||
module github.com/evt/video8 | ||
module github.com/evt/video12 | ||
|
||
go 1.14 | ||
|
||
require ( | ||
cloud.google.com/go v0.65.0 | ||
cloud.google.com/go v0.66.0 | ||
github.com/go-pg/migrations/v7 v7.1.11 | ||
github.com/go-pg/pg/v9 v9.1.7 | ||
github.com/gorilla/mux v1.7.4 | ||
github.com/go-pg/pg/v9 v9.2.0 | ||
github.com/gorilla/mux v1.8.0 | ||
github.com/jinzhu/configor v1.2.0 | ||
github.com/lib/pq v1.8.0 | ||
github.com/pkg/errors v0.9.1 | ||
golang.org/x/net v0.0.0-20200904194848-62affa334b73 // indirect | ||
golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43 // indirect | ||
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 | ||
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009 // indirect | ||
google.golang.org/api v0.31.0 // indirect | ||
google.golang.org/genproto v0.0.0-20200904004341-0bd0a958aa1d | ||
google.golang.org/grpc v1.32.0 // indirect | ||
google.golang.org/genproto v0.0.0-20200925023002-c2d885f95484 | ||
) |
Oops, something went wrong.