Skip to content

Commit

Permalink
implement AcquireProxyContainer function
Browse files Browse the repository at this point in the history
  • Loading branch information
algo7 committed Oct 12, 2023
1 parent 538fb0e commit 8ddcffc
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions container_provisioner/containers/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"os"

"github.com/algo7/TripAdvisor-Review-Scraper/container_provisioner/database"
"github.com/algo7/TripAdvisor-Review-Scraper/container_provisioner/utils"

"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -140,7 +141,7 @@ func TailLog(containerID string) io.Reader {

// Container information
type Container struct {
ContainerID string
ContainerID *string
TaskOwner *string
TargetName *string
URL *string
Expand Down Expand Up @@ -170,16 +171,20 @@ func ListContainersByType(containerType string) []Container {

for _, containerInfo := range containersInfo {

// Get the first 12 characters of the container ID
containerID := containerInfo.ID[:12]

switch containerType {

case "scraper":
// logic for listing scraper containers
if containerInfo.Labels["TaskOwner"] != "" && containerInfo.Labels["TaskOwner"] != "PROXY" {
taskOwner := containerInfo.Labels["TaskOwner"]
targetName := containerInfo.Labels["Target"]

url := fmt.Sprintf("/logs-viewer?container_id=%s", containerInfo.ID[:12])
containers = append(containers, Container{
ContainerID: containerInfo.ID[:12],
ContainerID: &containerID,
URL: &url,
TaskOwner: &taskOwner,
TargetName: &targetName,
Expand All @@ -189,7 +194,7 @@ func ListContainersByType(containerType string) []Container {
case "proxy":
if containerInfo.Labels["TaskOwner"] != "" && containerInfo.Labels["TaskOwner"] == "PROXY" {
containers = append(containers, Container{
ContainerID: containerInfo.ID[:12],
ContainerID: &containerID,
})
}
default:
Expand All @@ -200,6 +205,29 @@ func ListContainersByType(containerType string) []Container {
return containers
}

// AcquireProxyContainer acquires a lock on a proxy container and returns its ID
func AcquireProxyContainer() *string {
availableProxies := ListContainersByType("proxy")

for _, proxy := range availableProxies {
lockKey := "proxy-usage:" + *proxy.ContainerID
lockSuccess := database.SetLock(lockKey)
if lockSuccess {
return proxy.ContainerID
}
// If the lock is not successful, try the next proxy container
}

// If no proxy container could be locked, return an empty string
return nil
}

// ReleaseProxyContainer releases the lock on a proxy container
func ReleaseProxyContainer(containerID string) {
lockKey := "proxy-usage:" + containerID
database.ReleaseLock(lockKey)
}

// GetResultCSVSizeInContainer gets the size of the result csv file in the container
func getResultCSVSizeInContainer(containerID, filePathInContainer string) {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
Expand Down

0 comments on commit 8ddcffc

Please sign in to comment.