decider_distro/
lib.rs

1/*
2 * Copyright 2024 Fluence DAO
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use std::collections::HashMap;
18
19use fluence_spell_dtos::trigger_config::TriggerConfig;
20use maplit::hashmap;
21use serde_json::{json, Value as JValue};
22
23pub use build_info::PKG_VERSION as VERSION;
24
25const DECIDER_SPELL: &'static str = include_str!("../decider-spell/main.main.air");
26const WORKER_SPELL: &'static str = include_str!("../decider-spell/worker_spell.main.air");
27
28pub mod build_info {
29    include!(concat!(env!("OUT_DIR"), "/built.rs"));
30}
31
32pub struct DistrService {
33    pub name: &'static str,
34    pub config: &'static [u8],
35    pub modules: HashMap<&'static str, &'static [u8]>,
36}
37
38pub struct DistrSpell {
39    /// AIR script of the spell
40    pub air: &'static str,
41    /// Initial key-value records for spells KV storage
42    pub kv: HashMap<&'static str, JValue>,
43}
44
45/// Decider's configuration needed for the correct decider start-up
46#[derive(Debug)]
47pub struct DeciderConfig {
48    /// Multiaddr of the IPFS node from which to take worker definitions
49    pub worker_ipfs_multiaddr: String,
50    /// How often to run the worker-spell for updates/healthchecks
51    pub worker_period_sec: u32,
52}
53
54pub fn decider_spell(config: DeciderConfig) -> DistrSpell {
55    let mut worker_config = TriggerConfig::default();
56    worker_config.clock.start_sec = 1;
57    worker_config.clock.period_sec = config.worker_period_sec;
58
59    DistrSpell {
60        air: DECIDER_SPELL,
61        kv: hashmap! {
62            "worker_settings" => json!({
63                "script": WORKER_SPELL,
64                "config": worker_config,
65                "ipfs": config.worker_ipfs_multiaddr
66            }),
67        },
68    }
69}