Skip to content

Commit

Permalink
Merge pull request #144 from Avanade/143-refactor-timed-jobs
Browse files Browse the repository at this point in the history
143 refactor timed jobs
  • Loading branch information
jerricotandelacruz authored Oct 17, 2024
2 parents 697f0dc + a9be859 commit fa5985b
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 122 deletions.
3 changes: 2 additions & 1 deletion .bicep/webapp/parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"EMAIL_USER_ID" : "",
"LINK_FOOTERS": "",
"ORGANIZATION_NAME": "",
"COMMUNITY_PORTAL_APP_ID": ""
"COMMUNITY_PORTAL_APP_ID": "",
"CALLBACK_RETRY_FREQ": ""
}
}
}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/setup-appservice-resource.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ jobs:
parameters.appServiceSettings.value.DOCKER_REGISTRY_SERVER_PASSWORD : ${{ secrets.CONTAINER_REGISTRY_SERVER_PASSWORD }}
parameters.appServiceSettings.value.APPROVALSYSTEMDB_CONNECTION_STRING : ${{ secrets.DATABASE_CONNECTION_STRING }}
parameters.appServiceSettings.value.COMMUNITY_PORTAL_APP_ID : ${{ vars.COMMUNITY_PORTAL_APP_ID }}
parameters.appServiceSettings.value.CALLBACK_RETRY_FREQ: ${{ vars.CALLBACK_RETRY_FREQ }}

- name: Deploy App Service Plan and Web App
uses: azure/arm-deploy@v1
Expand Down
1 change: 1 addition & 0 deletions src/goapp/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ type ConfigManager interface {
GetLinkFooters() string
GetOrganizationName() string
GetCommunityPortalAppId() string
GetCallbackRetryFreq() string
}
4 changes: 4 additions & 0 deletions src/goapp/config/env-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ func (ecm *envConfigManager) GetOrganizationName() string {
func (ecm *envConfigManager) GetCommunityPortalAppId() string {
return os.Getenv("COMMUNITY_PORTAL_APP_ID")
}

func (ecm *envConfigManager) GetCallbackRetryFreq() string {
return os.Getenv("CALLBACK_RETRY_FREQ")
}
3 changes: 3 additions & 0 deletions src/goapp/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
c "main/controller"
r "main/repository"
s "main/service"
t "main/timed-jobs"
)

var (
Expand Down Expand Up @@ -38,5 +39,7 @@ var (
c.NewItemPageController(svc, conf),
)

timedJobs = t.NewTimedJobs(svc, conf)

httpRouter router.Router = router.NewMuxRouter()
)
18 changes: 1 addition & 17 deletions src/goapp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ package main
import (
"log"
session "main/pkg/session"
rtApprovals "main/routes/pages/approvals"
"strconv"
"time"

ev "main/pkg/envvar"

"github.com/joho/godotenv"
)
Expand All @@ -22,21 +17,10 @@ func main() {
// Create session and GitHubClient
session.InitializeSession()

go checkFailedCallbacks()
go timedJobs.ReprocessFailedCallbacks()

setPageRoutes()
setApiRoutes()

serve()
}

func checkFailedCallbacks() {
// TIMER SERVICE
freq := ev.GetEnvVar("CALLBACK_RETRY_FREQ", "15")
freqInt, _ := strconv.ParseInt(freq, 0, 64)
if freq > "0" {
for range time.NewTicker(time.Duration(freqInt) * time.Minute).C {
rtApprovals.ProcessFailedCallbacks()
}
}
}
1 change: 1 addition & 0 deletions src/goapp/repository/item/item-repository-interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
)

type ItemRepository interface {
GetFailedCallbacks() ([]string, error)
GetItemById(id string) (*model.Item, error)
GetItemsBy(itemOptions model.ItemOptions) ([]model.Item, error)
GetTotalItemsBy(itemOptions model.ItemOptions) (int, error)
Expand Down
20 changes: 20 additions & 0 deletions src/goapp/repository/item/item-repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ func NewItemRepository(db *db.Database) ItemRepository {
}
}

func (r *itemRepository) GetFailedCallbacks() ([]string, error) {
row, err := r.Query("PR_Items_Select_FailedCallbacks")
if err != nil {
return []string{}, err
}
defer row.Close()

result, err := r.RowsToMap(row)
if err != nil {
return []string{}, err
}

var failedCallbacks []string
for _, v := range result {
failedCallbacks = append(failedCallbacks, v["Id"].(string))
}

return failedCallbacks, nil
}

func (r *itemRepository) GetItemById(id string) (*model.Item, error) {
row, err := r.Query("PR_Items_Select_ById", sql.Named("Id", id))
if err != nil {
Expand Down
104 changes: 0 additions & 104 deletions src/goapp/routes/pages/approvals/response.go

This file was deleted.

1 change: 1 addition & 0 deletions src/goapp/service/item/item-service-interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
)

type ItemService interface {
GetFailedCallbacks() ([]string, error)
GetItemById(id string) (*model.Item, error)
GetAll(itemOptions model.ItemOptions) (model.Response, error)
InsertItem(item model.ItemInsertRequest) (string, error)
Expand Down
8 changes: 8 additions & 0 deletions src/goapp/service/item/item-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ func NewItemService(repo *repository.Repository, configManager config.ConfigMana
}
}

func (s *itemService) GetFailedCallbacks() ([]string, error) {
failedCallbacks, err := s.Repository.Item.GetFailedCallbacks()
if err != nil {
return []string{}, err
}
return failedCallbacks, nil
}

func (s *itemService) GetItemById(id string) (*model.Item, error) {
item, err := s.Repository.Item.GetItemById(id)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions src/goapp/timed-jobs/timed-jobs-interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package timedjobs

type TimedJobs interface {
ReprocessFailedCallbacks()
}
83 changes: 83 additions & 0 deletions src/goapp/timed-jobs/timed-jobs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package timedjobs

import (
"bytes"
"encoding/json"
"fmt"
"main/config"
"main/model"
"main/service"
"net/http"
"strconv"
"time"
)

type timedJobs struct {
Service *service.Service
configManager config.ConfigManager
}

func NewTimedJobs(s *service.Service, configManager config.ConfigManager) TimedJobs {
return &timedJobs{
Service: s,
configManager: configManager,
}
}

func (t *timedJobs) ReprocessFailedCallbacks() {
freq := t.configManager.GetCallbackRetryFreq()
freqInt, _ := strconv.ParseInt(freq, 0, 64)
if freqInt > 0 {
for range time.NewTicker(time.Duration(freqInt) * time.Minute).C {
f, err := t.Service.Item.GetFailedCallbacks()
if err != nil {
fmt.Printf("Failed to get failed callbacks: %v", err.Error())
return
}

for _, id := range f {
go t.postCallback(id)
}
}
}
}

func (t *timedJobs) postCallback(id string) {
item, err := t.Service.Item.GetItemById(id)
if err != nil {
fmt.Println("Error getting item by id: ", id)
return
}

if item.CallbackUrl == "" {
fmt.Println("No callback url found")
return
} else {
params := model.ResponseCallback{
ItemId: id,
IsApproved: item.IsApproved,
Remarks: item.ApproverRemarks,
ResponseDate: item.DateResponded,
RespondedBy: item.RespondedBy,
}

jsonReq, err := json.Marshal(params)
if err != nil {
return
}

res, err := http.Post(item.CallbackUrl, "application/json", bytes.NewBuffer(jsonReq))
if err != nil {
fmt.Println("Error posting callback: ", err)
return
}

isCallbackFailed := res.StatusCode != 200

err = t.Service.Item.UpdateItemCallback(id, isCallbackFailed)
if err != nil {
fmt.Println("Error updating item callback: ", err)
return
}
}
}

0 comments on commit fa5985b

Please sign in to comment.