wick_trigger/
trigger.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::collections::HashMap;
use std::convert::Infallible;
use std::sync::Arc;

use async_trait::async_trait;
use structured_output::StructuredOutput;
use tracing::Span;
use wick_config::config::{AppConfiguration, BoundIdentifier, TriggerDefinition};
use wick_runtime::{Runtime, RuntimeBuilder, RuntimeConstraint};

use crate::error::Error;
use crate::resources::Resource;

pub fn build_trigger_runtime(config: &AppConfiguration, span: Span) -> Result<RuntimeBuilder, Infallible> {
  let mut rt = RuntimeBuilder::default();
  if let Some(fetch_opts) = config.options() {
    rt = rt.allow_latest(*fetch_opts.allow_latest());
    rt = rt.allowed_insecure(fetch_opts.allow_insecure().clone());
  }
  for import in config.import() {
    rt.add_import(import.clone());
  }
  for resource in config.resources() {
    rt.add_resource(resource.clone());
  }
  rt = rt.span(span);
  Ok(rt)
}

#[async_trait]
pub trait Trigger {
  /// Start executing the trigger.
  async fn run(
    &self,
    name: String,
    runtime: Runtime,
    app_config: AppConfiguration,
    config: TriggerDefinition,
    resources: Arc<HashMap<BoundIdentifier, Resource>>,
    span: Span,
  ) -> Result<StructuredOutput, Error>;

  /// Shutdown a running trigger.
  async fn shutdown_gracefully(self) -> Result<(), Error>;

  /// Wait for the trigger to finish.
  #[must_use = "this returns the output of the trigger"]
  async fn wait_for_done(&self) -> StructuredOutput;
}

/// Runtime configuration necessary for a trigger to execute.
#[derive(Debug, Clone)]
pub struct TriggerRuntimeConfig {
  pub(crate) constraints: Vec<RuntimeConstraint>,
}

impl TriggerRuntimeConfig {
  /// Extend a runtime builder with the configuration contained within.
  pub fn extend_runtime(self, rt: &mut RuntimeBuilder) {
    for constraint in self.constraints {
      rt.add_constraint(constraint);
    }
  }
}