feat(postgres): Compatibility with new associated error type
Changes the implementation of action execution to deal with the returned associated errors. The only sensible constraint on those errors that I could think of for now is `Debug`, meaning that errors are now persisted as debug messages. This is not as nice to work with for a future implementation of retryable actions as the equivalent in Haskell, but maybe an idea shows up underway. The main issue is that most of the common error types will not be implementing Serde traits, so serialization to/from the same error type is difficult. Adding an implementation constraint for JSON serialisation on error types (i.e. `S::Error: Serialize + Deserialize`) would probably cause headaches for users, especially if they are trying to use an out-of-the-box error type or an error type wrapping foreign errors. Det ska'kke være lett ...
This commit is contained in:
parent
c4b94d8d2d
commit
45afa18846
3 changed files with 25 additions and 12 deletions
|
@ -30,7 +30,7 @@ CREATE TABLE actions (
|
||||||
event_id UUID NOT NULL REFERENCES events(id),
|
event_id UUID NOT NULL REFERENCES events(id),
|
||||||
content JSONB NOT NULL,
|
content JSONB NOT NULL,
|
||||||
status ActionStatus NOT NULL,
|
status ActionStatus NOT NULL,
|
||||||
error JSONB
|
error TEXT
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE INDEX idx_actions_machines ON actions(fsm_id);
|
CREATE INDEX idx_actions_machines ON actions(fsm_id);
|
||||||
|
|
|
@ -110,10 +110,9 @@ struct ActionT {
|
||||||
/// Current status of the action.
|
/// Current status of the action.
|
||||||
status: ActionStatus,
|
status: ActionStatus,
|
||||||
|
|
||||||
/// Serialised error representation, if an error occured during
|
/// Detailed (i.e. Debug-trait formatted) error message, if an
|
||||||
/// processing. TODO: Use some actual error type. Maybe failure
|
/// error occured during action processing.
|
||||||
/// has serialisation support?
|
error: Option<String>,
|
||||||
error: Option<Value>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// The following functions implement the public interface of
|
// The following functions implement the public interface of
|
||||||
|
@ -292,7 +291,7 @@ fn get_action<C, S>(conn: &C, id: Uuid) -> Result<(ActionStatus, S::Action)> whe
|
||||||
fn update_action_status<C, S>(conn: &C,
|
fn update_action_status<C, S>(conn: &C,
|
||||||
id: Uuid,
|
id: Uuid,
|
||||||
status: ActionStatus,
|
status: ActionStatus,
|
||||||
error: Option<Value>,
|
error: Option<String>,
|
||||||
_fsm: PhantomData<S>) -> Result<()> where
|
_fsm: PhantomData<S>) -> Result<()> where
|
||||||
C: GenericConnection,
|
C: GenericConnection,
|
||||||
S: FSM {
|
S: FSM {
|
||||||
|
@ -371,12 +370,26 @@ fn run_action<S>(tx: Transaction, id: Uuid, _fsm: PhantomData<S>)
|
||||||
|
|
||||||
let result = match status {
|
let result = match status {
|
||||||
ActionStatus::Pending => {
|
ActionStatus::Pending => {
|
||||||
let events = <S as FSM>::act(action);
|
match <S as FSM>::act(action) {
|
||||||
update_action_status(
|
// If the action succeeded, update its status to
|
||||||
&tx, id, ActionStatus::Completed, None, PhantomData::<S>
|
// completed and return the created events.
|
||||||
)?;
|
Ok(events) => {
|
||||||
|
update_action_status(
|
||||||
|
&tx, id, ActionStatus::Completed, None, PhantomData::<S>
|
||||||
|
)?;
|
||||||
|
events
|
||||||
|
},
|
||||||
|
|
||||||
events
|
// If the action failed, persist the debug message and
|
||||||
|
// return nothing.
|
||||||
|
Err(err) => {
|
||||||
|
let msg = Some(format!("{:?}", err));
|
||||||
|
update_action_status(
|
||||||
|
&tx, id, ActionStatus::Failed, msg, PhantomData::<S>
|
||||||
|
)?;
|
||||||
|
vec![]
|
||||||
|
},
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use super::*;
|
use super::*;
|
||||||
use finito;
|
|
||||||
use finito_door::*;
|
use finito_door::*;
|
||||||
use postgres::{Connection, TlsMode};
|
use postgres::{Connection, TlsMode};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue