From 1318d50f6b71f0adb81eb53ae05a7018071c64c3 Mon Sep 17 00:00:00 2001 From: Paul Swartz Date: Thu, 12 Dec 2024 15:50:46 -0500 Subject: [PATCH 1/3] chore: allow running Redis standalone This makes it much easier to run locally, as you can run a single instance. --- .env.template | 1 + config/runtime.exs | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/.env.template b/.env.template index 8329bef5a9..ff0555a945 100644 --- a/.env.template +++ b/.env.template @@ -19,6 +19,7 @@ MBTA_API_BASE_URL=https://api-dev.mbtace.com # If you're using Redis for local development, the port is 30001 # REDIS_HOST= # REDIS_PORT= +REDIS_STANDALONE=true # These credentials control access to resetting cache entries for the CMS. # You can set them to be whatever you want. diff --git a/config/runtime.exs b/config/runtime.exs index f115253b46..7fc4206752 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -48,6 +48,13 @@ end redis_host_env = System.get_env("REDIS_HOST", "127.0.0.1") redis_port_env = System.get_env("REDIS_PORT", "6379") +redis_mode = + if System.get_env("REDIS_STANDALONE") == nil do + :redis_cluster + else + :standalone + end + redis_host = if redis_host_env == "", do: "127.0.0.1", @@ -58,14 +65,17 @@ redis_port = do: 6379, else: String.to_integer(redis_port_env) +redis_conn_opts = [ + host: redis_host, + port: redis_port +] + redis_config = [ - mode: :redis_cluster, + mode: redis_mode, + conn_opts: redis_conn_opts, redis_cluster: [ configuration_endpoints: [ - conn_opts: [ - host: redis_host, - port: redis_port - ] + conn_opts: redis_conn_opts ] ], stats: true, @@ -73,7 +83,7 @@ redis_config = [ ] # This is used by PubSub, we only use the first node in the cluster -config :dotcom, :redis_config, redis_config[:redis_cluster][:configuration_endpoints][:conn_opts] +config :dotcom, :redis_config, redis_conn_opts # Set caches that use the Redis cluster config :dotcom, Dotcom.Cache.Multilevel, From c3fb263defa2343c6e51acb7c4df3de27b7f1d2d Mon Sep 17 00:00:00 2001 From: Paul Swartz Date: Thu, 12 Dec 2024 16:01:08 -0500 Subject: [PATCH 2/3] doc: update README (remove docker compose, add MBTA metro) --- README.md | 58 ++++++++++--------------------------------------------- 1 file changed, 10 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index a098f8e0ec..f24f6341b3 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,12 @@ Welcome to [Dotcom](https://www.notion.so/mbta-downtown-crossing/Dotcom-6aa7b0f0 Minor note - you may see a prompt to upgrade `npm`. This isn't needed, and `"lockfileVersion": 1` in our `assets/package-lock.json` file means it was generated with an `npm` version prior to 7. +1. Update the MBTA Metro assets. From the root of this repo: + ``` + mix mbta_metro.update_assets + ``` + + 1. Set up required environment variables: ``` cp .env.template .env @@ -121,57 +127,13 @@ For details on environment configuration, including optional variables, see ## Running the Server -The easiest way to develop MBTA dotcom is to use Docker Compose. - -``` -docker compose -f deploy/dev.yml up -d --build -``` - -This will set up Redis in cluster mode and run two versions of Dotcom with nginx load balancing requests between them. -Visit http://localhost:4001 to hit the load balancer. -http://localhost:4002 and http://localhost:4003 will take you directly to either of the two nodes. - -You can even connect to individual Elixir nodes in order to run commands. -Let's say you want to connect to dotcom2 (the one running at http://localhost:4003). - -``` -docker exec -it deploy-dotcom-2-1 iex --sname foobarbaz --cookie foobarbaz - -iex(foobarbaz@0b061394460f)1> node = :dotcom2@0b061394460f -:dotcom2@0b061394460f -iex(foobarbaz@0b061394460f)2> Node.connect(node) -true -iex(foobarbaz@0b061394460f)3> :rpc.call(node, Dotcom.Cache.Multilevel, :get, ["cms.repo|important-notices"]) -... -``` - -Note that the address (the @... part) will be different every time. - ---- - -If you choose not to use Docker Compose, you'll still have to run Redis cluster. -The easiest way to get it running is to download and compile it. +To run the server, you'll need to have a Redis instance running. You can either install it manually, or run it via Docker: -``` -cd $HOME -wget https://github.com/redis/redis/archive/7.2.4.tar.gz -tar xvf 7.2.4.tar.gz -cd redis-7.2.4 -make -./utils/create-cluster/create-cluster start -./utils/create-cluster/create-cluster create -f -``` - -When you're done with it: - -``` -./utils/create-cluster/create-cluster stop -cd $HOME -rm 7.2.4.tar.gz -rm -rf redis-7.2.4 +``` shell +docker run --rm -p 6379:6379 redis:7.2.4 ``` -Start the server with `iex -S mix phx.server` +Then, start the server with `iex -S mix phx.server` Then, visit the site at http://localhost:4001. From 8f8433058ebb17fd5d12571c75bd51c7c027f252 Mon Sep 17 00:00:00 2001 From: Paul Swartz Date: Fri, 13 Dec 2024 09:12:55 -0500 Subject: [PATCH 3/3] doc: add RedisClustering.md Documents how to test the Redis clustering locally. Mostly copied from the old README.md. --- docs/RedisClustering.md | 65 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 docs/RedisClustering.md diff --git a/docs/RedisClustering.md b/docs/RedisClustering.md new file mode 100644 index 0000000000..4928b1d207 --- /dev/null +++ b/docs/RedisClustering.md @@ -0,0 +1,65 @@ +# Testing Redis Clustering + +The default development environment only runs under a single Redis instance. +Some features (cross-cluster delete) do need a full cluster to test, so this +document describes how to run such as cluster locally for testing. + +These instructions assume you've already gotten a normal development environment running. + +## Docker Compose + +The easiest way to run the Redis Cluster is to use Docker Compose. + +``` +docker compose -f deploy/dev.yml up -d --build +``` + +This will set up Redis in cluster mode and run two versions of Dotcom with nginx load balancing requests between them. +Visit http://localhost:4001 to hit the load balancer. +http://localhost:4002 and http://localhost:4003 will take you directly to either of the two nodes. + +You can even connect to individual Elixir nodes in order to run commands. +Let's say you want to connect to dotcom2 (the one running at http://localhost:4003). + +``` +docker exec -it deploy-dotcom-2-1 iex --sname foobarbaz --cookie foobarbaz + +iex(foobarbaz@0b061394460f)1> node = :dotcom2@0b061394460f +:dotcom2@0b061394460f +iex(foobarbaz@0b061394460f)2> Node.connect(node) +true +iex(foobarbaz@0b061394460f)3> :rpc.call(node, Dotcom.Cache.Multilevel, :get, ["cms.repo|important-notices"]) +... +``` + +Note that the address (the @... part) will be different every time. + + +## Running Redis Locally + +If you choose not to use Docker Compose, you'll still have to run Redis cluster. +The easiest way to get it running is to download and compile it. + +``` +cd $HOME +wget https://github.com/redis/redis/archive/7.2.4.tar.gz +tar xvf 7.2.4.tar.gz +cd redis-7.2.4 +make +./utils/create-cluster/create-cluster start +./utils/create-cluster/create-cluster create -f +``` + +Start the server with `iex -S mix phx.server` + +Then, visit the site at http://localhost:4001. + +When you're done with the cluster: + +``` +./utils/create-cluster/create-cluster stop +cd $HOME +rm 7.2.4.tar.gz +rm -rf redis-7.2.4 +``` +