Radish alpha
r
Radicle Job Collaborative Object
Radicle
Git (anonymous pull)
Log in to clone via SSH
feat: add an action to set the log of a run to another URL
✗ CI failure Richard Levitte committed 2 months ago
commit 687ee1b857d25f77243dcff64a128b9493c564bf
parent 8799f728bc1b2e8d1824d2e06557059f8c82127d
1 failed (1 total) View logs
2 files changed +88 -3
modified src/bin/rad-job.rs
@@ -187,7 +187,7 @@ where
}

fn succeeded_job<G>(
-
    command::Succeeded { oid, run }: command::Succeeded,
+
    command::Succeeded { oid, run, url }: command::Succeeded,
    jobs: &mut Jobs<'_, Repository>,
    signer: &Device<G>,
) -> Result<(), error::Succeeded>
@@ -198,11 +198,16 @@ where
    let mut job = JobMut::new(id, job, jobs);
    job.finish(run, Reason::Succeeded, signer)
        .map_err(|err| error::Succeeded::Store { id, err })?;
+
    match url {
+
        Some(v) => job.set_log(run, v, signer)
+
            .map_err(|err| error::Succeeded::Store { id, err })?,
+
        None => oid,
+
    };
    Ok(())
}

fn failed_job<G>(
-
    command::Failed { oid, run }: command::Failed,
+
    command::Failed { oid, run, url }: command::Failed,
    jobs: &mut Jobs<'_, Repository>,
    signer: &Device<G>,
) -> Result<(), error::Failed>
@@ -213,6 +218,11 @@ where
    let mut job = JobMut::new(id, job, jobs);
    job.finish(run, Reason::Failed, signer)
        .map_err(|err| error::Failed::Store { id, err })?;
+
    match url {
+
        Some(v) => job.set_log(run, v, signer)
+
            .map_err(|err| error::Failed::Store { id, err })?,
+
        None => oid,
+
    };
    Ok(())
}

@@ -368,7 +378,7 @@ mod command {
    pub struct Run {
        /// Git commit which the run is processing.
        pub oid: Oid,
-
        /// URL to information about the run, such a a run log.
+
        /// URL to information about the run, such a live run log.
        pub url: Url,
    }

@@ -379,6 +389,8 @@ mod command {
        pub oid: Oid,
        /// Run identifier, a UUID.
        pub run: Uuid,
+
        /// URL to information about the run, such a saved run log.
+
        pub url: Option<Url>,
    }

    /// Mark a run as having finished successfully.
@@ -388,6 +400,8 @@ mod command {
        pub oid: Oid,
        /// Run identifier, a UUID.
        pub run: Uuid,
+
        /// URL to information about the run, such a saved run log.
+
        pub url: Option<Url>,
    }
}

modified src/lib.rs
@@ -276,6 +276,13 @@ pub enum Action {
        /// The [`Reason`] which the node finished with.
        reason: Reason,
    },
+
    /// Change the [`Url`] of the [`Run`]
+
    SetLog {
+
        /// The [`Uuid`] that identifies the [`Run`] to set the log URL for.
+
        uuid: Uuid,
+
        /// The [`Url`] where the node will log any information or data.
+
        log: Url,
+
    }
}

impl CobAction for Action {
@@ -394,6 +401,24 @@ impl Job {
        updated
    }

+
    fn set_log(
+
        &mut self,
+
        node: NodeId,
+
        uuid: Uuid,
+
        log: Url,
+
        timestamp: cob::Timestamp,
+
    ) -> bool {
+
        let Some(runs) = self.runs.get_mut(&node) else {
+
            return false;
+
        };
+
        let mut updated = false;
+
        runs.0.entry(uuid).and_modify(|run| {
+
            updated = true;
+
            *run = run.clone().set_log(log, timestamp);
+
        });
+
        updated
+
    }
+

    fn action(&mut self, node: NodeId, action: Action, timestamp: cob::Timestamp) {
        match action {
            // Cannot request for another `oid`, so we ignore any superfluous
@@ -405,6 +430,9 @@ impl Job {
            Action::Finished { uuid, reason } => {
                self.update(node, uuid, reason, timestamp);
            }
+
            Action::SetLog { uuid, log } => {
+
                self.set_log(node, uuid, log, timestamp);
+
            }
        }
    }
}
@@ -510,6 +538,15 @@ impl Run {
        }
    }

+
    /// Set the log of the `Run`.
+
    fn set_log(self, log: Url, timestamp: cob::Timestamp) -> Self {
+
        Self {
+
            status: self.status,
+
            log: log,
+
            timestamp,
+
        }
+
    }
+

    /// Return URL for the log of this run.
    pub fn log(&self) -> &Url {
        &self.log
@@ -783,6 +820,20 @@ where
        self.transaction("Finished node job", signer, |tx| tx.finish(uuid, reason))
    }

+
    /// Set the log of a [`Run`], identified by the given [`Uuid`], for the node, with
+
    /// the provided [`Url`].
+
    pub fn set_log<G>(
+
        &mut self,
+
        uuid: Uuid,
+
        log: Url,
+
        signer: &Device<G>,
+
    ) -> Result<EntryId, store::Error>
+
    where
+
        G: Signer<crypto::Signature>,
+
    {
+
        self.transaction("Finished node job", signer, |tx| tx.set_log(uuid, log))
+
    }
+

    /// Apply COB operations to a `JobMut`.
    fn transaction<G, F>(
        &mut self,
@@ -874,6 +925,11 @@ where
    fn finish(&mut self, uuid: Uuid, reason: Reason) -> Result<(), store::Error> {
        self.0.push(Action::Finished { uuid, reason })
    }
+

+
    /// Set the log URL.
+
    fn set_log(&mut self, uuid: Uuid, log: Url) -> Result<(), store::Error> {
+
        self.0.push(Action::SetLog { uuid, log })
+
    }
}

#[cfg(test)]
@@ -944,6 +1000,11 @@ mod test {

        job.finish(bob_uuid, Reason::Failed, &bob.signer).unwrap();

+
        // [TODO] replace RID, NID and COMMIT with the actual repo ID, node ID
+
        // and a real commit ID for the log itself.
+
        let bob_finish_log = Url::parse(&format!("rad:RID/NID/blob/COMMIT?path=run-log.md")).unwrap();
+
        job.set_log(bob_uuid, bob_finish_log.clone(), &bob.signer).unwrap();
+

        let succeeded = job.succeeded();
        assert!(succeeded.contains_key(alice.signer.public_key()));
        assert!(!succeeded.contains_key(bob.signer.public_key()));
@@ -952,6 +1013,16 @@ mod test {
        assert!(failed.contains_key(bob.signer.public_key()));
        let started = job.started();
        assert!(started.is_empty());
+

+
        // Check that we still have alice's initial log
+
        let alice_runs = job.runs_of(alice.signer.public_key()).unwrap();
+
        let run = alice_runs.get(&alice_uuid).unwrap();
+
        assert_eq!(run.log, alice_log);
+

+
        // Check that we have bob's finishing log
+
        let bob_runs = job.runs_of(bob.signer.public_key()).unwrap();
+
        let run = bob_runs.get(&bob_uuid).unwrap();
+
        assert_eq!(run.log, bob_finish_log);
    }

    #[test]