Skip to content

Commit

Permalink
Fix SetNextRun timezones, update Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
pjsier committed Sep 19, 2019
1 parent 3ef86ab commit a52e73f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
.PHONY: install test build deploy clean

functions := $(shell find functions -name \*main.go | awk -F'/' '{print $$2}')

install:
npm install

test:
go test ./...

Expand All @@ -8,5 +13,8 @@ build:
env GOOS=linux go build -ldflags="-s -w" -o bin/$$function functions/$$function/main.go ; \
done

deploy:
serverless deploy

clean:
rm -rf bin
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ You'll need GNU Make, Go and node.js installed as well as credentials for AWS an
To install dependencies, build functions and deploy:

```bash
npm install
make install
make build
sls deploy
make deploy
```
13 changes: 8 additions & 5 deletions pkg/models/bill.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,18 @@ func (b *Bill) CreateTweet(billUrl string) string {
func (b *Bill) SetNextRun() {
// Set NextRun to a time in the future within a defined range
loc, _ := time.LoadLocation("America/Chicago")
now := time.Now().In(loc)
now := time.Now().Add(time.Hour * time.Duration(12)).In(loc)
_, offset := now.Zone()

// Make sure it's between 9AM and 10PM Chicago with some randomness to stagger posts
localHour := now.Hour() + (offset / 3600)
diffHours := 24
diffMinutes := 0
if now.Hour() < 9 {
diffHours = diffHours + (9 - now.Hour()) + rand.Intn(4)
if localHour < 9 {
diffHours = diffHours + (9 - localHour) + rand.Intn(4)
diffMinutes = rand.Intn(60)
} else if now.Hour() > 17 {
diffHours = diffHours - (now.Hour() - 17) - rand.Intn(4)
} else if localHour > 17 {
diffHours = diffHours - (localHour - 17) - rand.Intn(4)
diffMinutes = -rand.Intn(60)
}
nextRun := now.Add(time.Hour * time.Duration(diffHours)).Add(time.Minute * time.Duration(diffMinutes))
Expand Down
8 changes: 7 additions & 1 deletion pkg/models/bill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"testing"
"time"
)

func TestUnmarshalBill(t *testing.T) {
Expand Down Expand Up @@ -54,7 +55,12 @@ func TestGetCleanBillID(t *testing.T) {
func TestSetNextRun(t *testing.T) {
bill := Bill{}
bill.SetNextRun()
if bill.NextRun.Hour() < 9 || bill.NextRun.Hour() > 17 {
loc, _ := time.LoadLocation("America/Chicago")
now := time.Now().In(loc)
_, offset := now.Zone()
localHour := bill.NextRun.Hour() + (offset / 3600)

if localHour < 9 || localHour > 17 {
t.Errorf("Hour: %d is outside range 9AM-10PM", bill.NextRun.Hour())
}
}
Expand Down
2 changes: 1 addition & 1 deletion serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ functions:
- Ref: ServerlessSubnetB
- Ref: ServerlessSubnetC
events:
- schedule: rate(4 hours)
- schedule: cron(3 2,14,17,20,23 * * ? *)
update_bill:
handler: bin/update_bill
timeout: 120
Expand Down

0 comments on commit a52e73f

Please sign in to comment.