Skip to content

Commit

Permalink
Added call room endpoint + deploy updates
Browse files Browse the repository at this point in the history
  • Loading branch information
evt committed Sep 27, 2020
1 parent fd76f0d commit dcbdc74
Show file tree
Hide file tree
Showing 15 changed files with 299 additions and 23 deletions.
16 changes: 16 additions & 0 deletions .gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This file specifies files that are *not* uploaded to Google Cloud Platform
# using gcloud. It follows the same syntax as .gitignore, with the addition of
# "#!include" directives (which insert the entries of the given .gitignore-style
# file at that point).
#
# For more information, run:
# $ gcloud topic gcloudignore
#
.gcloudignore
# If you would like to upload your .git directory, .gitignore file or files
# from your .gitignore file, remove the corresponding line
# below:
.git
.gitignore

node_modules
32 changes: 24 additions & 8 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions cloud.functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,8 @@ func ScheduleCall(w http.ResponseWriter, r *http.Request) {
s.ScheduleCall(w, r)
}


// CallRoom
func CallRoom(w http.ResponseWriter, r *http.Request) {
s.CallRoom(w, r)
}
14 changes: 2 additions & 12 deletions cmd/deploy.all.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
#!/bin/bash
source ./env.cloud.sh
cd ..
gcloud functions deploy ScheduleCall \
--runtime go113 \
--allow-unauthenticated \
--trigger-http \
--project $GC_PROJECT \
--set-env-vars PG_PROTO=$PG_PROTO,PG_ADDR=$PG_ADDR,PG_DB=$PG_DB,PG_USER=$PG_USER,PG_PASSWORD=$PG_PASSWORD,GC_PROJECT=$GC_PROJECT,GC_PROJECT_LOCATION=$GC_PROJECT_LOCATION \
--region $GC_PROJECT_LOCATION \
--memory 128MB \
--max-instances 10
cd cmd
source ./deploy.call.room.sh
source ./deploy.schedule.call.sh
13 changes: 13 additions & 0 deletions cmd/deploy.call.room.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
source ./env.cloud.sh
cd ..
gcloud functions deploy CallRoom \
--runtime go113 \
--allow-unauthenticated \
--trigger-http \
--project $GC_PROJECT \
--set-env-vars PG_PROTO=$PG_PROTO,PG_ADDR=$PG_ADDR,PG_DB=$PG_DB,PG_USER=$PG_USER,PG_PASSWORD=$PG_PASSWORD,GC_PROJECT=$GC_PROJECT,GC_PROJECT_LOCATION=$GC_PROJECT_LOCATION,CALL_ROOM_ENDPOINT=$CALL_ROOM_ENDPOINT,SCHEDULER_LOCATION=$SCHEDULER_LOCATION,SCHEDULER_TIMEZONE=$SCHEDULER_TIMEZONE,CALL_ENDPOINT=$CALL_ENDPOINT,SCHEDULER_MAX_RETRY_COUNT=$SCHEDULER_MAX_RETRY_COUNT,SCHEDULER_RETRY_PERIOD=$SCHEDULER_RETRY_PERIOD \
--region $GC_PROJECT_LOCATION \
--memory 128MB \
--max-instances 10
cd cmd
13 changes: 13 additions & 0 deletions cmd/deploy.schedule.call.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
source ./env.cloud.sh
cd ..
gcloud functions deploy ScheduleCall \
--runtime go113 \
--allow-unauthenticated \
--trigger-http \
--project $GC_PROJECT \
--set-env-vars PG_PROTO=$PG_PROTO,PG_ADDR=$PG_ADDR,PG_DB=$PG_DB,PG_USER=$PG_USER,PG_PASSWORD=$PG_PASSWORD,GC_PROJECT=$GC_PROJECT,GC_PROJECT_LOCATION=$GC_PROJECT_LOCATION,CALL_ROOM_ENDPOINT=$CALL_ROOM_ENDPOINT,SCHEDULER_LOCATION=$SCHEDULER_LOCATION,SCHEDULER_TIMEZONE=$SCHEDULER_TIMEZONE,CALL_ENDPOINT=$CALL_ENDPOINT,SCHEDULER_MAX_RETRY_COUNT=$SCHEDULER_MAX_RETRY_COUNT,SCHEDULER_RETRY_PERIOD=$SCHEDULER_RETRY_PERIOD \
--region $GC_PROJECT_LOCATION \
--memory 128MB \
--max-instances 10
cd cmd
4 changes: 2 additions & 2 deletions cmd/env.cloud.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ export GOOGLE_APPLICATION_CREDENTIALS=./serviceaccount.json

# Google Cloud Postgres
export PG_PROTO=unix
export PG_ADDR=/cloudsql/wakeup-286404:europe-west1:wakeup/.s.PGSQL.5432
export PG_ADDR=/cloudsql/wakeup-290812:europe-west1:wakeup-postgres/.s.PGSQL.5432
export PG_DB=wakeup
export PG_USER=wakeup
export PG_PASSWORD=wakeup

export GC_PROJECT=wakeup-286404
export GC_PROJECT=wakeup-290812
export GC_PROJECT_LOCATION=europe-west1

export CALL_ROOM_ENDPOINT=https://$GC_PROJECT_LOCATION-$GC_PROJECT.cloudfunctions.net/CallRoom
Expand Down
2 changes: 1 addition & 1 deletion cmd/env.local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export PG_DB=wakeup
export PG_USER=postgres
export PG_PASSWORD=postgres

export GC_PROJECT=wakeup-286404
export GC_PROJECT=wakeup-290812
export GC_PROJECT_LOCATION=europe-west1

export CALL_ROOM_ENDPOINT=https://$GC_PROJECT_LOCATION-$GC_PROJECT.cloudfunctions.net/CallRoom
Expand Down
11 changes: 11 additions & 0 deletions db/calls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package db

import (
"github.com/evt/video8/model"
)

// SaveCall saves a call in Postgres
func (db *PgDB) SaveCall(call *model.Call) error {
_, err := db.Model(call).Returning("*").Insert()
return err
}
24 changes: 24 additions & 0 deletions db/migrations/2_calls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package migrations

import (
"fmt"

"github.com/go-pg/migrations/v7"
)

func init() {
migrations.MustRegisterTx(func(db migrations.DB) error {
fmt.Println("creating table calls...")
_, err := db.Exec(`CREATE TABLE calls(
call_id serial not null primary key,
room_number integer not null,
call_status integer not null,
created timestamp default current_timestamp
)`)
return err
}, func(db migrations.DB) error {
fmt.Println("dropping table calls...")
_, err := db.Exec(`DROP TABLE calls`)
return err
})
}
24 changes: 24 additions & 0 deletions db/rooms.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db

import (
"github.com/evt/video8/model"
"github.com/go-pg/pg/v9"
)

// AddRooms saves a list of rooms in Postgres
Expand All @@ -12,3 +13,26 @@ func (db *PgDB) AddRooms(rooms []*model.Room) error {
return err
}


// FindRooms returns room list by call time
func (db *PgDB) FindRooms(callTime string, maxRetryCount int) ([]*model.Room, error) {
var rooms []*model.Room
err := db.Model(&rooms).
Where("call_time = ?", callTime).
Where("retry_count < ?", maxRetryCount).
Select()
if err != nil {
if err == pg.ErrNoRows {
return nil, nil
}
return nil, err
}
return rooms, nil
}

// IncRoomRetryCount increases call try count by 1
func (db *PgDB) IncRoomRetryCount(room *model.Room) error {
room.RetryCount++
_, err := db.Model(room).Set("retry_count = ?retry_count").Where("room_number = ?room_number").Update()
return err
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
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
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 h1:qwRHBd0NqMbJxfbotnDhm2ByMI1Shq4Y6oRJo21SGJA=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
17 changes: 17 additions & 0 deletions model/call.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package model

import "time"

// CallRoomRequest is a request coming from google cloud scheduler to call rooms
type CallRoomRequest struct {
callTime string `json:"call_time"`
}

// Call is a room call that we save in Postgres at the moment room is called to know when it happened and what call status was
type Call struct {
tableName struct{} `pg:"calls"`
CallID int `json:"call_id" pg:"call_id,notnull,pk"`
RoomNumber int `json:"room_number" pg:"room_number,notnull"`
CallStatus int `json:"call_status" pg:"call_status,notnull"`
Created time.Time `json:"created" pg:"created"`
}
Loading

0 comments on commit dcbdc74

Please sign in to comment.