|
1 | 1 | # Chronic
|
| 2 | + |
2 | 3 | Scheduling thingy for Perl6
|
| 4 | + |
| 5 | +## Synopsis |
| 6 | + |
| 7 | +``` |
| 8 | +
|
| 9 | +# Static configuration; |
| 10 | +
|
| 11 | +use Chronic; |
| 12 | +
|
| 13 | +react { |
| 14 | + # Every minute |
| 15 | + whenever Chronic.every() -> $v { |
| 16 | + say "One: $v"; |
| 17 | + } |
| 18 | + # Every five minutes |
| 19 | + whenever Chronic.every(minute => '*/5') -> $v { |
| 20 | + say "Five: $v"; |
| 21 | + } |
| 22 | + # At 21:31 every day |
| 23 | + whenever Chronic.every(minute => 31, hour => 21) -> $v { |
| 24 | + say "21:31 $v"; |
| 25 | + } |
| 26 | +
|
| 27 | +} |
| 28 | +
|
| 29 | +# Dynamic configuration |
| 30 | +
|
| 31 | +use Chronic; |
| 32 | +
|
| 33 | +my @events = ( |
| 34 | + { |
| 35 | + schedule => {}, |
| 36 | + code => sub ($v) { say "One: $v" }, |
| 37 | + }, |
| 38 | + { |
| 39 | + schedule => { minute => '*/2' }, |
| 40 | + code => sub ($v) { say "Two: $v" }, |
| 41 | + }, |
| 42 | + { |
| 43 | + schedule => { minute => '*/5' }, |
| 44 | + code => sub ($v) { say "Five: $v" }, |
| 45 | + }, |
| 46 | + { |
| 47 | + schedule => { minute => 31, hour => 21 }, |
| 48 | + code => sub ($v) { say "21:31 $v"; }, |
| 49 | + }, |
| 50 | +); |
| 51 | +
|
| 52 | +for @events -> $event { |
| 53 | + Chronic.every(|$event<schedule>).tap($event<code>); |
| 54 | +} |
| 55 | +
|
| 56 | +# This has the effect of waiting forever |
| 57 | +Chronic.supply.wait; |
| 58 | +
|
| 59 | +``` |
| 60 | + |
| 61 | +## Description |
| 62 | + |
| 63 | +This module provides a low-level scheduling mechanism, that be used to |
| 64 | +create cron-like schedules, the specifications can be provided as cron |
| 65 | +expression strings, lists of integer values or L<Junctions> of values. |
| 66 | + |
| 67 | +There is a single class method ```every``` that takes a schedule specification |
| 68 | +and returns a ```Supply``` that will emit a value (a ```DateTime```) on the |
| 69 | +schedule specified. |
| 70 | + |
| 71 | +This can be used to build custom scheduling services like ```cron``` with |
| 72 | +additional code to read the specification from a file and arrange the |
| 73 | +execution of the required thing or it could be used in a larger program |
| 74 | +that may require to execute some code asynchronously periodically. |
| 75 | + |
| 76 | +There is a single base Supply that emits an event at a 1 second frequency |
| 77 | +in order to preserve the accuracy of the timings (in testing it may drift |
| 78 | +by up to 59 seconds on a long run due to system latency if it didn't |
| 79 | +match the seconds too,) so this may be a problem on a heavily loaded |
| 80 | +single core computer. The sub-minute granularity isn't provided for in |
| 81 | +the interface as it is easily achieved anyway with a basic supply, it |
| 82 | +isn't supported by a standard ```cron``` and I think most code that would |
| 83 | +want to be executed with that frequency would be more highly optimised then |
| 84 | +this may allow. |
| 85 | + |
| 86 | + |
| 87 | +## Installation |
| 88 | + |
| 89 | +Assuming you have a working perl6 installation you should be able to |
| 90 | +install this with *ufo* : |
| 91 | + |
| 92 | + ufo |
| 93 | + make test |
| 94 | + make install |
| 95 | + |
| 96 | +*ufo* can be installed with *panda* for rakudo: |
| 97 | + |
| 98 | + panda install ufo |
| 99 | + |
| 100 | +Or you can install directly with "panda": |
| 101 | + |
| 102 | + # From the source directory |
| 103 | + |
| 104 | + panda install . |
| 105 | + |
| 106 | + # Remote installation |
| 107 | + |
| 108 | + panda install Chronic |
| 109 | + |
| 110 | +Other install mechanisms may be become available in the future. |
| 111 | + |
| 112 | +## Support |
| 113 | + |
| 114 | +This should be considered experimental software until such time that |
| 115 | +Perl 6 reaches an official release. However suggestions/patches are |
| 116 | +welcomed via github at |
| 117 | + |
| 118 | + https://github.com/jonathanstowe/Chronic |
| 119 | + |
| 120 | +## Licence |
| 121 | + |
| 122 | +Please see the LICENCE file in the distribution |
| 123 | + |
| 124 | +(C) Jonathan Stowe 2015 |
| 125 | + |
| 126 | + |
| 127 | + |
0 commit comments