Skip to content

Commit 3854894

Browse files
committed
Report storage issues
1 parent b64ce44 commit 3854894

File tree

12 files changed

+115
-62
lines changed

12 files changed

+115
-62
lines changed

rust/agama-l10n/src/service.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl Service {
123123
details: None,
124124
source: IssueSource::Config,
125125
severity: IssueSeverity::Error,
126-
kind: "unknown_locale".to_string(),
126+
class: "unknown_locale".to_string(),
127127
});
128128
}
129129

@@ -133,7 +133,7 @@ impl Service {
133133
details: None,
134134
source: IssueSource::Config,
135135
severity: IssueSeverity::Error,
136-
kind: "unknown_keymap".to_string(),
136+
class: "unknown_keymap".to_string(),
137137
});
138138
}
139139

@@ -143,7 +143,7 @@ impl Service {
143143
details: None,
144144
source: IssueSource::Config,
145145
severity: IssueSeverity::Error,
146-
kind: "unknown_timezone".to_string(),
146+
class: "unknown_timezone".to_string(),
147147
});
148148
}
149149

@@ -210,7 +210,7 @@ impl MessageHandler<message::SetConfig<api::l10n::Config>> for Service {
210210
self.config = config;
211211
let issues = self.find_issues();
212212
self.issues
213-
.cast(issue::message::Update::new(Scope::L10n, issues))?;
213+
.cast(issue::message::Set::new(Scope::L10n, issues))?;
214214
self.events
215215
.send(Event::ProposalChanged { scope: Scope::L10n })?;
216216
Ok(())

rust/agama-manager/src/start.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub async fn start(
5050
let issues = issue::start(events.clone(), dbus.clone()).await?;
5151
let progress = progress::start(events.clone()).await?;
5252
let l10n = l10n::start(issues.clone(), events.clone()).await?;
53-
let storage = storage::start(progress.clone(), events.clone(), dbus).await?;
53+
let storage = storage::start(progress.clone(), issues.clone(), events.clone(), dbus).await?;
5454

5555
let service = Service::new(l10n, storage, issues, progress, questions, events);
5656
let handler = actor::spawn(service);

rust/agama-storage/src/client.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
//! Implements a client to access Agama's storage service.
2222
2323
use crate::config::Config;
24+
use agama_utils::api::Issue;
2425
use serde_json::{value::RawValue, Value};
2526
use zbus::{names::BusName, zvariant::OwnedObjectPath, Connection, Message};
2627

@@ -73,27 +74,27 @@ impl Client {
7374

7475
pub async fn get_system(&self) -> Result<Option<Box<RawValue>>, Error> {
7576
let message = self.call("GetSystem", &()).await?;
76-
self.json_from(message)
77+
try_from_message(message)
7778
}
7879

7980
pub async fn get_config(&self) -> Result<Option<Config>, Error> {
8081
let message = self.call("GetConfig", &()).await?;
81-
self.json_from(message)
82+
try_from_message(message)
8283
}
8384

8485
pub async fn get_config_model(&self) -> Result<Option<Box<RawValue>>, Error> {
8586
let message = self.call("GetConfigModel", &()).await?;
86-
self.json_from(message)
87+
try_from_message(message)
8788
}
8889

8990
pub async fn get_proposal(&self) -> Result<Option<Box<RawValue>>, Error> {
9091
let message = self.call("GetProposal", &()).await?;
91-
self.json_from(message)
92+
try_from_message(message)
9293
}
9394

94-
pub async fn get_issues(&self) -> Result<Option<Box<RawValue>>, Error> {
95+
pub async fn get_issues(&self) -> Result<Vec<Issue>, Error> {
9596
let message = self.call("GetIssues", &()).await?;
96-
self.json_from(message)
97+
try_from_message(message)
9798
}
9899

99100
//TODO: send a product config instead of an id.
@@ -119,7 +120,7 @@ impl Client {
119120
model: Box<RawValue>,
120121
) -> Result<Option<Box<RawValue>>, Error> {
121122
let message = self.call("SolveConfigModel", &(model.to_string())).await?;
122-
self.json_from(message)
123+
try_from_message(message)
123124
}
124125

125126
pub async fn set_locale(&self, locale: String) -> Result<(), Error> {
@@ -139,25 +140,24 @@ impl Client {
139140
.await
140141
.map_err(|e| e.into())
141142
}
143+
}
142144

143-
fn json_from<T: serde::de::DeserializeOwned>(
144-
&self,
145-
message: Message,
146-
) -> Result<Option<T>, Error> {
147-
let value: String = message.body().deserialize()?;
148-
if self.is_null(value.as_str()) {
149-
return Ok(None);
150-
}
151-
let json = serde_json::from_str(value.as_str())?;
152-
Ok(Some(json))
153-
}
154-
155-
fn is_null(&self, value: &str) -> bool {
156-
let value = serde_json::from_str::<Value>(value);
157-
match value {
158-
Ok(Value::Null) => true,
159-
Ok(_) => false,
160-
Err(_) => false,
161-
}
145+
fn try_from_message<T: serde::de::DeserializeOwned + Default>(
146+
message: Message,
147+
) -> Result<T, Error> {
148+
let json: String = message.body().deserialize()?;
149+
if is_json_null(&json) {
150+
return Ok(T::default());
151+
}
152+
let value = serde_json::from_str(&json)?;
153+
Ok(value)
154+
}
155+
156+
fn is_json_null(value: &str) -> bool {
157+
let value = serde_json::from_str::<Value>(value);
158+
match value {
159+
Ok(Value::Null) => true,
160+
Ok(_) => false,
161+
Err(_) => false,
162162
}
163163
}

rust/agama-storage/src/message.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// find current contact information at www.suse.com.
2020

2121
use crate::config::Config;
22-
use agama_utils::actor::Message;
22+
use agama_utils::{actor::Message, api::Issue};
2323
use serde_json::value::RawValue;
2424

2525
#[derive(Clone)]
@@ -78,6 +78,13 @@ impl Message for GetProposal {
7878
type Reply = Option<Box<RawValue>>;
7979
}
8080

81+
#[derive(Clone)]
82+
pub struct GetIssues;
83+
84+
impl Message for GetIssues {
85+
type Reply = Vec<Issue>;
86+
}
87+
8188
#[derive(Clone)]
8289
pub struct SetProduct {
8390
pub id: String,

rust/agama-storage/src/monitor.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818
// To contact SUSE LLC about this file by physical or electronic mail, you may
1919
// find current contact information at www.suse.com.
2020

21+
use crate::{
22+
message,
23+
service::{self, Service},
24+
};
2125
use agama_utils::{
2226
actor::Handler,
2327
api::{
2428
event::{self, Event},
2529
Progress, Scope,
2630
},
27-
progress::{self, message},
31+
issue, progress,
2832
};
2933
use serde::Deserialize;
3034
use serde_json;
@@ -40,6 +44,10 @@ pub enum Error {
4044
#[error("Wrong signal data")]
4145
ProgressChangedData,
4246
#[error(transparent)]
47+
Service(#[from] service::Error),
48+
#[error(transparent)]
49+
Issue(#[from] issue::service::Error),
50+
#[error(transparent)]
4351
Progress(#[from] progress::service::Error),
4452
#[error(transparent)]
4553
DBus(#[from] zbus::Error),
@@ -96,19 +104,25 @@ impl From<ProgressData> for Progress {
96104
}
97105

98106
pub struct Monitor {
107+
storage: Handler<Service>,
99108
progress: Handler<progress::Service>,
109+
issues: Handler<issue::Service>,
100110
events: event::Sender,
101111
connection: Connection,
102112
}
103113

104114
impl Monitor {
105115
pub fn new(
116+
storage: Handler<Service>,
106117
progress: Handler<progress::Service>,
118+
issues: Handler<issue::Service>,
107119
events: event::Sender,
108120
connection: Connection,
109121
) -> Self {
110122
Self {
123+
storage,
111124
progress,
125+
issues,
112126
events,
113127
connection,
114128
}
@@ -124,16 +138,16 @@ impl Monitor {
124138
tokio::pin!(streams);
125139

126140
while let Some((_, signal)) = streams.next().await {
127-
self.handle_signal(signal)?;
141+
self.handle_signal(signal).await?;
128142
}
129143

130144
Ok(())
131145
}
132146

133-
fn handle_signal(&self, signal: Signal) -> Result<(), Error> {
147+
async fn handle_signal(&self, signal: Signal) -> Result<(), Error> {
134148
match signal {
135149
Signal::SystemChanged(signal) => self.handle_system_changed(signal)?,
136-
Signal::ProposalChanged(signal) => self.handle_proposal_changed(signal)?,
150+
Signal::ProposalChanged(signal) => self.handle_proposal_changed(signal).await?,
137151
Signal::ProgressChanged(signal) => self.handle_progress_changed(signal)?,
138152
Signal::ProgressFinished(signal) => self.handle_progress_finished(signal)?,
139153
}
@@ -147,10 +161,16 @@ impl Monitor {
147161
Ok(())
148162
}
149163

150-
fn handle_proposal_changed(&self, _signal: ProposalChanged) -> Result<(), Error> {
164+
async fn handle_proposal_changed(&self, _signal: ProposalChanged) -> Result<(), Error> {
151165
self.events.send(Event::ProposalChanged {
152166
scope: Scope::Storage,
153167
})?;
168+
169+
let issues = self.storage.call(message::GetIssues).await?;
170+
self.issues
171+
.call(issue::message::Set::new(Scope::Storage, issues))
172+
.await?;
173+
154174
Ok(())
155175
}
156176

@@ -162,13 +182,14 @@ impl Monitor {
162182
return Err(Error::ProgressChangedData);
163183
};
164184
self.progress
165-
.cast(message::Set::new(progress_data.into()))?;
185+
.cast(progress::message::Set::new(progress_data.into()))?;
166186

167187
Ok(())
168188
}
169189

170190
fn handle_progress_finished(&self, _signal: ProgressFinished) -> Result<(), Error> {
171-
self.progress.cast(message::Finish::new(Scope::Storage))?;
191+
self.progress
192+
.cast(progress::message::Finish::new(Scope::Storage))?;
172193
Ok(())
173194
}
174195

rust/agama-storage/src/service.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ use crate::{
2323
config::Config,
2424
message,
2525
};
26-
use agama_utils::actor::{self, Actor, MessageHandler};
26+
use agama_utils::{
27+
actor::{self, Actor, Handler, MessageHandler},
28+
api::{Issue, Scope},
29+
issue,
30+
};
2731
use async_trait::async_trait;
2832
use serde_json::value::RawValue;
2933

@@ -33,19 +37,31 @@ pub enum Error {
3337
Actor(#[from] actor::Error),
3438
#[error(transparent)]
3539
Client(#[from] client::Error),
40+
#[error(transparent)]
41+
Issue(#[from] issue::service::Error),
3642
}
3743

3844
/// Storage service.
3945
pub struct Service {
46+
issues: Handler<issue::Service>,
4047
client: Client,
4148
}
4249

4350
impl Service {
44-
pub fn new(connection: zbus::Connection) -> Service {
51+
pub fn new(issues: Handler<issue::Service>, connection: zbus::Connection) -> Service {
4552
Self {
53+
issues,
4654
client: Client::new(connection),
4755
}
4856
}
57+
58+
pub async fn start(self) -> Result<Self, Error> {
59+
let issues = self.client.get_issues().await?;
60+
self.issues
61+
.call(issue::message::Set::new(Scope::Storage, issues))
62+
.await?;
63+
Ok(self)
64+
}
4965
}
5066

5167
impl Actor for Service {
@@ -121,6 +137,13 @@ impl MessageHandler<message::GetProposal> for Service {
121137
}
122138
}
123139

140+
#[async_trait]
141+
impl MessageHandler<message::GetIssues> for Service {
142+
async fn handle(&mut self, _message: message::GetIssues) -> Result<Vec<Issue>, Error> {
143+
self.client.get_issues().await.map_err(|e| e.into())
144+
}
145+
}
146+
124147
#[async_trait]
125148
impl MessageHandler<message::SetProduct> for Service {
126149
async fn handle(&mut self, message: message::SetProduct) -> Result<(), Error> {

rust/agama-storage/src/start.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,34 @@
2020

2121
use crate::{
2222
monitor::{self, Monitor},
23-
service::Service,
23+
service::{self, Service},
2424
};
2525
use agama_utils::{
2626
actor::{self, Handler},
2727
api::event,
28-
progress,
28+
issue, progress,
2929
};
3030

3131
#[derive(thiserror::Error, Debug)]
3232
pub enum Error {
3333
#[error(transparent)]
3434
Monitor(#[from] monitor::Error),
35+
#[error(transparent)]
36+
Service(#[from] service::Error),
3537
}
3638

3739
/// Starts the storage service.
38-
///
39-
/// * `dbus`: connection to Agama's D-Bus server.
4040
pub async fn start(
4141
progress: Handler<progress::Service>,
42+
issues: Handler<issue::Service>,
4243
events: event::Sender,
4344
dbus: zbus::Connection,
4445
) -> Result<Handler<Service>, Error> {
45-
let monitor = Monitor::new(progress, events, dbus.clone());
46+
let service = Service::new(issues.clone(), dbus.clone()).start().await?;
47+
let handler = actor::spawn(service);
48+
49+
let monitor = Monitor::new(handler.clone(), progress, issues, events, dbus);
4650
monitor::spawn(monitor)?;
4751

48-
let service = Service::new(dbus);
49-
let handler = actor::spawn(service);
5052
Ok(handler)
5153
}

0 commit comments

Comments
 (0)