Skip to content

Commit

Permalink
implement init func
Browse files Browse the repository at this point in the history
  • Loading branch information
algo7 committed Oct 12, 2023
1 parent 1e573a8 commit 3ace0bf
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions container_provisioner/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"log"
"os"
"os/signal"
Expand All @@ -11,6 +12,32 @@ import (
"github.com/algo7/TripAdvisor-Review-Scraper/container_provisioner/database"
)

const containerImage = "ghcr.io/algo7/tripadvisor-review-scraper/scraper:latest"

var imageLockKey = fmt.Sprintf("image-pull:%s", containerImage)

func init() {

// Check if the R2_URL environment variable is set
if os.Getenv("R2_URL") == "" {
log.Fatal("R2_URL environment variable not set")
}

// Check if the redis server is up and running
database.RedisConnectionCheck()

// Try to pull the scraper image
lockSuccess := database.SetLock(imageLockKey)

if !lockSuccess {
// If the lock is not acquired, another instance is already pulling the image
return
}

// Pull the scraper image
containers.PullImage(containerImage)
}

func main() {

// Set up signal handling to catch SIGINT and SIGTERM
Expand All @@ -21,20 +48,12 @@ func main() {
go func() {
sig := <-sigCh
cleanupScraperContainers()
database.ReleaseLock(imageLockKey)
os.Exit(int(sig.(syscall.Signal)))
}()

// Check if the R2_URL environment variable is set
if os.Getenv("R2_URL") == "" {
log.Fatal("R2_URL environment variable not set")
}

// Check if the redis server is up and running
database.RedisConnectionCheck()

// Load the API routes
api.Router()

}

// cleanupScraperContainers removes all the running scraper containers
Expand All @@ -49,10 +68,8 @@ func cleanupScraperContainers() {
if !lockSuccess {
continue // skip to the next iteration of the loop
}

// If lockSuccess is true, we have the lock, so we can proceed with the cleanup
containers.RemoveContainer(*container.ContainerID)
database.ReleaseLock(lockKey)

}
}

0 comments on commit 3ace0bf

Please sign in to comment.