Skip to content

Commit

Permalink
Merge pull request #8 from junho100/apm
Browse files Browse the repository at this point in the history
Apm
  • Loading branch information
junho100 authored Dec 10, 2024
2 parents bff1bed + be40be3 commit 1835fd2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
15 changes: 13 additions & 2 deletions internal/infrastructure/cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type CronJob struct {
discordClient discord.DiscordClient
notificationStatus redis.NotificationStatus
crawler crawler.Crawler
jobWrapper func(job func()) func()
}

func NewCronJob(notificationService service.NotificationService, telegramClient telegram.TelegramClient, discordClient discord.DiscordClient, notificationStatus redis.NotificationStatus, crawler crawler.Crawler) *CronJob {
Expand All @@ -43,9 +44,13 @@ func NewCronJob(notificationService service.NotificationService, telegramClient
}
}

func (c *CronJob) SetJobWrapper(wrapper func(job func()) func()) {
c.jobWrapper = wrapper
}

func (c *CronJob) Start() {
// 매일 오전 11시에 실행 (KST)
c.cron.AddFunc("0 11 * * *", func() {
job := func() {
// 1. 오늘 올라온 모든 공지사항 크롤링 및 DB 저장
deptNotices, err := c.crawler.FetchDepartmentNotices()
if err != nil {
Expand Down Expand Up @@ -163,8 +168,14 @@ func (c *CronJob) Start() {
}
}
}
})
}

// jobWrapper가 설정되어 있다면 적용
if c.jobWrapper != nil {
job = c.jobWrapper(job)
}

c.cron.AddFunc("0 11 * * *", job)
c.cron.Start()
log.Println("Cron job started (KST - runs at 11:00 AM)")
}
Expand Down
4 changes: 3 additions & 1 deletion internal/infrastructure/monitoring/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ type CloudWatchClient struct {
}

func NewCloudWatchClient() (*CloudWatchClient, error) {
cfg, err := config.LoadDefaultConfig(context.TODO())
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion("ap-northeast-2"),
)
if err != nil {
return nil, err
}
Expand Down
29 changes: 16 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,24 @@ func main() {
)

if cronJob != nil {
if batchMetrics != nil {
// 프로덕션 환경: 모니터링 활성화
go func() {
err := batchMetrics.TrackBatchJob("DailyNotificationDelivery", func() error {
cronJob.Start()
return nil
go func() {
if batchMetrics != nil {
// 프로덕션 환경: 각 크론 작업 실행을 모니터링
cronJob.SetJobWrapper(func(job func()) func() {
return func() {
err := batchMetrics.TrackBatchJob("DailyNotificationDelivery", func() error {
job()
return nil
})
if err != nil {
log.Printf("배치 작업 메트릭 기록 실패: %v", err)
}
}
})
if err != nil {
log.Printf("배치 작업 실행 실패: %v", err)
}
}()
} else {
// 로컬 환경: 모니터링 없이 실행
}
// 크론 작업 시작
cronJob.Start()
}
}()
defer cronJob.Stop()
}

Expand Down

0 comments on commit 1835fd2

Please sign in to comment.