asched-core 0.2.0

Reusable local project routine scheduler, daemon, persistence, and typed client
Documentation
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! Explicit import paths from schedulers that predate asched.
//! ref: README.md#migrating-from-wsx

use crate::routine::store::{
    atomic_create, atomic_toml, project_key, read_text_limited, ProjectRoutines, RoutineStore,
};
use crate::{Project, ProjectRegistry, RegistryError, RegistryStore};
use serde::{Deserialize, Serialize};
use std::fs::{self, File, OpenOptions};
use std::os::fd::AsRawFd;
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};

const WSX_IMPORT_TRANSACTION_VERSION: u32 = 1;

pub fn default_wsx_paths() -> Result<(PathBuf, PathBuf), RegistryError> {
    let root = dirs::config_dir()
        .map(|path| path.join("wsx"))
        .ok_or_else(|| RegistryError::Unavailable("no config directory".into()))?;
    Ok((root.join("routines"), root.join("config.toml")))
}

#[derive(Debug, Clone, Serialize)]
pub struct WsxImportPlan {
    pub source_root: PathBuf,
    pub source_config: PathBuf,
    pub projects: Vec<WsxImportProject>,
}

#[derive(Debug, Clone, Serialize)]
pub struct WsxImportProject {
    pub project: Project,
    pub routine_count: usize,
    pub has_routine_file: bool,
}

#[derive(Debug, Clone, Serialize)]
pub struct WsxImportResult {
    pub registry: ProjectRegistry,
    pub projects_registered: usize,
    pub routine_files_imported: usize,
    pub routines_imported: usize,
}

#[derive(Debug, Deserialize)]
struct WsxConfig {
    #[serde(default)]
    projects: Vec<WsxProject>,
}

#[derive(Debug, Deserialize)]
struct WsxProject {
    name: String,
    path: PathBuf,
}

#[derive(Debug, Serialize, Deserialize)]
struct WsxImportTransaction {
    version: u32,
    registry_revision: u64,
    projects_before: usize,
    routines_imported: usize,
    projects: Vec<Project>,
    files: Vec<WsxImportFile>,
}

#[derive(Debug, Serialize, Deserialize)]
struct WsxImportFile {
    target: PathBuf,
    contents: String,
}

pub fn plan_wsx_import(
    source_root: &Path,
    source_config: &Path,
) -> Result<WsxImportPlan, RegistryError> {
    let text = read_text_limited(source_config).map_err(|error| {
        RegistryError::Io(format!("reading {}: {error}", source_config.display()))
    })?;
    let config: WsxConfig = toml::from_str(&text)
        .map_err(|error| RegistryError::Corrupt(format!("{}: {error}", source_config.display())))?;
    let mut projects = Vec::with_capacity(config.projects.len());
    for source in config.projects {
        let project = Project {
            name: source.name,
            working_dir: source.path,
        }
        .validated()?;
        let routine_file = source_root
            .join("projects")
            .join(format!("{}.toml", project_key(&project.working_dir)));
        let has_routine_file = routine_file.exists();
        let routine_count = if has_routine_file {
            load_source_routines(&routine_file, &project.working_dir)?
                .routines
                .len()
        } else {
            0
        };
        projects.push(WsxImportProject {
            project,
            routine_count,
            has_routine_file,
        });
    }
    projects.sort_by(|left, right| left.project.name.cmp(&right.project.name));
    Ok(WsxImportPlan {
        source_root: source_root.to_path_buf(),
        source_config: source_config.to_path_buf(),
        projects,
    })
}

// ^ [[Crash-Safe wsx Import]]
pub fn apply_wsx_import(
    plan: &WsxImportPlan,
    destination: &RegistryStore,
    keep_enabled: bool,
) -> Result<WsxImportResult, RegistryError> {
    let _guard = destination.exclusive_lock()?;
    let _daemon_guard = DaemonOfflineGuard::acquire(destination.root())?;
    if let Some(result) = recover_wsx_import(destination)? {
        return Ok(result);
    }
    let before = destination.load()?;
    validate_destination(&before, plan, destination.root())?;

    let mut files = Vec::new();
    let mut routines_imported = 0;
    for item in &plan.projects {
        if !item.has_routine_file {
            continue;
        }
        let key = project_key(&item.project.working_dir);
        let source = plan
            .source_root
            .join("projects")
            .join(format!("{key}.toml"));
        let mut config = load_source_routines(&source, &item.project.working_dir)?;
        if !keep_enabled {
            for routine in &mut config.routines {
                routine.enabled = false;
            }
        }
        routines_imported += config.routines.len();
        let target = RoutineStore::new(destination.root().to_path_buf(), &item.project.working_dir)
            .map_err(|error| RegistryError::Validation(error.to_string()))?
            .project_file();
        let contents = toml::to_string_pretty(&config)
            .map_err(|error| RegistryError::Corrupt(error.to_string()))?;
        files.push(WsxImportFile { target, contents });
    }

    let transaction = WsxImportTransaction {
        version: WSX_IMPORT_TRANSACTION_VERSION,
        registry_revision: before.revision,
        projects_before: before.projects.len(),
        routines_imported,
        projects: plan
            .projects
            .iter()
            .map(|item| item.project.clone())
            .collect(),
        files,
    };
    atomic_toml(&wsx_transaction_path(destination.root()), &transaction)
        .map_err(|error| RegistryError::Io(error.to_string()))?;

    for file in &transaction.files {
        if let Err(error) = atomic_create(&file.target, file.contents.as_bytes()) {
            rollback_wsx_import(destination, &transaction)?;
            return Err(if error.kind() == std::io::ErrorKind::AlreadyExists {
                RegistryError::Validation(format!(
                    "refusing to overwrite existing routine file {}",
                    file.target.display()
                ))
            } else {
                RegistryError::Io(error.to_string())
            });
        }
    }
    let registry = match destination.merge_locked(before.revision, transaction.projects.clone()) {
        Ok(registry) => registry,
        // ^ A registry rename may commit even when the following directory sync
        // fails. Re-read the transaction before deciding whether rollback is safe.
        Err(error) => match recover_wsx_import(destination)? {
            Some(result) => return Ok(result),
            None => return Err(error),
        },
    };
    remove_transaction(destination.root())?;
    Ok(WsxImportResult {
        projects_registered: registry.projects.len() - before.projects.len(),
        routine_files_imported: transaction.files.len(),
        routines_imported,
        registry,
    })
}

fn recover_wsx_import(
    destination: &RegistryStore,
) -> Result<Option<WsxImportResult>, RegistryError> {
    let path = wsx_transaction_path(destination.root());
    if !path.exists() {
        return Ok(None);
    }
    let text = read_text_limited(&path)
        .map_err(|error| RegistryError::Io(format!("reading {}: {error}", path.display())))?;
    let transaction: WsxImportTransaction = toml::from_str(&text)
        .map_err(|error| RegistryError::Corrupt(format!("{}: {error}", path.display())))?;
    validate_transaction(destination.root(), &transaction)?;
    let registry = destination.load()?;
    let committed = registry.revision > transaction.registry_revision
        && transaction
            .projects
            .iter()
            .all(|project| registry.projects.iter().any(|stored| stored == project));
    if committed {
        for file in &transaction.files {
            ensure_transaction_file(file)?;
        }
        remove_transaction(destination.root())?;
        Ok(Some(WsxImportResult {
            projects_registered: registry
                .projects
                .len()
                .saturating_sub(transaction.projects_before),
            routine_files_imported: transaction.files.len(),
            routines_imported: transaction.routines_imported,
            registry,
        }))
    } else {
        rollback_wsx_import(destination, &transaction)?;
        Ok(None)
    }
}

fn rollback_wsx_import(
    destination: &RegistryStore,
    transaction: &WsxImportTransaction,
) -> Result<(), RegistryError> {
    validate_transaction(destination.root(), transaction)?;
    for file in &transaction.files {
        if !file.target.exists() {
            continue;
        }
        ensure_transaction_file(file)?;
        fs::remove_file(&file.target).map_err(|error| {
            RegistryError::Io(format!("removing {}: {error}", file.target.display()))
        })?;
    }
    remove_transaction(destination.root())
}

fn ensure_transaction_file(file: &WsxImportFile) -> Result<(), RegistryError> {
    let contents = read_text_limited(&file.target).map_err(|error| {
        RegistryError::Io(format!("reading {}: {error}", file.target.display()))
    })?;
    if contents != file.contents {
        return Err(RegistryError::Corrupt(format!(
            "migration target changed after installation: {}",
            file.target.display()
        )));
    }
    Ok(())
}

fn validate_transaction(
    root: &Path,
    transaction: &WsxImportTransaction,
) -> Result<(), RegistryError> {
    if transaction.version != WSX_IMPORT_TRANSACTION_VERSION {
        return Err(RegistryError::Corrupt(format!(
            "unsupported wsx import transaction schema {}",
            transaction.version
        )));
    }
    for file in &transaction.files {
        let valid = transaction.projects.iter().any(|project| {
            file.target
                == root
                    .join("projects")
                    .join(format!("{}.toml", project_key(&project.working_dir)))
        });
        if !valid {
            return Err(RegistryError::Corrupt(format!(
                "wsx import target is outside the transaction: {}",
                file.target.display()
            )));
        }
    }
    Ok(())
}

fn wsx_transaction_path(root: &Path) -> PathBuf {
    root.join("migrations").join("wsx-import-v1.toml")
}

pub(crate) fn ensure_no_pending_wsx_import(root: &Path) -> Result<(), RegistryError> {
    let path = wsx_transaction_path(root);
    if path.exists() {
        return Err(RegistryError::Unavailable(format!(
            "unfinished wsx import at {}; run 'asched migrate wsx' to recover it before starting the daemon",
            path.display()
        )));
    }
    Ok(())
}

fn remove_transaction(root: &Path) -> Result<(), RegistryError> {
    let path = wsx_transaction_path(root);
    if path.exists() {
        fs::remove_file(&path)
            .map_err(|error| RegistryError::Io(format!("removing {}: {error}", path.display())))?;
        if let Some(parent) = path.parent() {
            File::open(parent)?.sync_all()?;
        }
    }
    Ok(())
}

struct DaemonOfflineGuard(File);

impl DaemonOfflineGuard {
    fn acquire(root: &Path) -> Result<Self, RegistryError> {
        let path = root.join("daemon-v1.lock");
        let file = OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(false)
            .open(&path)
            .map_err(|error| RegistryError::Io(format!("opening {}: {error}", path.display())))?;
        file.set_permissions(fs::Permissions::from_mode(0o600))?;
        let locked = unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) };
        if locked != 0 {
            return Err(RegistryError::Unavailable(
                "stop the asched daemon before importing wsx routines".into(),
            ));
        }
        Ok(Self(file))
    }
}

impl Drop for DaemonOfflineGuard {
    fn drop(&mut self) {
        unsafe {
            libc::flock(self.0.as_raw_fd(), libc::LOCK_UN);
        }
    }
}

fn validate_destination(
    current: &ProjectRegistry,
    plan: &WsxImportPlan,
    destination_root: &Path,
) -> Result<(), RegistryError> {
    for item in &plan.projects {
        if let Some(existing) = current.projects.iter().find(|existing| {
            existing.name == item.project.name || existing.working_dir == item.project.working_dir
        }) {
            if existing != &item.project {
                return Err(RegistryError::Validation(format!(
                    "imported project '{}' conflicts with '{}' ({})",
                    item.project.name,
                    existing.name,
                    existing.working_dir.display()
                )));
            }
        }
        if item.has_routine_file {
            let target = destination_root
                .join("projects")
                .join(format!("{}.toml", project_key(&item.project.working_dir)));
            if target.exists() {
                return Err(RegistryError::Validation(format!(
                    "refusing to overwrite existing routine file {}",
                    target.display()
                )));
            }
        }
    }
    Ok(())
}

fn load_source_routines(path: &Path, working_dir: &Path) -> Result<ProjectRoutines, RegistryError> {
    let text = read_text_limited(path)
        .map_err(|error| RegistryError::Io(format!("reading {}: {error}", path.display())))?;
    let mut config: ProjectRoutines = toml::from_str(&text)
        .map_err(|error| RegistryError::Corrupt(format!("{}: {error}", path.display())))?;
    if !matches!(config.version, 1 | crate::routine::PROJECT_CONFIG_VERSION) {
        return Err(RegistryError::Corrupt(format!(
            "{} uses unsupported routine schema {}",
            path.display(),
            config.version
        )));
    }
    if config.project_path != working_dir {
        return Err(RegistryError::Corrupt(format!(
            "{} stores project {}, expected {}",
            path.display(),
            config.project_path.display(),
            working_dir.display()
        )));
    }
    for routine in &mut config.routines {
        *routine = routine
            .clone()
            .validated()
            .map_err(|error| RegistryError::Corrupt(error.to_string()))?;
    }
    Ok(config)
}

#[cfg(test)]
#[path = "migration_contract_tests.rs"]
mod migration_contract_tests;