feat: delay for scheduled pipelines (#6616)

Co-authored-by: sai nikhil kethe <nikhil@openobserve.ai>
This commit is contained in:
Taiming Liu
2025-04-24 22:30:34 -07:00
committed by GitHub
co-authored by sai nikhil kethe
parent 14e014fe38
commit c19b649da1
7 changed files with 141 additions and 20 deletions
+1
View File
@@ -107,6 +107,7 @@ async fn migrate_pipelines() -> Result<(), anyhow::Error> {
query_condition: old_derived_stream.query_condition,
trigger_condition: old_derived_stream.trigger_condition,
tz_offset: old_derived_stream.tz_offset,
delay: None,
};
let pipeline_source = PipelineSource::Scheduled(new_derived_stream.clone());
@@ -52,6 +52,8 @@ pub struct DerivedStream {
/// The negative secs means the Western Hemisphere
#[serde(default)]
pub tz_offset: i32,
#[serde(skip_serializing_if = "Option::is_none")]
pub delay: Option<i32>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
+17 -1
View File
@@ -143,7 +143,7 @@ pub async fn save(
}
// Save the trigger to db
let next_run_at = Utc::now().timestamp_micros();
let next_run_at = get_next_run_at(&derived_stream)?;
let trigger = db::scheduler::Trigger {
org: derived_stream.org_id.to_string(),
module: db::scheduler::TriggerModule::DerivedStream,
@@ -220,3 +220,19 @@ impl DerivedStreamExt for DerivedStream {
.await
}
}
pub(super) fn get_next_run_at(derived_stream: &DerivedStream) -> Result<i64, anyhow::Error> {
let delay_in_mins = derived_stream.delay.unwrap_or_default();
// validate & parse delay value
if delay_in_mins < 0 {
return Err(anyhow::anyhow!(
"Invalid delay value. Value must be non-negative"
));
}
let delay = chrono::Duration::minutes(delay_in_mins as _);
Ok(chrono::Utc::now()
.checked_add_signed(delay)
.ok_or(anyhow::anyhow!("DateTime arithmetic overflow"))?
.timestamp_micros())
}
+25 -17
View File
@@ -844,7 +844,7 @@ async fn handle_derived_stream_triggers(
"Pipeline associated with trigger not found: {}/{}/{}/{}. Checking after 5 mins.",
org_id, stream_type, pipeline_name, pipeline_id
);
// Check after 7 days if the pipeline is created
// Check after 5 mins if the pipeline is created
new_trigger.next_run_at += Duration::try_minutes(5)
.unwrap()
.num_microseconds()
@@ -882,7 +882,7 @@ async fn handle_derived_stream_triggers(
};
if !pipeline.enabled {
// Pipeline not enabled, check after 7 days
// Pipeline not enabled, check again in 5 mins
let msg = format!(
"Pipeline associated with trigger not enabled: {}/{}/{}/{}. Checking after 5 mins.",
org_id, stream_type, pipeline_name, pipeline_id
@@ -973,9 +973,15 @@ async fn handle_derived_stream_triggers(
// in case the range [start_time, end_time] is greater than querying period, it needs to
// evaluate and ingest 1 period at a time.
let suppossed_to_be_run_at = trigger.next_run_at;
let delay = current_time - suppossed_to_be_run_at; // delay is in microseconds
let mut final_end_time = suppossed_to_be_run_at;
let user_defined_delay = derived_stream
.delay
.and_then(|delay_in_mins| {
chrono::Duration::try_minutes(delay_in_mins as _).and_then(|td| td.num_microseconds())
})
.unwrap_or_default();
let supposed_to_be_run_at = trigger.next_run_at - user_defined_delay;
let delay = current_time - user_defined_delay - supposed_to_be_run_at; // delay is in microseconds
let mut final_end_time = supposed_to_be_run_at;
// let now = Utc::now().timestamp_micros();
let period_num_microseconds = Duration::try_minutes(derived_stream.trigger_condition.period)
.unwrap()
@@ -996,20 +1002,19 @@ async fn handle_derived_stream_triggers(
// query for the period from 5:15pm to 5:20pm. But, if the suppossed to be run at is
// 5:10pm, then we need ingest data for the period from 5:05pm to 5:15pm.
// Which is to cover the skipped period from 5:05pm to 5:15pm.
if delay >= period_num_microseconds {
let end = if delay >= period_num_microseconds {
// final_end_time is the last multiple of given frequency after the "suppossed to be run
// at" timestamp
let frequency_count = delay / period_num_microseconds;
final_end_time = suppossed_to_be_run_at + (frequency_count * period_num_microseconds);
(
Some(t0),
std::cmp::min(suppossed_to_be_run_at, t0 + period_num_microseconds),
)
final_end_time = supposed_to_be_run_at + (frequency_count * period_num_microseconds);
std::cmp::min(supposed_to_be_run_at, t0 + period_num_microseconds)
} else {
(Some(t0), suppossed_to_be_run_at)
}
supposed_to_be_run_at
};
(Some(t0), end)
} else {
(None, suppossed_to_be_run_at)
(None, supposed_to_be_run_at)
};
// In case the scheduler background job (watch_timeout) updates the trigger retries
@@ -1025,7 +1030,8 @@ async fn handle_derived_stream_triggers(
// Go to the next nun at, but use the same trigger start time
new_trigger.next_run_at = derived_stream
.trigger_condition
.get_aligned_next_trigger_time(false, derived_stream.tz_offset, false)?;
.get_aligned_next_trigger_time(false, derived_stream.tz_offset, false)?
+ user_defined_delay;
// Start over next time
new_trigger.retries = 0;
db::scheduler::update_trigger(new_trigger).await?;
@@ -1295,7 +1301,8 @@ async fn handle_derived_stream_triggers(
// Store the last used derived stream period end time
if let Some(start_time) = start {
new_trigger.data = json::to_string(&ScheduledTriggerData {
period_end_time: Some(start_time), // updated start_time as end_time
// updated start_time as end_time
period_end_time: Some(start_time + user_defined_delay),
tolerance: 0,
last_satisfied_at: None,
})
@@ -1312,7 +1319,8 @@ async fn handle_derived_stream_triggers(
// Go to the next nun at, but use the same trigger start time
new_trigger.next_run_at = derived_stream
.trigger_condition
.get_aligned_next_trigger_time(false, derived_stream.tz_offset, false)?;
.get_aligned_next_trigger_time(false, derived_stream.tz_offset, false)?
+ user_defined_delay;
// If the trigger didn't fail, we need to reset the `retries` count.
// Only cumulative failures should be used to check with `max_retries`
@@ -42,6 +42,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
v-model:trigger="streamRoute.trigger_condition"
v-model:sql="streamRoute.query_condition.sql"
v-model:promql="streamRoute.query_condition.promql"
v-model:delay="streamRoute.delay"
v-model:promql_condition="
streamRoute.query_condition.promql_condition
"
@@ -56,6 +57,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
@update:fullscreen="updateFullscreenMode"
@update:stream_type="updateStreamType"
@expandLog="toggleExpandLog"
@update:delay="updateDelay"
class="q-mt-sm"
/>
</div>
@@ -127,6 +129,7 @@ interface StreamRoute {
cron: string;
timezone: any;
};
delay: number;
context_attributes: any;
description: string;
enabled: boolean;
@@ -240,6 +243,7 @@ const getDefaultStreamRoute: any = () => {
frequency: frequency <= 15 ? 15 : frequency,
timezone: "UTC",
},
delay: 0,
context_attributes: [
{
key: "",
@@ -407,6 +411,7 @@ const saveQueryData = async () => {
silence: 0,
timezone: formData.trigger_condition.timezone,
},
delay: formData.delay,
};
if (formData.trigger_condition.frequency_type === "cron") {
@@ -536,6 +541,10 @@ const updateQueryType = (val: string) => {
const toggleExpandLog = (index: number) => {
expandedLogs.value = [];
};
const updateDelay = (val: any) => {
streamRoute.value.delay = parseInt(val);
};
</script>
<style scoped>
@@ -854,8 +854,80 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
>
Note: The period should be the same as frequency.
</div>
</div>
<div class="flex items-center q-mr-sm q-mt-lg">
<div
data-test="scheduled-pipeline-delay-title"
class="text-bold flex items-center q-pb-sm"
style="width: 130px"
>
{{ t("pipeline.delay") + " *" }}
<q-icon
:name="outlinedInfo"
size="17px"
class="q-ml-xs cursor-pointer"
:class="
store.state.theme === 'dark' ? 'text-grey-5' : 'text-grey-7'
"
>
<q-tooltip
anchor="center right"
self="center left"
max-width="300px"
>
<span style="font-size: 14px"
>Delay for which the pipeline is scheduled to run.<br />
e.g. 10 minutes delay means that the pipeline will run 10 minutes after its scheduled time.</span
>
</q-tooltip>
</q-icon>
</div>
<div style="min-height: 58px">
<div
class="flex items-center q-mr-sm"
style="border: 1px solid rgba(0, 0, 0, 0.05); width: fit-content"
>
<div
data-test="scheduled-pipeline-delay-input"
style="width: 87px; margin-left: 0 !important"
class="silence-notification-input"
>
<q-input
v-model="delayCondition"
type="number"
dense
filled
min="0"
style="background: none"
@update:model-value="updateDelay"
/>
</div>
<div
data-test="scheduled-pipeline-delay-unit"
style="
min-width: 90px;
margin-left: 0 !important;
height: 40px;
font-weight: normal;
"
:class="store.state.theme === 'dark' ? 'bg-grey-10' : 'bg-grey-2'"
class="flex justify-center items-center"
>
{{ t("alerts.minutes") }}
</div>
</div>
</div>
</div>
</div>
<div>
</div>
<div
@@ -1115,7 +1187,8 @@ const props = defineProps([
"disableQueryTypeSelection",
"showTimezoneWarning",
"streamType",
"validatingSqlQuery"
"validatingSqlQuery",
"delay"
]);
const emits = defineEmits([
@@ -1139,6 +1212,7 @@ const emits = defineEmits([
"delete:node",
"update:fullscreen",
"update:stream_type",
"update:delay"
]);
const { pipelineObj } = useDragAndDrop();
const { searchObj } = useLogs();
@@ -1197,6 +1271,8 @@ const getColumns = computed(() => {
const { t } = useI18n();
const triggerData = ref(props.trigger);
@@ -1205,6 +1281,8 @@ const query = ref(props.sql);
const promqlQuery = ref(props.promql);
const delayCondition = ref(props.delay);
const tab = ref(props.query_type || "custom");
const stream_type = ref(props.streamType || "logs");
const collapseFields = ref(false);
@@ -1977,6 +2055,10 @@ const copyLogToClipboard = (log: any, copyAsJson: boolean = true) => {
);
};
const updateDelay = (val: any) => {
emits("update:delay",val)
}
defineExpose({
tab,
validateInputs,
@@ -2006,6 +2088,8 @@ defineExpose({
expandedLogs,
copyLogToClipboard,
copyToClipboard,
updateDelay,
delayCondition
});
</script>
+2 -1
View File
@@ -490,7 +490,8 @@
"export":"Export",
"edit":"Edit",
"delete":"Delete",
"view":"View"
"view":"View",
"delay":"Delay"
},
"function": {
"header": "Functions",