<commit_before>//
//  Cron.swift
//  Swift-Cron
//
//  Created by <NAME> on 2/22/17.
//
//

import Foundation

//Base Cron
public class Cron {
    private var _cronStore: CronStore
    private var _timer: Timer!
    private var _interval: TimeInterval
    
    /// Cron Initialization
    ///
    /// - Parameters:
    ///   - frequency: how often to check for and run cron jobs that have passed their run Date, in seconds
    ///   - cronStorage: Where to store cron jobs (default is in memory, but you can use a pre-made database connector or your own implementation here)
    public init(frequency: TimeInterval = 60, cronStorage: CronStore = MemoryCronStore()) {
        _cronStore = cronStorage
        _interval = frequency
    }
    
    public func start() {
        _timer = Timer.scheduledTimer(timeInterval: _interval, target: self, selector: #selector(run), userInfo: nil, repeats: true)
        RunLoop.current.add(_timer, forMode: .commonModes)
    }
    
    @objc func run() {
        for job in _cronStore.jobs {
            if job.date <= Date() {
                job.method()
                
                _cronStore.remove(job)
                
                if job.repeats {
                    if let interval = job.repeatInterval {
                        job.set(date: Date(timeInterval: interval, since: job.date))
                    }
                    
                    cronStorage.add(job)
                }
                
            }
        }
    }
    <commit_msg>Ground work for disallowing simultaneous jobs<commit_after>//
//  Cron.swift
//  Swift-Cron
//
//  Created by <NAME> on 2/22/17.
//
//

import Foundation

//Base Cron
public class Cron {
    private var _cronStore: CronStore
    private var _runningJobs: [CronJob]
    private var _timer: Timer!
    private var _interval: TimeInterval
    
    /// Cron Initialization
    ///
    /// - Parameters:
    ///   - frequency: how often to check for and run cron jobs that have passed their run Date, in seconds
    ///   - cronStorage: Where to store cron jobs (default is in memory, but you can use a pre-made database connector or your own implementation here)
    public init(frequency: TimeInterval = 60, cronStorage: CronStore = MemoryCronStore()) {
        _cronStore = cronStorage
        _interval = frequency
        _runningJobs = [CronJob]()
    }
    
    public func start() {
        _timer = Timer.scheduledTimer(timeInterval: _interval, target: self, selector: #selector(run), userInfo: nil, repeats: true)
        RunLoop.current.add(_timer, forMode: .commonModes)
    }
    
    @objc func run() {
        for job in _cronStore.jobs {
            if job.allowsSimultaneious || (!job.allowsSimultaneious && !_runningJobs.contains(job)) {
                if job.date <= Date() {
                    
                    DispatchQueue.global(qos: .userInitiated).async {
                        self._runningJobs.append(job)
                        
                        //                    print("Running Job ID: \(job.id)")
                        //                    dump(self._runningJobs)
                        
                        job.method()
                        
                        DispatchQueue.main.async {
                            self._runningJobs = self._runningJobs.filter() { $0 != job }
                            //                        print("Removing Job ID: \(job.id)")
                            //                        dump(self._runningJobs)
                        }
                    }
                    _cronStore.remove(job)
                    
                    if job.repeats {
                        if let interval = job.repeatInterval {
                            job.set(date: Date(timeInterval: interval, since: job.date))
                        }
                        
                        cronStorage.add(job)
                    }
                    
                }
            }
        }
    }
    
{'fraction_non_alphanumeric': 0.08929907707657771, 'fraction_numerical': 0.0037415814417560487, 'mean_word_length': 1.2875071306332002, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2595393', 'n_tokens_mistral': 1086, 'n_tokens_neox': 1002, 'n_words': 279}