From 91e6b7a3d71d1a7395e652c5aeec82d71d63e054 Mon Sep 17 00:00:00 2001 From: sukhman-sukh Date: Wed, 22 May 2024 23:09:21 +0530 Subject: [PATCH 1/6] Cron job sorted --- backend-server/controllers/cron_job.go | 33 ++++++++++++++++++++++++++ backend-server/go.mod | 16 ++++++------- backend-server/go.sum | 28 ++++------------------ backend-server/main.go | 2 +- 4 files changed, 46 insertions(+), 33 deletions(-) create mode 100644 backend-server/controllers/cron_job.go diff --git a/backend-server/controllers/cron_job.go b/backend-server/controllers/cron_job.go new file mode 100644 index 0000000..27dd0cb --- /dev/null +++ b/backend-server/controllers/cron_job.go @@ -0,0 +1,33 @@ +package controllers + +import ( + "log" + + "github.com/robfig/cron/v3" +) + +// myScheduledTask is the task that will be run every minute +func myScheduledTask() { + log.Println("This task is run every minute") +} + + + +// StartCronJob starts the cron job +func init() { + // Initialize a new cron instance + c := cron.New(cron.WithSeconds()) + + // Schedule the task to run every minute + _, err := c.AddFunc("0 */1 * * * *", myScheduledTask) + if err != nil { + log.Fatalf("Error scheduling task: %v", err) + } + + // // Funcs may also be added to a running Cron + // c.AddFunc("@daily", func() { fmt.Println("Every day") }) + + // Start the cron scheduler + c.Start() + +} diff --git a/backend-server/go.mod b/backend-server/go.mod index 40a45c8..44544d2 100644 --- a/backend-server/go.mod +++ b/backend-server/go.mod @@ -2,18 +2,18 @@ module regional_server go 1.21.6 +require ( + github.com/gofiber/fiber/v2 v2.52.0 + github.com/joho/godotenv v1.5.1 + github.com/robfig/cron/v3 v3.0.1 + go.mongodb.org/mongo-driver v1.13.1 +) + require ( github.com/andybalholm/brotli v1.0.5 // indirect - github.com/gabriel-vasile/mimetype v1.4.2 // indirect - github.com/go-playground/locales v0.14.1 // indirect - github.com/go-playground/universal-translator v0.18.1 // indirect - github.com/go-playground/validator/v10 v10.17.0 // indirect - github.com/gofiber/fiber/v2 v2.52.0 // indirect github.com/golang/snappy v0.0.1 // indirect github.com/google/uuid v1.5.0 // indirect - github.com/joho/godotenv v1.5.1 // indirect github.com/klauspost/compress v1.17.0 // indirect - github.com/leodido/go-urn v1.2.4 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect @@ -26,9 +26,7 @@ require ( github.com/xdg-go/scram v1.1.2 // indirect github.com/xdg-go/stringprep v1.0.4 // indirect github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect - go.mongodb.org/mongo-driver v1.13.1 // indirect golang.org/x/crypto v0.14.0 // indirect - golang.org/x/net v0.17.0 // indirect golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect golang.org/x/sys v0.15.0 // indirect golang.org/x/text v0.13.0 // indirect diff --git a/backend-server/go.sum b/backend-server/go.sum index 7065b05..23f0972 100644 --- a/backend-server/go.sum +++ b/backend-server/go.sum @@ -1,19 +1,12 @@ github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= -github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= -github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= -github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= -github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= -github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= -github.com/go-playground/validator/v10 v10.17.0 h1:SmVVlfAOtlZncTxRuinDPomC2DkXJ4E5T9gDA0AIH74= -github.com/go-playground/validator/v10 v10.17.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= github.com/gofiber/fiber/v2 v2.52.0 h1:S+qXi7y+/Pgvqq4DrSmREGiFwtB7Bu6+QFLuIHYw/UE= github.com/gofiber/fiber/v2 v2.52.0/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ= github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -22,8 +15,6 @@ github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwA github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM= github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= -github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= -github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= @@ -33,15 +24,10 @@ github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZ github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= +github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA= @@ -69,8 +55,6 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= -golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -98,7 +82,5 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/backend-server/main.go b/backend-server/main.go index aab2f85..3d248b9 100644 --- a/backend-server/main.go +++ b/backend-server/main.go @@ -11,7 +11,7 @@ func main() { app := fiber.New() configs.ConnectDB() - + routes.MerchantRouter(app) routes.MapRouter(app) From b4c093d0d71155047c4e073df29300bb4764e30a Mon Sep 17 00:00:00 2001 From: sukhman-sukh Date: Fri, 24 May 2024 16:32:15 +0530 Subject: [PATCH 2/6] Feat : freq Mapper --- ansible_script/Inventory/inventory.yml | 0 ansible_script/playbook/container_manager.yml | 48 +++++++++++++++++++ centralCDN/pkg/types/types.go | 8 ++++ centralCDN/pkg/utils/frequencyMapper.go | 36 ++++++++++++++ centralCDN/pkg/utils/init.go | 1 + 5 files changed, 93 insertions(+) create mode 100644 ansible_script/Inventory/inventory.yml create mode 100644 ansible_script/playbook/container_manager.yml create mode 100644 centralCDN/pkg/utils/frequencyMapper.go diff --git a/ansible_script/Inventory/inventory.yml b/ansible_script/Inventory/inventory.yml new file mode 100644 index 0000000..e69de29 diff --git a/ansible_script/playbook/container_manager.yml b/ansible_script/playbook/container_manager.yml new file mode 100644 index 0000000..c276786 --- /dev/null +++ b/ansible_script/playbook/container_manager.yml @@ -0,0 +1,48 @@ +- name: Build and run Docker container + hosts: host1 + # gather_facts: true + become: yes + tasks: + - name: Check If Docker Is Installed + command: docker --version + + - name: Update the software package repository + pacman: + update_cache: yes + + - name: Clone the Git repository + git: + repo: "https://github.com/ONDC-Sparse-Matrix/Sparse-Matrix-PS" + dest: "/home/death/Desktop/test/" + update: yes + + # - name: Ensure Docker is installed + # pacman: + # name: docker + # state: present + # update_cache: no + # ignore_errors: True + # async: 30 + # poll: 30 + + - name: Start Docker service + service: + name: docker + state: started + enabled: yes + + + - name: Move to repository directory + command: chdir=/home/death/Desktop/test/ ls + + - name: Build Docker image + command: docker build -t frontend ./user-frontend + + - name: Run Docker container + command: docker run -d --name Fronend_container frontend + + - name: Ensure the Docker container is running + docker_container: + name: Fronend_container + image: frontend + state: started diff --git a/centralCDN/pkg/types/types.go b/centralCDN/pkg/types/types.go index ee07bc6..ca0b816 100644 --- a/centralCDN/pkg/types/types.go +++ b/centralCDN/pkg/types/types.go @@ -16,3 +16,11 @@ type ServerRange struct { } var ServerRangeList []ServerRange + +// array of pincode vs frequency +var FrequencyMap = make(map[int]int) + +var MinFreq int + +// Array of frequency vs pincode +var Top50 = make(map[int][]int) diff --git a/centralCDN/pkg/utils/frequencyMapper.go b/centralCDN/pkg/utils/frequencyMapper.go new file mode 100644 index 0000000..84d21e6 --- /dev/null +++ b/centralCDN/pkg/utils/frequencyMapper.go @@ -0,0 +1,36 @@ +package utils + +import ( + // "../types" + "centralCDN/pkg/types" + "fmt" +) + +func UpdateFreqMap(pincode int) { + + fmt.Println("Updating frequency map with pincode ", pincode) + + freq := types.FrequencyMap[pincode] + types.FrequencyMap[pincode] = types.FrequencyMap[pincode] + 1 + + if len(types.Top50) < 50 { + fmt.Println("Less than 50 in map ", pincode) + types.Top50[freq+1] = append(types.Top50[freq+1], pincode) + if freq+1 > types.MinFreq { + types.MinFreq = freq + 1 + } + } else { + fmt.Println("More than 50 ", pincode) + if freq+1 > types.MinFreq { + types.Top50[freq+1] = append(types.Top50[freq+1], pincode) + delete(types.Top50, types.MinFreq) + types.MinFreq = freq + 1 + fmt.Println("Updating top 50 ", pincode) + } + } + + for key, array := range types.Top50 { + fmt.Printf("Array for key %d: %v\n", key, array) + } + +} diff --git a/centralCDN/pkg/utils/init.go b/centralCDN/pkg/utils/init.go index f8c81e8..4f86934 100644 --- a/centralCDN/pkg/utils/init.go +++ b/centralCDN/pkg/utils/init.go @@ -16,6 +16,7 @@ func InitCacheServerList() { func InitPincode() { types.PincodeCount = 30000 + types.MinFreq = 0 } func InitServerRangeList() { From 91645b3433ed200500fd0ff0a8e3d699cb28bcf1 Mon Sep 17 00:00:00 2001 From: sukhman-sukh Date: Sun, 26 May 2024 04:51:05 +0530 Subject: [PATCH 3/6] Ansible_scripts --- ansible_script/Inventory/inventory.yml | 46 +++++++ ansible_script/playbook/container_manager.yml | 124 ++++++++++++++---- 2 files changed, 147 insertions(+), 23 deletions(-) diff --git a/ansible_script/Inventory/inventory.yml b/ansible_script/Inventory/inventory.yml index e69de29..0cf6626 100644 --- a/ansible_script/Inventory/inventory.yml +++ b/ansible_script/Inventory/inventory.yml @@ -0,0 +1,46 @@ +frontend: + hosts: + host1: + ansible_host: localhost # Add IP of frontend server here + # ansible_port: 3000 # ssh-port + # ansible_user: + # ansible_ssh_pass: + # ansible_ssh_private_key_file: + ansible_connection: local + ansible_python_interpreter: "/usr/bin/env python" + +centralServer: #Also acts as load balancer + hosts: + host2: + ansible_host: localhost # Add IP of frontend server here + # ansible_port: 3001 + ansible_connection: local + ansible_python_interpreter: "/usr/bin/env python" + +cacheServer: + hosts: + host3: + ansible_host: localhost # Add IP of frontend server here + # ansible_port: 4000 + ansible_connection: local + ansible_python_interpreter: "/usr/bin/env python" + +regionalServer: + hosts: + host4: + ansible_host: localhost # Add IP of frontend server here + # ansible_port: 5000 + ansible_connection: local + + +#groups + +Server1: + children: + frontend: + centralServer: + cacheServer: + +Server2: + children: + regionalServer: \ No newline at end of file diff --git a/ansible_script/playbook/container_manager.yml b/ansible_script/playbook/container_manager.yml index c276786..7cf9203 100644 --- a/ansible_script/playbook/container_manager.yml +++ b/ansible_script/playbook/container_manager.yml @@ -1,20 +1,22 @@ +# Set up basic server configurations + - name: Build and run Docker container - hosts: host1 - # gather_facts: true + hosts: all + gather_facts: true become: yes tasks: - - name: Check If Docker Is Installed - command: docker --version + # - name: Check If Docker Is Installed + # command: docker --version - - name: Update the software package repository - pacman: - update_cache: yes - - - name: Clone the Git repository - git: - repo: "https://github.com/ONDC-Sparse-Matrix/Sparse-Matrix-PS" - dest: "/home/death/Desktop/test/" - update: yes + # - name: Update the software package repository + # pacman: + # update_cache: yes + + # - name: Clone the Git repository + # git: + # repo: "https://github.com/ONDC-Sparse-Matrix/Sparse-Matrix-PS" + # dest: "$HOME/test/" + # update: yes # - name: Ensure Docker is installed # pacman: @@ -30,19 +32,95 @@ name: docker state: started enabled: yes + + # - name: Clean up the Docker images + # shell: docker rm $(docker ps -q -a -f status=exited) - - - name: Move to repository directory - command: chdir=/home/death/Desktop/test/ ls +# Build frontend container +- name: Build and run frontend + hosts: frontend + gather_facts: true + become: yes + tasks: + - name: Build Docker image frontend + command: ls + command: docker build -t frontend $HOME/test/user-frontend + + - name: Run Docker container frontend + command: docker run -d --name=fronend_container -p 3000:3000 frontend + ignore_errors: True + + - name: Ensure the Docker container is running frontend + docker_container: + name: fronend_container + image: frontend + state: started + ignore_errors: True - - name: Build Docker image - command: docker build -t frontend ./user-frontend +# Build central_server container +- name: Build and run + hosts: centralServer + gather_facts: true + become: yes + tasks: + - name: Build Docker image central_server + command: ls + command: docker build -t central_server $HOME/test/centralCDN - - name: Run Docker container - command: docker run -d --name Fronend_container frontend + - name: Run Docker container central_server + command: docker run -d --name=central_container -p 3001:3001 central_server + ignore_errors: True - - name: Ensure the Docker container is running + - name: Ensure the Docker container is running central_server docker_container: - name: Fronend_container - image: frontend + name: central_container + image: central_server state: started + ignore_errors: True + +# Build cache_server container +- name: Build and run cache_server + hosts: cacheServer + gather_facts: true + become: yes + tasks: + + - name: Build Docker image cache_server + command: ls + command: docker build -t cache_server $HOME/test/cacheServer + + - name: Run Docker container cache_server + command: docker run -d --name=cache_container -p 4000:4000 cache_server + ignore_errors: True + + - name: Ensure the Docker container is running cache_server + docker_container: + name: cache_container + image: cache_server + state: started + + + # Build regional_server container +- name: Build and run regional_server + hosts: regionalServer + gather_facts: true + become: yes + tasks: + + - name: Build Docker image regional_server + command: ls + command: docker build -t regional_server $HOME/test/backend-server + + - name: Run Docker container regional_server + command: docker run -d --name=regional_container -p 5000:5000 regional_server + ignore_errors: True + + - name: Ensure the Docker container is running regional_server + docker_container: + name: regional_container + image: regional_server + state: started + ignore_errors: True + + +# ansible-playbook -v -K playbook/container_manager.yml -i Inventory/inventory.yml \ No newline at end of file From f14377729657dbd04e7a71693ca9bf74af21cd77 Mon Sep 17 00:00:00 2001 From: sukhman-sukh Date: Sun, 26 May 2024 13:25:59 +0530 Subject: [PATCH 4/6] Auto scaling --- ansible_script/playbook/container_manager.yml | 66 +++++++++++++------ 1 file changed, 45 insertions(+), 21 deletions(-) diff --git a/ansible_script/playbook/container_manager.yml b/ansible_script/playbook/container_manager.yml index 7cf9203..137b2d1 100644 --- a/ansible_script/playbook/container_manager.yml +++ b/ansible_script/playbook/container_manager.yml @@ -1,23 +1,16 @@ # Set up basic server configurations - name: Build and run Docker container - hosts: all + hosts: frontend #all gather_facts: true become: yes tasks: - # - name: Check If Docker Is Installed - # command: docker --version - - # - name: Update the software package repository - # pacman: - # update_cache: yes - - # - name: Clone the Git repository - # git: - # repo: "https://github.com/ONDC-Sparse-Matrix/Sparse-Matrix-PS" - # dest: "$HOME/test/" - # update: yes - + - name: Clone the Git repository + git: + repo: "https://github.com/ONDC-Sparse-Matrix/Sparse-Matrix-PS" + dest: "$HOME/test" + update: yes + # - name: Ensure Docker is installed # pacman: # name: docker @@ -27,21 +20,29 @@ # async: 30 # poll: 30 - - name: Start Docker service - service: - name: docker - state: started - enabled: yes + # - name: Start Docker service + # service: + # name: docker + # state: started + # enabled: yes # - name: Clean up the Docker images # shell: docker rm $(docker ps -q -a -f status=exited) -# Build frontend container +Build frontend container + - name: Build and run frontend hosts: frontend gather_facts: true become: yes tasks: + - name: Generate .env file + copy: + dest: "$HOME/test/user-frontend/.env" + content: | + MONGOURI=mongodb://localhost:27017 + CENTRAL_SERVER_URL=http://localhost:3001 + - name: Build Docker image frontend command: ls command: docker build -t frontend $HOME/test/user-frontend @@ -56,13 +57,23 @@ image: frontend state: started ignore_errors: True + + +Build central_server container + -# Build central_server container - name: Build and run hosts: centralServer gather_facts: true become: yes tasks: + - name: Generate .env file + copy: + dest: "$HOME/test/user-frontend/.env" + content: | + MONGOURI=mongodb://localhost:27017 + CENTRAL_SERVER_URL=http://localhost:3001 + - name: Build Docker image central_server command: ls command: docker build -t central_server $HOME/test/centralCDN @@ -84,6 +95,12 @@ gather_facts: true become: yes tasks: + - name: Generate .env file + copy: + dest: "$HOME/test/user-frontend/.env" + content: | + MONGOURI=mongodb://localhost:27017 + CENTRAL_SERVER_URL=http://localhost:3001 - name: Build Docker image cache_server command: ls @@ -107,6 +124,13 @@ become: yes tasks: + - name: Generate .env file + copy: + dest: "$HOME/test/user-frontend/.env" + content: | + MONGOURI=mongodb://localhost:27017 + CENTRAL_SERVER_URL=http://localhost:3001 + - name: Build Docker image regional_server command: ls command: docker build -t regional_server $HOME/test/backend-server From 95df2bd225746adccf7a0d0acd15d4efee91ff8b Mon Sep 17 00:00:00 2001 From: sukhman-sukh Date: Sun, 26 May 2024 16:00:08 +0530 Subject: [PATCH 5/6] Fix cron job --- Todo.json | 1 + backend-server/controllers/cron_job.go | 16 +++++++++++++--- backend-server/main.go | 4 +++- centralServer/.env | 19 +++++++++++++++++++ 4 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 Todo.json create mode 100644 centralServer/.env diff --git a/Todo.json b/Todo.json new file mode 100644 index 0000000..3140334 --- /dev/null +++ b/Todo.json @@ -0,0 +1 @@ +{"verion":"0.0.1","entries":[]} \ No newline at end of file diff --git a/backend-server/controllers/cron_job.go b/backend-server/controllers/cron_job.go index 27dd0cb..f0328dd 100644 --- a/backend-server/controllers/cron_job.go +++ b/backend-server/controllers/cron_job.go @@ -2,19 +2,29 @@ package controllers import ( "log" + "os/exec" "github.com/robfig/cron/v3" ) // myScheduledTask is the task that will be run every minute func myScheduledTask() { - log.Println("This task is run every minute") -} + dumpPath := "./dump/" + uri := "mongodb://localhost:27017" + cmd := exec.Command("mongodump", "--uri", uri, "--out", dumpPath) + + err := cmd.Run() + if err != nil { + log.Fatal(err) + } + + log.Println("Data dumped successfully...") +} // StartCronJob starts the cron job -func init() { +func InitCronJob() { // Initialize a new cron instance c := cron.New(cron.WithSeconds()) diff --git a/backend-server/main.go b/backend-server/main.go index 3d248b9..f8f6a0c 100644 --- a/backend-server/main.go +++ b/backend-server/main.go @@ -4,6 +4,8 @@ import ( "regional_server/configs" "regional_server/routes" //add this + controllers "command-line-arguments/home/death/Desktop/linux_backup/sdsLabs/HACKATHONS/Matrix/Matrix/backendServer/controllers/merchants_controller.go" + "github.com/gofiber/fiber/v2" ) @@ -11,7 +13,7 @@ func main() { app := fiber.New() configs.ConnectDB() - + controllers.InitCronJob(); routes.MerchantRouter(app) routes.MapRouter(app) diff --git a/centralServer/.env b/centralServer/.env new file mode 100644 index 0000000..71c33eb --- /dev/null +++ b/centralServer/.env @@ -0,0 +1,19 @@ +MONGOURI=mongodb://localhost:27017 +MONGOURI_1=mongodb://localhost:27017 +MONGOURI_2=mongodb://localhost:27017 +MONGOURI_3=mongodb://localhost:27017 +MONGOURI_4=mongodb://localhost:27017 +MONGOURI_CENTRAL=mongodb://localhost:27017 + +FRONTEND_URL=http://localhost:5173/ + +RABBITMQ_URI=amqp://guest:guest@localhost:5672/ + +CACHE_SERVER_HOST_1=localhost +CACHE_SERVER_PORT_1=4000 +CACHE_SERVER_HOST_2=localhost +CACHE_SERVER_PORT_2=8001 +CACHE_SERVER_HOST_3=localhost +CACHE_SERVER_PORT_3=8002 +CACHE_SERVER_HOST_4=localhost +CACHE_SERVER_PORT_3=8003 \ No newline at end of file From 214d963aa1128e116160266d360b07ec923f85b7 Mon Sep 17 00:00:00 2001 From: sukhman-sukh Date: Sun, 26 May 2024 19:22:08 +0530 Subject: [PATCH 6/6] Fix cron job --- centralCDN/.env | 19 +++++++++++++++++++ centralCDN/pkg/cmd/main.go | 5 +++-- 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 centralCDN/.env diff --git a/centralCDN/.env b/centralCDN/.env new file mode 100644 index 0000000..71c33eb --- /dev/null +++ b/centralCDN/.env @@ -0,0 +1,19 @@ +MONGOURI=mongodb://localhost:27017 +MONGOURI_1=mongodb://localhost:27017 +MONGOURI_2=mongodb://localhost:27017 +MONGOURI_3=mongodb://localhost:27017 +MONGOURI_4=mongodb://localhost:27017 +MONGOURI_CENTRAL=mongodb://localhost:27017 + +FRONTEND_URL=http://localhost:5173/ + +RABBITMQ_URI=amqp://guest:guest@localhost:5672/ + +CACHE_SERVER_HOST_1=localhost +CACHE_SERVER_PORT_1=4000 +CACHE_SERVER_HOST_2=localhost +CACHE_SERVER_PORT_2=8001 +CACHE_SERVER_HOST_3=localhost +CACHE_SERVER_PORT_3=8002 +CACHE_SERVER_HOST_4=localhost +CACHE_SERVER_PORT_3=8003 \ No newline at end of file diff --git a/centralCDN/pkg/cmd/main.go b/centralCDN/pkg/cmd/main.go index d8d0495..6bb23d5 100644 --- a/centralCDN/pkg/cmd/main.go +++ b/centralCDN/pkg/cmd/main.go @@ -49,7 +49,8 @@ func main() { // {"pincode" : "22114094", "merchantList" : ["merchant1", "merchant2", "merchant3"]}, // {"pincode" : "22114093", "merchantList" : ["merchant1", "merchant2", "merchant3"]} // ]`) - + // pincodeInt, _ := strconv.Atoi(pincode) + // utils.UpdateFreqMap(pincodeInt) // go utils.FetchMerchantData(pincode) return c.SendString(body) }) @@ -69,7 +70,7 @@ func main() { //TODO: @DAGGER store the cacheResponse in the cache - //TODO: @Wayne store the clientCacheResponse in a Queue + //TODO: @Wayne store the clientCacheResponse in a Queue })