Add support for multiple cron schedules

This commit is contained in:
Alex Mazanov 2022-10-23 09:47:28 -04:00
parent 1cef69ab84
commit 152fef23dd
No known key found for this signature in database
GPG Key ID: FD35C3C7C1D34AB4
3 changed files with 18 additions and 4 deletions

View File

@ -212,6 +212,13 @@ A special tag can be used as an alternative to refresh interval defined in plugi
<swiftbar.schedule>01,16,31,46 * * * *</swiftbar.schedule>
```
You can configure multiple schedules, using the sepparator `|`:
```
<swiftbar.schedule>1 * * * *|2 * * * *</swiftbar.schedule>
```
#### Other Parameters
* `#<swiftbar.refreshOnOpen>true</swiftbar.refreshOnOpen>` - refreshes plugin on click, before presenting the menu

View File

@ -30,6 +30,8 @@ class ExecutablePlugin: Plugin {
Timer.TimerPublisher(interval: updateInterval, runLoop: .main, mode: .default)
}
var cronTimer: Timer?
var cancellable: Set<AnyCancellable> = []
let prefs = PreferencesStore.shared
@ -67,11 +69,16 @@ class ExecutablePlugin: Plugin {
refresh()
}
// this function called each time plugin updated(manual or scheduled)
func enableTimer() {
// handle cron scheduled plugins
if let nextDate = metadata?.nextDate {
let timer = Timer(fireAt: nextDate, interval: 0, target: self, selector: #selector(scheduledContentUpdate), userInfo: nil, repeats: false)
RunLoop.main.add(timer, forMode: .common)
print("Time: \(nextDate)")
cronTimer?.invalidate()
cronTimer = Timer(fireAt: nextDate, interval: 0, target: self, selector: #selector(scheduledContentUpdate), userInfo: nil, repeats: false)
if let cronTimer {
RunLoop.main.add(cronTimer, forMode: .common)
}
return
}
guard cancellable.isEmpty else { return }

View File

@ -74,8 +74,8 @@ class PluginMetadata: ObservableObject {
}
var nextDate: Date? {
guard let cron = try? SwifCron(schedule) else { return nil }
return try? cron.next()
// parse schedule string and return the minimum date
schedule.components(separatedBy: "|").compactMap { try? SwifCron($0).next() }.reduce(Date.distantFuture, min)
}
init(name: String = "", version: String = "", author: String = "", github: String = "", desc: String = "", previewImageURL: URL? = nil, dependencies: [String] = [], aboutURL: URL? = nil, dropTypes: [String] = [], schedule: String = "", type: PluginType = .Executable, hideAbout: Bool = false, hideRunInTerminal: Bool = false, hideLastUpdated: Bool = false, hideDisablePlugin: Bool = false, hideSwiftBar: Bool = false, environment: [String: String] = [:], runInBash: Bool = true, refreshOnOpen: Bool = false, useTrailingStreamSeparator: Bool = false) {