1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#![deny(missing_docs)]

//! # Background Jobs Sled Storage
//! _An implementation of the Background Jobs Storage trait based on the Sled embedded database_
//!
//! ### Usage
//! ```rust,ignore
//! use background_jobs::{ServerConfig, sled_storage::Storage};
//! use sled_extensions::{ConfigBuilder, Db};
//!
//! let db = Db::start(ConfigBuilder::default().temporary(true).build())?;
//! let storage = Storage::new(db)?;
//! let queue_handle = ServerConfig::new(storage).thread_count(8).start();
//! ```

use actix_rt::task::{spawn_blocking, JoinError};
use background_jobs_core::{JobInfo, Stats};
use chrono::offset::Utc;
use sled::{Db, Tree};
use uuid::Uuid;

/// The error produced by sled storage calls
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// Error in the database
    #[error("Error in sled extensions, {0}")]
    Sled(#[from] sled::Error),

    /// Error storing or retrieving job info
    #[error("Error transforming job info, {0}")]
    Cbor(#[from] serde_cbor::Error),

    /// Error executing db operation
    #[error("Blocking operation was canceled")]
    Canceled,
}

/// A simple alias for Result<T, Error>
pub type Result<T> = std::result::Result<T, Error>;

#[derive(Clone)]
/// The Sled-backed storage implementation
pub struct Storage {
    id: Tree,
    jobinfo: Tree,
    running: Tree,
    running_inverse: Tree,
    queue: Tree,
    stats: Tree,
    db: Db,
}

#[async_trait::async_trait]
impl background_jobs_core::Storage for Storage {
    type Error = Error;

    async fn generate_id(&self) -> Result<Uuid> {
        let this = self.clone();

        Ok(spawn_blocking(move || {
            let mut uuid;
            while {
                uuid = Uuid::new_v4();

                this.id
                    .compare_and_swap(
                        uuid.as_bytes(),
                        None as Option<&[u8]>,
                        Some(uuid.as_bytes()),
                    )?
                    .is_err()
            } {}

            Ok(uuid) as Result<Uuid>
        })
        .await??)
    }

    async fn save_job(&self, job: JobInfo) -> Result<()> {
        let this = self.clone();

        Ok(spawn_blocking(move || {
            let job_vec = serde_cbor::to_vec(&job)?;

            this.jobinfo.insert(job.id().as_bytes(), job_vec)?;

            Ok(()) as Result<_>
        })
        .await??)
    }

    async fn fetch_job(&self, id: Uuid) -> Result<Option<JobInfo>> {
        let this = self.clone();

        Ok(spawn_blocking(move || {
            if let Some(job_ivec) = this.jobinfo.get(id.as_bytes())? {
                let job: JobInfo = serde_cbor::from_slice(&job_ivec)?;
                Ok(Some(job)) as Result<_>
            } else {
                Ok(None)
            }
        })
        .await??)
    }

    async fn fetch_job_from_queue(&self, queue: &str) -> Result<Option<JobInfo>> {
        let this = self.clone();
        let queue = queue.to_owned();

        Ok(spawn_blocking(move || {
            let mut job;

            let now = Utc::now();

            while {
                let job_opt = this
                    .queue
                    .iter()
                    .filter_map(|res| res.ok())
                    .filter_map(|(id, in_queue)| {
                        if queue.as_bytes() == in_queue.as_ref() {
                            Some(id)
                        } else {
                            None
                        }
                    })
                    .filter_map(|id| this.jobinfo.get(id).ok())
                    .filter_map(|opt| opt)
                    .filter_map(|ivec| serde_cbor::from_slice(&ivec).ok())
                    .find(|job: &JobInfo| job.is_ready(now) && job.is_pending(now));

                job = if let Some(job) = job_opt {
                    job
                } else {
                    return Ok(None);
                };

                this.queue.remove(job.id().as_bytes())?.is_none()
            } {}

            Ok(Some(job)) as Result<Option<JobInfo>>
        })
        .await??)
    }

    async fn queue_job(&self, queue: &str, id: Uuid) -> Result<()> {
        let this = self.clone();
        let queue = queue.to_owned();

        Ok(spawn_blocking(move || {
            if let Some(runner_id) = this.running_inverse.remove(id.as_bytes())? {
                this.running.remove(runner_id)?;
            }

            this.queue.insert(id.as_bytes(), queue.as_bytes())?;

            Ok(()) as Result<_>
        })
        .await??)
    }

    async fn run_job(&self, id: Uuid, runner_id: Uuid) -> Result<()> {
        let this = self.clone();

        Ok(spawn_blocking(move || {
            this.queue.remove(id.as_bytes())?;
            this.running.insert(runner_id.as_bytes(), id.as_bytes())?;
            this.running_inverse
                .insert(id.as_bytes(), runner_id.as_bytes())?;

            Ok(()) as Result<()>
        })
        .await??)
    }

    async fn delete_job(&self, id: Uuid) -> Result<()> {
        let this = self.clone();

        Ok(spawn_blocking(move || {
            this.jobinfo.remove(id.as_bytes())?;
            this.queue.remove(id.as_bytes())?;
            this.id.remove(id.as_bytes())?;

            if let Some(runner_id) = this.running_inverse.remove(id.as_bytes())? {
                this.running.remove(runner_id)?;
            }

            Ok(()) as Result<()>
        })
        .await??)
    }

    async fn get_stats(&self) -> Result<Stats> {
        let this = self.clone();

        let stats = spawn_blocking(move || {
            let stats = if let Some(stats_ivec) = this.stats.get("stats")? {
                bincode::deserialize(&stats_ivec).unwrap_or_default()
            } else {
                Stats::default()
            };

            Ok(stats) as Result<Stats>
        })
        .await??;

        Ok(stats)
    }

    async fn update_stats<F>(&self, f: F) -> Result<()>
    where
        F: Fn(Stats) -> Stats + Send + 'static,
    {
        let this = self.clone();

        Ok(spawn_blocking(move || {
            this.stats.fetch_and_update("stats", move |opt| {
                let stats = if let Some(stats_ivec) = opt {
                    bincode::deserialize(&stats_ivec).unwrap_or_default()
                } else {
                    Stats::default()
                };

                let new_stats = (f)(stats);

                let stats_vec = bincode::serialize(&new_stats).ok()?;
                Some(stats_vec)
            })?;

            Ok(()) as Result<()>
        })
        .await??)
    }
}

impl Storage {
    /// Create a new Storage struct
    pub fn new(db: Db) -> Result<Self> {
        Ok(Storage {
            id: db.open_tree("background-jobs-id")?,
            jobinfo: db.open_tree("background-jobs-jobinfo")?,
            running: db.open_tree("background-jobs-running")?,
            running_inverse: db.open_tree("background-jobs-running-inverse")?,
            queue: db.open_tree("background-jobs-queue")?,
            stats: db.open_tree("background-jobs-stats")?,
            db,
        })
    }
}

impl From<JoinError> for Error {
    fn from(_: JoinError) -> Self {
        Error::Canceled
    }
}