Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
### Changed

- Requires `innmind/foundation:~1.5`
- `Innmind\Cron\Crontab::_invoke()` now returns an `Innmind\Immutable\Attempt<Innmind\Immutable\SideEffect>`
- `Innmind\Cron\Read::_invoke()` now returns an `Innmind\Immutable\Attempt`
- `Innmind\Cron\Schedule\Range` is now declared internal

## 3.2.0 - 2023-09-23

Expand Down
30 changes: 22 additions & 8 deletions src/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Innmind\Immutable\{
Str,
Maybe,
Attempt,
};

/**
Expand All @@ -33,10 +34,7 @@ public function __construct(Schedule $schedule, Command $command)
*/
public static function of(string $value): self
{
return self::maybe($value)->match(
static fn($self) => $self,
static fn() => throw new \DomainException($value),
);
return self::attempt($value)->unwrap();
}

/**
Expand All @@ -45,6 +43,16 @@ public static function of(string $value): self
* @return Maybe<self>
*/
public static function maybe(string $value): Maybe
{
return self::attempt($value)->maybe();
}

/**
* @psalm-pure
*
* @return Attempt<self>
*/
public static function attempt(string $value): Attempt
{
$parts = Str::of($value)
->split(' ')
Expand All @@ -53,15 +61,21 @@ public static function maybe(string $value): Maybe
$command = Str::of(' ')->join($parts->drop(5))->toString();

if ($command === '') {
/** @var Maybe<self> */
return Maybe::nothing();
/** @var Attempt<self> */
return Attempt::error(new \RuntimeException('Job without a command'));
}

return Schedule::maybe(Str::of(' ')->join($parts->take(5))->toString())
$schedule = Str::of(' ')->join($parts->take(5))->toString();

return Schedule::maybe($schedule)
->map(static fn($schedule) => new self(
$schedule,
Command::foreground($command),
));
))
->attempt(static fn() => new \RuntimeException(\sprintf(
'Invalid schedule %s',
$schedule,
)));
}

public function toString(): string
Expand Down
1 change: 1 addition & 0 deletions src/Job/Schedule/Range.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Innmind\Immutable\Str;

/**
* @internal
* @psalm-immutable
*/
final class Range
Expand Down
59 changes: 35 additions & 24 deletions src/Read.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
};
use Innmind\Immutable\{
Sequence,
Str,
Attempt,
Maybe,
Monoid\Concat,
};
Expand All @@ -24,32 +24,28 @@ private function __construct(Command $command)
}

/**
* @return Maybe<Sequence<Job>> Returns nothing when unable to read the crontab
* @return Attempt<Sequence<Job>>
*/
public function __invoke(Server $server): Maybe
public function __invoke(Server $server): Attempt
{
$process = $server->processes()->execute($this->command)->unwrap();
$success = $process->wait()->match(
static fn() => true,
static fn() => false,
);

if (!$success) {
/** @var Maybe<Sequence<Job>> */
return Maybe::nothing();
}

$jobs = $process
->output()
->map(static fn($chunk) => $chunk->data())
->fold(new Concat)
->split("\n")
->filter(static function(Str $line): bool {
return !$line->startsWith('#') && !$line->trim()->empty();
})
return $server
->processes()
->execute($this->command)
->flatMap(
static fn($process) => $process
->wait()
->attempt(static fn($error) => new \RuntimeException($error::class)),
)
->map(
static fn(Str $line) => Job::maybe($line->toString()),
);
static fn($success) => $success
->output()
->map(static fn($chunk) => $chunk->data())
->fold(new Concat)
->split("\n")
->filter(static fn($line) => !$line->startsWith('#') && !$line->trim()->empty())
->map(static fn($line) => Job::attempt($line->toString())),
)
->flatMap(self::parse(...));

/**
* @psalm-suppress NamedArgumentNotAllowed
Expand Down Expand Up @@ -82,4 +78,19 @@ public static function forUser(string $user): self
->withShortOption('l'),
);
}

/**
* @param Sequence<Attempt<Job>> $jobs
*
* @return Attempt<Sequence<Job>>
*/
private static function parse(Sequence $jobs): Attempt
{
/** @var Sequence<Job> */
$parsed = Sequence::of();

return $jobs
->sink($parsed)
->attempt(static fn($parsed, $job) => $job->map($parsed));
}
}
6 changes: 5 additions & 1 deletion tests/Job/Schedule/DaysOfMonthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ public function testRangeOfDaysOfMonthSteppedFromRawString()
public function testReturnNothingWhenUsingRandomString()
{
$this
->forAll(Set::strings()->filter(static fn($value) => !\is_numeric($value)))
->forAll(
Set::strings()
->filter(static fn($value) => !\is_numeric($value))
->filter(static fn($value) => $value !== '*'),
)
->then(function($value) {
$schedule = DaysOfMonth::maybe($value)->match(
static fn($schedule) => $schedule,
Expand Down
6 changes: 5 additions & 1 deletion tests/Job/Schedule/DaysOfWeekTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ public function testRangeOfDaysOfWeekSteppedFromRawString()
public function testReturnNothingWhenUsingRandomString()
{
$this
->forAll(Set::strings()->filter(static fn($value) => !\is_numeric($value)))
->forAll(
Set::strings()
->filter(static fn($value) => !\is_numeric($value))
->filter(static fn($value) => $value !== '*'),
)
->then(function($value) {
$schedule = DaysOfWeek::maybe($value)->match(
static fn($schedule) => $schedule,
Expand Down
6 changes: 5 additions & 1 deletion tests/Job/Schedule/HoursTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ public function testRangeOfHoursSteppedFromRawString()
public function testReturnNothingWhenUsingRandomString()
{
$this
->forAll(Set::strings()->filter(static fn($string) => !\is_numeric($string)))
->forAll(
Set::strings()
->filter(static fn($value) => !\is_numeric($value))
->filter(static fn($value) => $value !== '*'),
)
->then(function($value) {
$schedule = Hours::maybe($value)->match(
static fn($schedule) => $schedule,
Expand Down
6 changes: 5 additions & 1 deletion tests/Job/Schedule/MinutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ public function testRangeOfMinutesSteppedFromRawString()
public function testReturnNothingWhenUsingRandomString()
{
$this
->forAll(Set::strings()->filter(static fn($value) => !\is_numeric($value)))
->forAll(
Set::strings()
->filter(static fn($value) => !\is_numeric($value))
->filter(static fn($value) => $value !== '*'),
)
->then(function($value) {
$schedule = Minutes::maybe($value)->match(
static fn($schedule) => $schedule,
Expand Down
6 changes: 5 additions & 1 deletion tests/Job/Schedule/MonthsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ public function testRangeOfMonthsSteppedFromRawString()
public function testReturnNothingWhenUsingRandomString()
{
$this
->forAll(Set::strings()->filter(static fn($value) => !\is_numeric($value)))
->forAll(
Set::strings()
->filter(static fn($value) => !\is_numeric($value))
->filter(static fn($value) => $value !== '*'),
)
->then(function($value) {
$schedule = Months::maybe($value)->match(
static fn($schedule) => $schedule,
Expand Down
Loading