Skip to content

Instantly share code, notes, and snippets.

@Xliff
Created October 18, 2022 02:42
Show Gist options
  • Save Xliff/b950e7e0080f3c2e9bac849497814808 to your computer and use it in GitHub Desktop.
Save Xliff/b950e7e0080f3c2e9bac849497814808 to your computer and use it in GitHub Desktop.
A Simple Cron

Here's something I wrote for guifa a couple of days ago. I am thinking of turning it into a simple Rakuish event loop for my projects.

Is this a fairly decent implementation, or is this just NIH for cron?

Please share your thoughts.

my %suppliers;
our %shifts is export;
my %times;

my $master = DateTime.now;

class TimeEvent {
  has $.time is built;
}

my $minute-time = $master;
for <minute hour day week month year> {
  %suppliers{$_} = Supplier::Preserving.new;
  %shifts{$_}    = -> { %suppliers{$_}.Supply };
  %times{$_}     =  $master;
  start loop {
    await Promise.at(
      Instant.from-posix: (
        %times{$_} = %times{$_}.later( |("{ $_ }" => 1) ).truncated-to($_)
      ).posix
    );

    %suppliers{$_}.emit: TimeEvent.new( time => %times{$_} );
  }
}

react {
  whenever %shifts<minute>() -> $e {
    CATCH { default { .message.say } }

    say "Minute shifted for event { $e.gist }";
  }
}
@Xliff
Copy link
Author

Xliff commented Oct 26, 2022

I think I would prefer the second, as it wouldn't impose too much change over the listed code, and would be more flexible. What do you see as the drawbacks of this?

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment