cron icon indicating copy to clipboard operation
cron copied to clipboard

Passing UTCTime to Job

Open joehealy opened this issue 7 years ago • 3 comments

Thanks in advance of my request for a really useful scheduling library.

Is there any scope to extend cron so that the time it has been scheduled for is passed to the job?

Ie so the Job definition becomes something like:

data Job = Job CronSchedule (UTCTime -> IO ())

This way, systems relying on cron can be tested independently of the current wall clock time... I realise this would break plenty of code, but maybe there is a way to add it without doing so?

Maybe something like:

data Job = Job CronSchedule (IO ()) | JobWithTime CronSchedule (UTCTime -> IO ())

I'm using a fork at https://github.com/joehealy/cron/tree/pass_time_to_jobs to achieve this, but without the backwards compatibility.

joehealy avatar Feb 07 '17 05:02 joehealy

I'll take a look at this. It would be a shame if we had to break the constructor to enable this functionality. I'm not that familiar with the Job functionality, someone else added it.

MichaelXavier avatar Feb 09 '17 02:02 MichaelXavier

Ok how about this: what if we change ScheduleT from:

newtype ScheduleT m a = ScheduleT { unSchedule :: StateT Jobs (ExceptT ScheduleError m) a }

to

data JobState = JobState {
    jobs :: [Job]
 , getTime :: IO UTCTime
}

defJobState :: JobState
defJobState = JobState [] getCurrentTime

newtype ScheduleT m a = ScheduleT { unSchedule :: StateT JobState (ExceptT ScheduleError m) a }

Then if you want to inject your own timer you can, otherwise you use the default. What do you think?

MichaelXavier avatar Feb 09 '17 02:02 MichaelXavier

I just realized this doesn't give your jobs the time but it does seem to address the issue of testing independently of wall time.

MichaelXavier avatar Feb 09 '17 02:02 MichaelXavier