diff --git a/backend/.sqlx/query-1d27895aa42ccbb542479b19baefd62790205b529ab0d8af36f18c470e8bb838.json b/backend/.sqlx/query-1d27895aa42ccbb542479b19baefd62790205b529ab0d8af36f18c470e8bb838.json new file mode 100644 index 0000000000..be045d9e75 --- /dev/null +++ b/backend/.sqlx/query-1d27895aa42ccbb542479b19baefd62790205b529ab0d8af36f18c470e8bb838.json @@ -0,0 +1,23 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT restart_unless_cancelled FROM script WHERE hash = $1 AND workspace_id = $2", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "restart_unless_cancelled", + "type_info": "Bool" + } + ], + "parameters": { + "Left": [ + "Int8", + "Text" + ] + }, + "nullable": [ + true + ] + }, + "hash": "1d27895aa42ccbb542479b19baefd62790205b529ab0d8af36f18c470e8bb838" +} diff --git a/backend/.sqlx/query-52ad1773a9f5a1b6be1bb16af408f051f27d3f21b615c468b85aa75b64d943db.json b/backend/.sqlx/query-cc916f88edb1ac791cdfb24c37691db9048e7d921a628a1ff5b71fead1de5e3a.json similarity index 83% rename from backend/.sqlx/query-52ad1773a9f5a1b6be1bb16af408f051f27d3f21b615c468b85aa75b64d943db.json rename to backend/.sqlx/query-cc916f88edb1ac791cdfb24c37691db9048e7d921a628a1ff5b71fead1de5e3a.json index bf25140825..779c29a8d4 100644 --- a/backend/.sqlx/query-52ad1773a9f5a1b6be1bb16af408f051f27d3f21b615c468b85aa75b64d943db.json +++ b/backend/.sqlx/query-cc916f88edb1ac791cdfb24c37691db9048e7d921a628a1ff5b71fead1de5e3a.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "INSERT INTO script (workspace_id, hash, path, parent_hashes, summary, description, content, created_by, schema, is_template, extra_perms, lock, language, kind, tag, draft_only, envs, concurrent_limit, concurrency_time_window_s, cache_ttl, dedicated_worker, ws_error_handler_muted, priority) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9::text::json, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23)", + "query": "INSERT INTO script (workspace_id, hash, path, parent_hashes, summary, description, content, created_by, schema, is_template, extra_perms, lock, language, kind, tag, draft_only, envs, concurrent_limit, concurrency_time_window_s, cache_ttl, dedicated_worker, ws_error_handler_muted, priority, restart_unless_cancelled) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9::text::json, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24)", "describe": { "columns": [], "parameters": { @@ -61,10 +61,11 @@ "Int4", "Bool", "Bool", - "Int2" + "Int2", + "Bool" ] }, "nullable": [] }, - "hash": "52ad1773a9f5a1b6be1bb16af408f051f27d3f21b615c468b85aa75b64d943db" + "hash": "cc916f88edb1ac791cdfb24c37691db9048e7d921a628a1ff5b71fead1de5e3a" } diff --git a/backend/migrations/20231203110120_add_more_script_config.up.sql b/backend/migrations/20231203110120_add_more_script_config.up.sql index f5440ca18f..4241292a86 100644 --- a/backend/migrations/20231203110120_add_more_script_config.up.sql +++ b/backend/migrations/20231203110120_add_more_script_config.up.sql @@ -2,4 +2,4 @@ ALTER TABLE script ADD COLUMN timeout INTEGER; ALTER TABLE flow ADD COLUMN timeout INTEGER; ALTER TABLE script ADD COLUMN delete_after_use BOOLEAN; -ALTER TABLE script ADD COLUMN restart_unless_cancelled SMALLINT; +ALTER TABLE script ADD COLUMN restart_unless_cancelled BOOLEAN; diff --git a/backend/windmill-api/src/scripts.rs b/backend/windmill-api/src/scripts.rs index 0ec80e5cf2..f7a9e3ca97 100644 --- a/backend/windmill-api/src/scripts.rs +++ b/backend/windmill-api/src/scripts.rs @@ -465,8 +465,8 @@ async fn create_script( "INSERT INTO script (workspace_id, hash, path, parent_hashes, summary, description, \ content, created_by, schema, is_template, extra_perms, lock, language, kind, tag, \ draft_only, envs, concurrent_limit, concurrency_time_window_s, cache_ttl, \ - dedicated_worker, ws_error_handler_muted, priority) \ - VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9::text::json, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23)", + dedicated_worker, ws_error_handler_muted, priority, restart_unless_cancelled) \ + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9::text::json, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24)", &w_id, &hash.0, ns.path, @@ -490,6 +490,7 @@ async fn create_script( ns.dedicated_worker, ns.ws_error_handler_muted.unwrap_or(false), ns.priority, + ns.restart_unless_cancelled ) .execute(&mut tx) .await?; diff --git a/backend/windmill-queue/src/jobs.rs b/backend/windmill-queue/src/jobs.rs index 795796de13..e3fea3d181 100644 --- a/backend/windmill-queue/src/jobs.rs +++ b/backend/windmill-queue/src/jobs.rs @@ -559,6 +559,63 @@ pub async fn add_completed_job< } } + if !queued_job.is_flow_step && queued_job.job_kind == JobKind::Script && canceled_by.is_none() { + if let Some(hash) = queued_job.script_hash { + let p = sqlx::query_scalar!( + "SELECT restart_unless_cancelled FROM script WHERE hash = $1 AND workspace_id = $2", + hash.0, + &queued_job.workspace_id + ) + .fetch_optional(db) + .await? + .flatten() + .unwrap_or(false); + + if p { + let tx = PushIsolationLevel::IsolatedRoot(db.clone(), rsmq); + + let (_uuid, tx) = push( + db, + tx, + &queued_job.workspace_id, + JobPayload::ScriptHash { + hash, + path: queued_job.script_path().to_string(), + concurrent_limit: queued_job.concurrent_limit, + concurrency_time_window_s: queued_job.concurrency_time_window_s, + cache_ttl: queued_job.cache_ttl, + dedicated_worker: None, + language: queued_job + .language + .clone() + .unwrap_or_else(|| ScriptLang::Deno), + priority: queued_job.priority, + }, + queued_job.args.clone(), + &queued_job.created_by, + &queued_job.email, + queued_job.permissioned_as.clone(), + None, + queued_job.schedule_path.clone(), + None, + None, + None, + false, + false, + None, + queued_job.visible_to_owner, + Some(queued_job.tag.clone()), + queued_job.timeout, + None, + queued_job.priority, + ) + .await?; + if let Err(e) = tx.commit().await { + tracing::error!("Could not restart job {}: {}", queued_job.id, e); + } + } + } + } // tracing::error!("4 {:?}", start.elapsed()); Ok(queued_job.id) diff --git a/frontend/src/lib/components/ScriptBuilder.svelte b/frontend/src/lib/components/ScriptBuilder.svelte index eff504abec..0a20d37615 100644 --- a/frontend/src/lib/components/ScriptBuilder.svelte +++ b/frontend/src/lib/components/ScriptBuilder.svelte @@ -210,7 +210,8 @@ concurrency_time_window_s: script.concurrency_time_window_s, cache_ttl: script.cache_ttl, ws_error_handler_muted: script.ws_error_handler_muted, - priority: script.priority + priority: script.priority, + restart_unless_cancelled: script.restart_unless_cancelled } }) savedScript = cloneDeep(script) as NewScriptWithDraft @@ -643,6 +644,24 @@ {/if} +
+
+ { + if (script.restart_unless_cancelled) { + script.restart_unless_cancelled = undefined + } else { + script.restart_unless_cancelled = true + } + }} + options={{ + right: 'Restart upon ending unless cancelled' + }} + /> +
+
{#if !isCloudHosted()} -
+
{ + if (!$userStore && $workspaceStore) { + $userStore = await getUserExt($workspaceStore) + } + }, 5000) + } + }