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
// Sorceress
// Copyright (C) 2021 Wesley Merkel
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! An ahead-of-time scheduler for executing recurring jobs.
//!
//! This module provides a scheduler for running jobs that send events to the SuperCollider server
//! at, or ahead of, specific times. A [`Scheduler`] creates a [`Job`] from a [`JobDef`] and
//! invokes [`Job::run`] on that job continuously. The interval at which the job is run is
//! determined by the [`Duration`] returned by `Job::run`.
//!
//! # Examples
//!
//! ```no_run
//! use sorceress::scheduler::Scheduler;
//! use std::time::Duration;
//!
//! let mut beats = (0..4).into_iter();
//! Scheduler::new().run(|_| {
//! move |_| {
//! let beat = beats.next()?;
//! println!("beat {}", beat);
//! Some(Duration::from_millis(500))
//! }
//! })?;
//! # sorceress::scheduler::Result::Ok(())
//! ```
//!
//! # Ephemeral Jobs
//!
//! If a job does not need to be snapshotted and restored during live-reloading it can `()` as its
//! [`Snapshot`](Job::Snapshot) type. As a convenience, these jobs can be implemented as closure
//! that takes the initial time and returns a Job, as seen in the example above.
//!
//! # Logical Time
//!
//! The functions [`JobDef::init`] and [`Job::run`] both take a `logical_time` argument. The
//! logical time is kept by the scheduler and provides a time that jobs can use as a basis for
//! timestamps on OSC bundles sent to SuperCollider. The logical time is distinctly different from
//! the _actual time_ that jobs can observe using [`SystemTime::now()`](std::time::SystemTime::now)
//! in a few ways:
//!
//! * Logical time only advances when [`Job::run`] returns a duration, and the logical time always
//! advances by _exactly_ that duration. The logical time is never affected by fluctuations in
//! system performance.
//!
//! * The `logical_time` is usually, and is by default, ahead of the actual time that the job is
//! run. This delay allows time for OSC bundles to be sent to and processed by the SuperCollider
//! server in advance of when the OSC bundles need to be scheduled. This delay can be configured
//! using [`Scheduler::ahead_by`].
//!
//! * The logical time and actual time will vary slightly on top of the `ahead_by` delay due to the
//! imprecise nature of the [`std::thread::sleep`] function used by the scheduler. This amount is
//! on the order of 10ths of milliseconds on a modern desktop computer. If OSC messages are
//! timestamped and sent to the SuperCollider server before they need to be scheduled this slight
//! variation in time will be of no consequence.
use ;
use ;
use Error;
pub use Handle;
/// A specialized [`Result`] type for scheduler errors.
///
/// Most of the functions and methods that can fail in this module return this type.
pub type Result<T> = Result;
/// The error type returned by [`Scheduler`] operations.
;
/// An ahead-of-time scheduler for executing recurring tasks.
;
/// A [`Job`] factory.
///
/// Job definitions are given to [`Scheduler::run`] so that the jobs can be initialized and started
/// some time after `run` is called. The live-reloading functionality provided by sorceress
/// utilizes this delayed initialization.
/// A recurring job.