Set up logging properly

Rocket's logger was the only one, so messages from the daemons depended on
it having launched, and most messages bypassed logging with println!.

- Install env_logger at startup: level from log.level (LOG_LEVEL), with
  RUST_LOG overriding it. Rocket's per-request logs and sqlx's Postgres
  notices are kept to warnings.
- Under systemd, prefix lines with their syslog priority so journald
  records levels (journalctl -p warning works).
- Replace every println! with a log macro at a fitting level.
- Recurring failures are a warning when they start or change, debug while
  they repeat, and an info message when they recover.
- Listenbrainz errors are readable: an unknown user is "Listenbrainz user
  "x" doesn't exist" rather than a JSON decode error, and a period without
  stats yet (204) leaves the playlist alone instead of failing to decode.
  The UI's Refresh shows the same messages.
- Warnings name the playlist or rule they're about.
- Songs missing from the library are debug output, not errors every tick.
- Daemons no longer run twice at startup (interval's first tick is
  immediate), and a failing playlist rule or daemon is logged instead of
  panicking the task.
- Subsonic errors display as messages rather than Debug output.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018opPJMJjLWr4RqdG6vuK6p
This commit is contained in:
Claude committed 2026-11-13 21:48:54 +00:00
1 parent e176ddac6c
commit a594c56abc
14 files changed
+434 -65

No files matched your search

Generated
+176 -13
View File
@@ -47,6 +47,56 @@ dependencies = [
"libc",
]
[[package]]
name = "anstream"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"
[[package]]
name = "anstyle-parse"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
dependencies = [
"anstyle",
"once_cell_polyfill",
"windows-sys 0.61.2",
]
[[package]]
name = "arraydeque"
version = "0.5.1"
@@ -234,6 +284,12 @@ dependencies = [
"windows-targets 0.52.5",
]
[[package]]
name = "colorchoice"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
[[package]]
name = "concurrent-queue"
version = "2.5.0"
@@ -478,6 +534,29 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "env_filter"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "900d271a03799a1ee8d1ca9b19893b48ca674a9284fefcfb85f05e74ed314217"
dependencies = [
"log",
"regex",
]
[[package]]
name = "env_logger"
version = "0.11.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de671bd27a75a797dc9ae289ba1e77276e75e2026408aab65185384e2d5cd3f6"
dependencies = [
"anstream",
"anstyle",
"env_filter",
"jiff",
"log",
]
[[package]]
name = "equivalent"
version = "1.0.1"
@@ -979,12 +1058,53 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "is_terminal_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
[[package]]
name = "itoa"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
[[package]]
name = "jiff"
version = "0.2.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26699905cc795a23e3949585cc7ba3ee8ade090872a27e4316304a777297894a"
dependencies = [
"jiff-core",
"jiff-static",
"log",
"portable-atomic",
"portable-atomic-util",
"serde_core",
]
[[package]]
name = "jiff-core"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffe6854081f5ac0a3046a9b223729ccd7a20b1e0e8277e44dc0660eedf67dfe2"
dependencies = [
"log",
]
[[package]]
name = "jiff-static"
version = "0.2.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b4ff49b2887190c760b86207b55cdcaabbdecaf7a0660d68aa2ed6f652abf0a"
dependencies = [
"jiff-core",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "js-sys"
version = "0.3.83"
@@ -1055,9 +1175,9 @@ dependencies = [
[[package]]
name = "log"
version = "0.4.21"
version = "0.4.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
checksum = "5d15f86d12da44cd8d4ec3c73984537eb7f4cadcb9776a575a0a8a05ca11005c"
[[package]]
name = "loom"
@@ -1256,6 +1376,12 @@ version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
[[package]]
name = "once_cell_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
[[package]]
name = "openssl"
version = "0.10.75"
@@ -1479,6 +1605,7 @@ version = "0.2.6"
dependencies = [
"chrono",
"config",
"env_logger",
"futures-util",
"log",
"md5",
@@ -1494,6 +1621,21 @@ dependencies = [
"uuid",
]
[[package]]
name = "portable-atomic"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85"
[[package]]
name = "portable-atomic-util"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10ab3eb7f3becc3a1cbc4f2c6f20267996cfc1a6467a873763411b136a122715"
dependencies = [
"portable-atomic",
]
[[package]]
name = "powerfmt"
version = "0.2.0"
@@ -1530,9 +1672,9 @@ dependencies = [
[[package]]
name = "quote"
version = "1.0.36"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
dependencies = [
"proc-macro2",
]
@@ -1598,14 +1740,14 @@ dependencies = [
[[package]]
name = "regex"
version = "1.10.4"
version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c"
checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata 0.4.6",
"regex-syntax 0.8.3",
"regex-automata 0.4.18",
"regex-syntax 0.8.11",
]
[[package]]
@@ -1619,13 +1761,13 @@ dependencies = [
[[package]]
name = "regex-automata"
version = "0.4.6"
version = "0.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea"
checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax 0.8.3",
"regex-syntax 0.8.11",
]
[[package]]
@@ -1636,9 +1778,9 @@ checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"
[[package]]
name = "regex-syntax"
version = "0.8.3"
version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56"
checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4"
[[package]]
name = "reqwest"
@@ -2757,6 +2899,12 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "uuid"
version = "1.8.0"
@@ -2936,6 +3084,12 @@ dependencies = [
"windows-targets 0.52.5",
]
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]]
name = "windows-sys"
version = "0.48.0"
@@ -2954,6 +3108,15 @@ dependencies = [
"windows-targets 0.52.5",
]
[[package]]
name = "windows-sys"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
"windows-link",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
+1
View File
@@ -17,6 +17,7 @@ md5 = "0.8.0"
thiserror = "2.0.17"
config = "0.15.19"
log = "0.4.21"
env_logger = "0.11"
chrono = { version = "0.4.38", features = ["serde"] }
rand = "0.8"
serde_json = "1.0"
+11
View File
@@ -40,6 +40,17 @@ podman run --rm \
The package is written to `target/debian/`.
### Logging
playlistd logs to stdout. Set the level with `log.level` in `playlistd.toml` (or `LOG_LEVEL`):
`error`, `warn`, `info` (the default), `debug` or `trace`. `RUST_LOG` overrides it for finer control,
e.g. `RUST_LOG=playlistd=debug` to also see which top songs couldn't be found in your library.
Under systemd, lines are tagged with their priority, so `journalctl -u playlistd -p warning` shows
just the problems. A failure that keeps happening (say, a tracked playlist for a Listenbrainz user
that doesn't exist) is logged as a warning once, and again only if the error changes; recovering
from it is logged too.
## How to use
Each of these will work best when your music library is well organised with Musicbrainz metadata.
+5
View File
@@ -6,6 +6,11 @@ username = "admin"
# Your subsonic password
password = ""
[log]
# error, warn, info (default), debug or trace. RUST_LOG overrides this when set,
# e.g. RUST_LOG=playlistd=debug,rocket=info
# level = "info"
[daemon]
# How often to refresh playlists (in seconds)
interval = 240
+24 -16
View File
@@ -21,7 +21,7 @@ const SECONDS_PER_MONTH: i64 = 30 * 24 * 60 * 60;
pub async fn cluster_mixes_daemon(
media_client: impl AsRef<Subsonic> + Clone,
) -> Result<(), Box<dyn std::error::Error>> {
println!("Cluster mix daemon starting...");
log::info!("Cluster mix daemon starting");
let database = PgPool::connect(
CONFIG
@@ -38,23 +38,30 @@ pub async fn cluster_mixes_daemon(
));
ticker.set_missed_tick_behavior(MissedTickBehavior::Skip);
let mut failures = super::Failures::default();
loop {
// the first tick completes straight away
ticker.tick().await;
match ClusterMixRule::due(&database).await {
Ok(rules) => {
for rule in rules {
let id = rule.id;
if let Err(e) = build_mixes(media_client.as_ref(), rule, &database).await {
println!("Could not build cluster mixes for rule {}: {:?}", id, e);
// failed builds are retried every tick, so don't repeat the same error
let key = rule.id.to_string();
let about = format!("cluster mixes for {}", rule.tracking_user);
match build_mixes(media_client.as_ref(), rule, &database).await {
Ok(()) => failures.succeeded(&key, &about),
Err(e) => {
failures.failed(&key, format!("Could not build {}: {}", about, e))
}
}
}
}
Err(e) => {
println!("Could not fetch cluster_mix_rule: {:?}", e);
log::error!("Could not fetch cluster mix rules: {}", e);
}
}
ticker.tick().await;
}
}
@@ -102,7 +109,7 @@ pub async fn build_mixes(
..Default::default()
},
);
println!(
log::info!(
"{}: {} listens over {} months -> {} clusters",
rule.tracking_user,
listens.len(),
@@ -144,7 +151,7 @@ pub async fn build_mixes(
.await?
{
Some(mix) => mixes.push((mix, cluster)),
None => println!(
None => log::info!(
"No library genres match cluster {:?}",
cluster
.profile
@@ -250,7 +257,7 @@ pub async fn build_mixes(
media_client
.replace_playlist_songs(&playlist_id, mix.song_ids.clone())
.await?;
println!(" {} ({} songs)", name, mix.song_ids.len());
log::info!("Updated mix {:?} ({} songs)", name, mix.song_ids.len());
}
// mixes for tastes that no longer show up in the listening history
@@ -258,7 +265,7 @@ pub async fn build_mixes(
for (o, old) in existing.iter().enumerate() {
if !kept.contains(&o) {
if let Err(e) = media_client.delete_playlist(&old.playlist_id).await {
println!("Could not delete playlist {}: {:?}", old.playlist_id, e);
log::warn!("Could not delete playlist {}: {}", old.playlist_id, e);
}
sqlx::query!(
r#"DELETE FROM "cluster_mix_playlist" WHERE id = $1"#,
@@ -274,10 +281,7 @@ pub async fn build_mixes(
if let Err(e) =
save_suggestions(media_client, &rule, &targets, &recently_played, database).await
{
println!(
"Could not find new songs for {}: {:?}",
rule.tracking_user, e
);
log::warn!("Could not find new songs for {}: {}", rule.tracking_user, e);
}
sqlx::query!(
@@ -353,6 +357,10 @@ async fn save_suggestions(
.execute(database)
.await?;
println!(" {} new songs suggested", suggestions.len());
log::info!(
"{}: {} new songs suggested",
rule.tracking_user,
suggestions.len()
);
Ok(())
}
+13 -8
View File
@@ -10,7 +10,7 @@ use uuid::Uuid;
pub async fn create_playlists_daemon(
media_client: impl AsRef<Subsonic> + Clone,
) -> Result<(), Box<dyn std::error::Error>> {
println!("Create playlist daemon starting...");
log::info!("Playlist rule daemon starting");
let database = PgPool::connect(
CONFIG
@@ -27,7 +27,11 @@ pub async fn create_playlists_daemon(
));
ticker.set_missed_tick_behavior(MissedTickBehavior::Skip);
let mut failures = super::Failures::default();
loop {
// the first tick completes straight away
ticker.tick().await;
let records_res = sqlx::query_as!(
CreateTrackedPlaylist,
r#"SELECT * FROM "create_tracked_playlist""#
@@ -38,19 +42,19 @@ pub async fn create_playlists_daemon(
match records_res {
Ok(records) => {
for record in records {
// todo handle me
create_playlist(media_client.clone(), record, database.clone())
.await
.unwrap();
let key = record.id.to_string();
let about = format!("playlist rule {:?}", record.name_template);
match create_playlist(media_client.clone(), record, database.clone()).await {
Ok(()) => failures.succeeded(&key, &about),
Err(e) => failures.failed(&key, format!("Could not run {}: {}", about, e)),
}
}
}
Err(e) => {
println!("Could not fetch create_tracked_playlist: {:?}", e);
log::error!("Could not fetch playlist rules: {}", e);
}
}
ticker.tick().await;
}
}
@@ -63,6 +67,7 @@ async fn create_playlist(
let new_name = now.format(&playlist_rule.name_template).to_string();
if Some(&new_name) != playlist_rule.last_name.as_ref() {
log::info!("Creating playlist {:?}", new_name);
// Create playlist
let playlist = media_client.as_ref().create_playlist(&new_name).await?;
let uuid = Uuid::new_v4();
+24
View File
@@ -5,3 +5,27 @@ pub mod update_playlists;
pub use cluster_mixes::cluster_mixes_daemon;
pub use create_playlists::create_playlists_daemon;
pub use update_playlists::update_playlists_daemon;
use std::collections::HashMap;
/// Logs a recurring job's failures without repeating the same warning on every run: an error is
/// a warning when it first happens or changes, and debug output while it keeps happening.
#[derive(Default)]
pub struct Failures(HashMap<String, String>);
impl Failures {
pub fn failed(&mut self, key: &str, message: String) {
if self.0.get(key) == Some(&message) {
log::debug!("Still failing: {}", message);
} else {
log::warn!("{}", message);
self.0.insert(key.to_string(), message);
}
}
pub fn succeeded(&mut self, key: &str, what: impl std::fmt::Display) {
if self.0.remove(key).is_some() {
log::info!("{} is working again", what);
}
}
}
+35 -8
View File
@@ -1,8 +1,9 @@
use crate::listenbrainz::ListenbrainzError;
use crate::listenbrainz::StatsRange;
use crate::models::TrackedPlaylist;
use crate::subsonic::Subsonic;
use crate::{listenbrainz, CONFIG};
use log::{error, warn};
use log::{debug, error, info};
use sqlx::postgres::PgPool;
use std::time::Duration;
use thiserror::Error;
@@ -19,7 +20,7 @@ pub enum UpdatePlaylistError {
pub async fn update_playlists_daemon(
media_client: impl AsRef<Subsonic> + Clone,
) -> Result<(), Box<dyn std::error::Error>> {
println!("Track playlist daemon starting...");
info!("Tracked playlist daemon starting");
let database = PgPool::connect(
CONFIG
@@ -36,7 +37,11 @@ pub async fn update_playlists_daemon(
));
ticker.set_missed_tick_behavior(MissedTickBehavior::Skip);
let mut failures = super::Failures::default();
loop {
// the first tick completes straight away
ticker.tick().await;
let records_res = sqlx::query_as!(TrackedPlaylist, "SELECT * FROM \"tracked_playlist\"")
.fetch_all(&database)
.await;
@@ -44,18 +49,39 @@ pub async fn update_playlists_daemon(
match records_res {
Ok(records) => {
for record in records {
if let Err(e) = update_playlist(media_client.clone(), record).await {
warn!("Could not update playlist: {:?}", e);
let key = record.id.to_string();
let about = format!(
"tracked playlist {} (top {}, {}, {})",
record
.playlist_id
.as_deref()
.unwrap_or("without a playlist"),
record.playlist_size,
record.tracking_type,
record.tracking_user
);
match update_playlist(media_client.clone(), record).await {
Ok(()) => failures.succeeded(&key, &about),
// expected at the start of a period, so not worth a warning
Err(e)
if matches!(
e.downcast_ref(),
Some(ListenbrainzError::NoStats { .. })
) =>
{
debug!("Skipped {}: {}", about, e)
}
Err(e) => {
failures.failed(&key, format!("Could not update {}: {}", about, e))
}
}
}
}
Err(e) => {
println!("Could not fetch tracked_playlists: {:?}", e);
error!("Could not fetch tracked playlists: {}", e);
}
}
ticker.tick().await;
}
}
@@ -127,7 +153,8 @@ pub async fn update_playlist(
tracks.push(track.id);
}
None => {
error!(
// songs that aren't in the library are expected
debug!(
"Couldn't find matching track for {} - {}",
recording.track_name, recording.artist_name
)
+1 -1
View File
@@ -87,7 +87,7 @@ pub async fn suggest(
let prompt = format!("tag:({})::or", genres.join(", "));
match listenbrainz::radio(&prompt, "medium", &token).await {
Ok(mbids) => candidates.extend(mbids),
Err(e) => println!("LB Radio failed for {}: {:?}", prompt, e),
Err(e) => log::warn!("LB Radio failed for {}: {}", prompt, e),
}
}
}
+67 -8
View File
@@ -1,6 +1,26 @@
use serde::Deserialize;
use std::collections::HashMap;
use std::fmt::Display;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum ListenbrainzError {
#[error("Listenbrainz user \"{0}\" doesn't exist")]
UserNotFound(String),
#[error("Listenbrainz has no stats for {user} for {range} yet")]
NoStats { user: String, range: &'static str },
#[error("Listenbrainz request failed: {0}")]
Http(#[from] reqwest::Error),
}
/// Requests about a user fail with 404 if the user doesn't exist
fn user_error(user: &str, e: reqwest::Error) -> ListenbrainzError {
if e.status() == Some(reqwest::StatusCode::NOT_FOUND) {
ListenbrainzError::UserNotFound(user.to_string())
} else {
ListenbrainzError::Http(e)
}
}
const BASE: &'static str = "https://api.listenbrainz.org/1";
@@ -49,6 +69,21 @@ impl StatsRange {
}
}
/// e.g. "last month", for messages
pub fn description(&self) -> &'static str {
match self {
StatsRange::ThisWeek => "this week",
StatsRange::ThisMonth => "this month",
StatsRange::ThisYear => "this year",
StatsRange::Week => "last week",
StatsRange::Month => "last month",
StatsRange::Quarter => "last quarter",
StatsRange::HalfYear => "the last six months",
StatsRange::Year => "last year",
StatsRange::All => "all time",
}
}
pub fn from_param(range: &str) -> Option<Self> {
match range {
"this_week" => Some(StatsRange::ThisWeek),
@@ -93,7 +128,8 @@ pub async fn recordings(
user: impl Display,
range: StatsRange,
count: i32,
) -> Result<RecordingsPayload, reqwest::Error> {
) -> Result<RecordingsPayload, ListenbrainzError> {
let user = user.to_string();
let url = format!(
"{}/stats/user/{}/{}?range={}&count={}",
BASE,
@@ -103,11 +139,24 @@ pub async fn recordings(
count
);
Ok(reqwest::get(url)
.await?
let response = reqwest::Client::new()
.get(url)
.header(reqwest::header::USER_AGENT, USER_AGENT)
.send()
.await?;
match response.status() {
reqwest::StatusCode::NOT_FOUND => Err(ListenbrainzError::UserNotFound(user)),
// stats are calculated periodically, so a new period starts out empty
reqwest::StatusCode::NO_CONTENT => Err(ListenbrainzError::NoStats {
user,
range: range.description(),
}),
_ => Ok(response
.error_for_status()?
.json::<RecordingsResponse>()
.await?
.payload)
.payload),
}
}
const USER_AGENT: &str = concat!("playlistd/", env!("CARGO_PKG_VERSION"));
@@ -209,7 +258,11 @@ impl Listen {
}
/// All of a user's listens at or after `min_ts`, newest first
pub async fn listens_since(user: impl Display, min_ts: i64) -> Result<Vec<Listen>, reqwest::Error> {
pub async fn listens_since(
user: impl Display,
min_ts: i64,
) -> Result<Vec<Listen>, ListenbrainzError> {
let user = user.to_string();
let client = reqwest::Client::new();
let mut listens: Vec<Listen> = vec![];
let mut max_ts: Option<i64> = None;
@@ -222,7 +275,11 @@ pub async fn listens_since(user: impl Display, min_ts: i64) -> Result<Vec<Listen
request = request.query(&[("max_ts", max_ts)]);
}
let page = send_json::<ListensResponse>(request).await?.payload.listens;
let page = send_json::<ListensResponse>(request)
.await
.map_err(|e| user_error(&user, e))?
.payload
.listens;
let Some(oldest) = page.last().map(|l| l.listened_at) else {
break;
};
@@ -389,7 +446,8 @@ pub struct Recommendation {
pub async fn recommendations(
user: impl Display,
count: usize,
) -> Result<Vec<Recommendation>, reqwest::Error> {
) -> Result<Vec<Recommendation>, ListenbrainzError> {
let user = user.to_string();
let response = reqwest::Client::new()
.get(format!(
"{}/cf/recommendation/user/{}/recording",
@@ -399,7 +457,8 @@ pub async fn recommendations(
.header(reqwest::header::USER_AGENT, USER_AGENT)
.send()
.await?
.error_for_status()?;
.error_for_status()
.map_err(|e| user_error(&user, e))?;
// 204 when there are no recommendations for the user (yet)
if response.status() == reqwest::StatusCode::NO_CONTENT {
+60 -3
View File
@@ -38,6 +38,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
)
.unwrap();
init_logging(CONFIG.get().unwrap());
let database = PgPool::connect(
CONFIG
.get()
@@ -59,19 +61,25 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
{
let _media_client = media_client.clone();
tokio::spawn(async move {
update_playlists_daemon(_media_client).await.unwrap();
if let Err(e) = update_playlists_daemon(_media_client).await {
log::error!("Tracked playlist daemon stopped: {}", e);
}
});
}
{
let _media_client = media_client.clone();
tokio::spawn(async move {
create_playlists_daemon(_media_client).await.unwrap();
if let Err(e) = create_playlists_daemon(_media_client).await {
log::error!("Playlist rule daemon stopped: {}", e);
}
});
}
{
let _media_client = media_client.clone();
tokio::spawn(async move {
cluster_mixes_daemon(_media_client).await.unwrap();
if let Err(e) = cluster_mixes_daemon(_media_client).await {
log::error!("Cluster mix daemon stopped: {}", e);
}
});
}
@@ -111,6 +119,55 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
/// Log to stdout at `log.level` (default "info"). RUST_LOG overrides it, e.g.
/// `RUST_LOG=playlistd=debug,sqlx=info`.
fn init_logging(config: &Config) {
use log::LevelFilter;
use std::io::Write;
let configured = config.get::<String>("log.level").ok();
let level = configured
.as_deref()
.and_then(|l| l.parse::<LevelFilter>().ok())
.unwrap_or(LevelFilter::Info);
let mut builder = env_logger::Builder::new();
builder.filter_level(level);
// Rocket logs every request at info; keep its warnings and launch messages
builder.filter_module("rocket", level.min(LevelFilter::Warn));
// sqlx logs Postgres notices (e.g. from migrations) at info
builder.filter_module("sqlx", level.min(LevelFilter::Warn));
// libraries that are very chatty at debug
for module in ["hyper", "hyper_util", "reqwest", "rustls", "h2"] {
builder.filter_module(module, level.min(LevelFilter::Info));
}
builder.parse_env("RUST_LOG");
// Under systemd, tag lines with their priority so `journalctl -p warning` works.
// journald adds its own timestamps.
if std::env::var_os("JOURNAL_STREAM").is_some() {
builder.format(|buf, record| {
let priority = match record.level() {
log::Level::Error => 3,
log::Level::Warn => 4,
log::Level::Info => 6,
log::Level::Debug | log::Level::Trace => 7,
};
writeln!(buf, "<{}>{}: {}", priority, record.target(), record.args())
});
}
builder.init();
if let Some(configured) = configured {
if configured.parse::<LevelFilter>().is_err() {
log::warn!(
"Unknown log.level {:?}; use error, warn, info, debug or trace",
configured
);
}
}
}
#[derive(Serialize)]
struct JsonError {
error: String,
+5 -1
View File
@@ -92,7 +92,11 @@ pub enum SubsonicError {
impl Display for SubsonicError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
match self {
SubsonicError::Reqwest(e) => write!(f, "Subsonic request failed: {}", e),
SubsonicError::Api(message) => write!(f, "Subsonic said: {}", message),
SubsonicError::Json(e) => write!(f, "Unexpected response from Subsonic: {}", e),
}
}
}
+3 -3
View File
@@ -53,14 +53,14 @@ fn db_error(e: sqlx::Error) -> Custom<Json<ApiError>> {
match e {
sqlx::Error::RowNotFound => fail(Status::NotFound, "Not found"),
e => {
println!("Database error: {:?}", e);
log::error!("Database error: {}", e);
fail(Status::InternalServerError, "Database error")
}
}
}
fn subsonic_error(e: impl std::fmt::Debug) -> Custom<Json<ApiError>> {
println!("Subsonic error: {:?}", e);
log::error!("Subsonic request failed: {:?}", e);
fail(Status::BadGateway, "Couldn't reach Subsonic")
}
@@ -325,7 +325,7 @@ async fn delete_tracked_playlist(
if let Some(playlist_id) = row.playlist_id {
// it may already be gone from Subsonic
if let Err(e) = media_client.delete_playlist(&playlist_id).await {
println!("Could not delete playlist {}: {:?}", playlist_id, e);
log::warn!("Could not delete playlist {}: {}", playlist_id, e);
}
}
}
+9 -4
View File
@@ -28,7 +28,7 @@ fn db_error(e: sqlx::Error) -> Status {
match e {
sqlx::Error::RowNotFound => Status::NotFound,
e => {
println!("Database error: {:?}", e);
log::error!("Database error: {}", e);
Status::InternalServerError
}
}
@@ -114,7 +114,7 @@ async fn mix(
.get_playlist(&summary.playlist_id)
.await
.map_err(|e| {
println!("Could not load playlist {}: {:?}", summary.playlist_id, e);
log::warn!("Could not load playlist {}: {}", summary.playlist_id, e);
Status::BadGateway
})?
.entry;
@@ -350,7 +350,12 @@ async fn youtube(
.search(&suggestion.artist, &suggestion.title)
.await
.map_err(|e| {
println!("YouTube search failed: {}", e);
log::warn!(
"YouTube search failed for {} - {}: {}",
suggestion.artist,
suggestion.title,
e
);
Status::BadGateway
})?
.ok_or(Status::NotFound)?;
@@ -369,7 +374,7 @@ async fn youtube(
// a cached URL may have expired; fetch a fresh one once
for refresh in [false, true] {
let url = youtube.audio_url(&video_id, refresh).await.map_err(|e| {
println!("Could not get audio for {}: {}", video_id, e);
log::warn!("Could not get audio for YouTube video {}: {}", video_id, e);
Status::BadGateway
})?;
let mut request = youtube.client.get(url);