From 646fe9c67f3a229ce06ef396b7f0ed9514251c9e Mon Sep 17 00:00:00 2001 From: Piyush Gupta <56182734+gupta-piyush19@users.noreply.github.com> Date: Tue, 18 Mar 2025 15:43:31 +0530 Subject: [PATCH] feat: optional cron jobs check (#4966) --- .env.example | 3 +++ apps/web/Dockerfile | 7 ++++++- docker/docker-compose.yml | 3 +++ .../configuration/environment-variables.mdx | 5 +++-- docs/self-hosting/setup/cluster-setup.mdx | 17 +++++++++++++++++ helm-chart/values.yaml | 2 +- packages/lib/env.ts | 2 ++ turbo.json | 1 + 8 files changed, 36 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index 8564c8edc7..9110d3c192 100644 --- a/.env.example +++ b/.env.example @@ -97,6 +97,9 @@ PASSWORD_RESET_DISABLED=1 # Organization Invite. Disable the ability for invited users to create an account. # INVITE_DISABLED=1 +# Docker cron jobs. Disable the supercronic cron jobs in the Docker image (useful for cluster setups). +# DOCKER_CRON_ENABLED=1 + ########## # Other # ########## diff --git a/apps/web/Dockerfile b/apps/web/Dockerfile index 2bee6c354f..b16f185c9f 100644 --- a/apps/web/Dockerfile +++ b/apps/web/Dockerfile @@ -111,7 +111,12 @@ VOLUME /home/nextjs/apps/web/uploads/ RUN mkdir -p /home/nextjs/apps/web/saml-connection VOLUME /home/nextjs/apps/web/saml-connection -CMD supercronic -quiet /app/docker/cronjobs & \ +CMD if [ "${DOCKER_CRON_ENABLED:-1}" = "1" ]; then \ + echo "Starting cron jobs..."; \ + supercronic -quiet /app/docker/cronjobs & \ + else \ + echo "Docker cron jobs are disabled via DOCKER_CRON_ENABLED=0"; \ + fi; \ (cd packages/database && npm run db:migrate:deploy) && \ (cd packages/database && npm run db:create-saml-database:deploy) && \ exec node apps/web/server.js diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 4e87afb250..a0f2272186 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -69,6 +69,9 @@ x-environment: &environment # Set the below to your Unsplash API Key for their Survey Backgrounds # UNSPLASH_ACCESS_KEY: + # Set the below to 0 to disable cron jobs + # DOCKER_CRON_ENABLED: 1 + ################################################### OPTIONAL (STORAGE) ################################################### # Set the below to set a custom Upload Directory diff --git a/docs/self-hosting/configuration/environment-variables.mdx b/docs/self-hosting/configuration/environment-variables.mdx index 77f768ce22..7783a9793c 100644 --- a/docs/self-hosting/configuration/environment-variables.mdx +++ b/docs/self-hosting/configuration/environment-variables.mdx @@ -59,9 +59,10 @@ These variables are present inside your machine’s docker-compose file. Restart | OIDC_ISSUER | Issuer URL for Custom OpenID Connect Provider (should have .well-known configured at this) | optional (required if OIDC auth is enabled) | | | OIDC_SIGNING_ALGORITHM | Signing Algorithm for Custom OpenID Connect Provider | optional | RS256 | | OPENTELEMETRY_LISTENER_URL | URL for OpenTelemetry listener inside Formbricks. | optional | | -| UNKEY_ROOT_KEY | Key for the [Unkey](https://www.unkey.com/) service. This is used for Rate Limiting for management API. | optional | | +| UNKEY_ROOT_KEY | Key for the [Unkey](https://www.unkey.com/) service. This is used for Rate Limiting for management API. | optional | | | CUSTOM_CACHE_DISABLED | Disables custom cache handler if set to 1 (required for deployment on Vercel) | optional | | | PROMETHEUS_ENABLED | Enables Prometheus metrics if set to 1. | optional | | -| PROMETHEUS_EXPORTER_PORT | Port for Prometheus metrics. | optional | 9090 | | optional | | +| PROMETHEUS_EXPORTER_PORT | Port for Prometheus metrics. | optional | 9090 | +| DOCKER_CRON_ENABLED | Controls whether cron jobs run in the Docker image. Set to 0 to disable (useful for cluster setups). | optional | 1 | Note: If you want to configure something that is not possible via above, please open an issue on our GitHub repo here or reach out to us on Github Discussions and we’ll try our best to work out a solution with you. diff --git a/docs/self-hosting/setup/cluster-setup.mdx b/docs/self-hosting/setup/cluster-setup.mdx index ad2c05f256..eae4fc82ce 100644 --- a/docs/self-hosting/setup/cluster-setup.mdx +++ b/docs/self-hosting/setup/cluster-setup.mdx @@ -160,6 +160,19 @@ When using S3 in a cluster setup, ensure that: - The bucket has appropriate CORS settings configured - IAM roles/users have sufficient permissions for read/write operations +## Disabling Docker Cron Jobs + +When running Formbricks in a cluster setup, you should disable the built-in cron jobs in the Docker image to prevent them from running on multiple instances simultaneously. Instead, you should set up cron jobs in your orchestration system (like Kubernetes) to run on a single instance or as separate jobs. + +To disable the Docker cron jobs, set the following environment variable: + +```sh env +# Disable Docker cron jobs (0 = disabled, 1 = enabled) +DOCKER_CRON_ENABLED=0 +``` + +This will prevent the cron jobs from starting in the Docker container while still allowing all other Formbricks functionality to work normally. + ## Kubernetes Setup Formbricks provides an official Helm chart for deploying the entire cluster stack on Kubernetes. The Helm chart is available in the [Formbricks GitHub repository](https://github.com/formbricks/formbricks/tree/main/helm-chart). @@ -167,6 +180,7 @@ Formbricks provides an official Helm chart for deploying the entire cluster stac ### Features of the Helm Chart The Helm chart provides a complete deployment solution that includes: + - Formbricks application with configurable replicas - PostgreSQL database (with optional HA configuration) - Redis cluster for caching @@ -176,12 +190,14 @@ The Helm chart provides a complete deployment solution that includes: ### Installation Steps 1. Add the Formbricks Helm repository: + ```sh helm repo add formbricks https://raw.githubusercontent.com/formbricks/formbricks/main/helm-chart helm repo update ``` 2. Install the chart: + ```sh helm install formbricks formbricks/formbricks ``` @@ -189,6 +205,7 @@ helm install formbricks formbricks/formbricks ### Configuration Options The Helm chart can be customized using a `values.yaml` file to configure: + - Number of Formbricks replicas - Resource limits and requests - Database configuration diff --git a/helm-chart/values.yaml b/helm-chart/values.yaml index def45c5aa5..c33e1469f6 100644 --- a/helm-chart/values.yaml +++ b/helm-chart/values.yaml @@ -296,4 +296,4 @@ postgresql: containerSecurityContext: enabled: true runAsUser: 1001 - readOnlyRootFilesystem: false + readOnlyRootFilesystem: false \ No newline at end of file diff --git a/packages/lib/env.ts b/packages/lib/env.ts index c2b13e147f..08650a2615 100644 --- a/packages/lib/env.ts +++ b/packages/lib/env.ts @@ -22,6 +22,7 @@ export const env = createEnv({ BREVO_LIST_ID: z.string().optional(), DATABASE_URL: z.string().url(), DEBUG: z.enum(["1", "0"]).optional(), + DOCKER_CRON_ENABLED: z.enum(["1", "0"]).optional(), DEFAULT_ORGANIZATION_ID: z.string().optional(), DEFAULT_ORGANIZATION_ROLE: z.enum(["owner", "manager", "member", "billing"]).optional(), E2E_TESTING: z.enum(["1", "0"]).optional(), @@ -153,6 +154,7 @@ export const env = createEnv({ DEBUG: process.env.DEBUG, DEFAULT_ORGANIZATION_ID: process.env.DEFAULT_ORGANIZATION_ID, DEFAULT_ORGANIZATION_ROLE: process.env.DEFAULT_ORGANIZATION_ROLE, + DOCKER_CRON_ENABLED: process.env.DOCKER_CRON_ENABLED, E2E_TESTING: process.env.E2E_TESTING, EMAIL_AUTH_DISABLED: process.env.EMAIL_AUTH_DISABLED, EMAIL_VERIFICATION_DISABLED: process.env.EMAIL_VERIFICATION_DISABLED, diff --git a/turbo.json b/turbo.json index be2d78dc9a..7a7893accb 100644 --- a/turbo.json +++ b/turbo.json @@ -94,6 +94,7 @@ "BREVO_LIST_ID", "DEFAULT_ORGANIZATION_ID", "DEFAULT_ORGANIZATION_ROLE", + "DOCKER_CRON_ENABLED", "CRON_SECRET", "CUSTOM_CACHE_DISABLED", "DATABASE_URL",