From e320a8cead1d7919bb1e9751bf1c0e352a4c32ff Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Wed, 25 Jan 2023 13:07:17 +0100 Subject: [PATCH] restrict number of jobs in the queue for free users --- backend/sqlx-data.json | 75 +++++++++++++++++++++++------- backend/windmill-queue/src/jobs.rs | 12 +++++ 2 files changed, 70 insertions(+), 17 deletions(-) diff --git a/backend/sqlx-data.json b/backend/sqlx-data.json index fbccb68609..048563d040 100644 --- a/backend/sqlx-data.json +++ b/backend/sqlx-data.json @@ -1771,23 +1771,6 @@ }, "query": "\n UPDATE queue\n SET flow_status = JSONB_SET(\n JSONB_SET(flow_status, ARRAY['modules', $1::TEXT], $2),\n ARRAY['step'], $3)\n WHERE id = $4\n " }, - "53f2836a652d34aafe6ffb266a092d7390212bc5532dcc226529a61a53deec42": { - "describe": { - "columns": [], - "nullable": [], - "parameters": { - "Left": [ - "Varchar", - "Varchar", - "Varchar", - "Varchar", - "Text", - "Bool" - ] - } - }, - "query": "INSERT INTO token\n (workspace_id, token, owner, label, expiration, super_admin)\n VALUES ($1, $2, $3, $4, now() + ($5 || ' seconds')::interval, $6)" - }, "541ebd3bac65431237cf3b882dfdcd61ca97c253d9754d05bba59fda89841067": { "describe": { "columns": [ @@ -2088,6 +2071,26 @@ }, "query": "DELETE FROM group_ WHERE name = $1 AND workspace_id = $2" }, + "5ba4b87528ad49f17d72b53c3db30f5ca4b3b0b0afbd5d9721c8b5d692af601b": { + "describe": { + "columns": [ + { + "name": "count", + "ordinal": 0, + "type_info": "Int8" + } + ], + "nullable": [ + null + ], + "parameters": { + "Left": [ + "Text" + ] + } + }, + "query": "SELECT COUNT(id) FROM queue WHERE email = $1" + }, "5c377fffc224a06f693c125f4c13b0a9ccfc217190ba6cf78246294bbc6c93bc": { "describe": { "columns": [], @@ -4101,6 +4104,26 @@ }, "query": "\n SELECT resume_id, approver\n FROM resume_job\n WHERE job = $1\n " }, + "b053117536c067095e2fb2864ce5af33f84b22c24c92fcb870f37f0501f8ea9a": { + "describe": { + "columns": [ + { + "name": "count", + "ordinal": 0, + "type_info": "Int8" + } + ], + "nullable": [ + null + ], + "parameters": { + "Left": [ + "Text" + ] + } + }, + "query": "SELECT COUNT(id) FROM queue WHERE running = true AND email = $1" + }, "b05c5f62ef4aa21d33369130cced0e9d7d128727eb58a9be7ae69cbb16bcbb27": { "describe": { "columns": [], @@ -5600,6 +5623,24 @@ }, "query": "DELETE FROM password WHERE email = $1" }, + "e9c0e331c16312bf086b17c91466c5389d41454fd3f18d73c2e9554845ee9a72": { + "describe": { + "columns": [], + "nullable": [], + "parameters": { + "Left": [ + "Varchar", + "Varchar", + "Varchar", + "Varchar", + "Text", + "Bool", + "Varchar" + ] + } + }, + "query": "INSERT INTO token\n (workspace_id, token, owner, label, expiration, super_admin, email)\n VALUES ($1, $2, $3, $4, now() + ($5 || ' seconds')::interval, $6, $7)" + }, "ea8ebb8d972fe99c960b5a69f794ee2b57bfb1914bf370c5b10313e45fa9b65f": { "describe": { "columns": [], diff --git a/backend/windmill-queue/src/jobs.rs b/backend/windmill-queue/src/jobs.rs index a2581a91bd..c0f267aaa4 100644 --- a/backend/windmill-queue/src/jobs.rs +++ b/backend/windmill-queue/src/jobs.rs @@ -326,6 +326,18 @@ pub async fn push<'c>( "User {email} has exceeded the free usage limit of {MAX_FREE_EXECS} that applies outside of premium workspaces." ))); } + let in_queue = + sqlx::query_scalar!("SELECT COUNT(id) FROM queue WHERE email = $1", email) + .fetch_one(&mut tx) + .await? + .unwrap_or(0); + + if in_queue > MAX_FREE_EXECS.into() { + return Err(error::Error::BadRequest(format!( + "User {email} has exceeded the jobs in queue limit of {MAX_FREE_EXECS} that applies outside of premium workspaces." + ))); + } + let concurrent_runs = sqlx::query_scalar!( "SELECT COUNT(id) FROM queue WHERE running = true AND email = $1", email