Skip to content

Commit

Permalink
Update to first release-ready version
Browse files Browse the repository at this point in the history
  • Loading branch information
a-bashtannik committed Oct 6, 2024
1 parent b2247a8 commit e1245fd
Show file tree
Hide file tree
Showing 23 changed files with 532 additions and 175 deletions.
86 changes: 61 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
⚠️ Work in Progress
Note: This package is currently under development and not ready for production use.
# ⚠️ Work in Progress
#### Note: This package is currently under development and not ready for production use.
--


<p align="center">
<img height="100%" src="assets/cover.png" alt="Laravel Package Skeleton Logo"/>
Expand Down Expand Up @@ -31,7 +33,14 @@ Fasti::schedule($job, '2024-12-31 23:59:59'); // Schedule a job for New Year's E
composer require a-bashtannik/fasti
```

Fasti is built with developers in mind.
Add the tick command to your `console.php` file:

```php
use Bashtannik\Fasti\Console\Commands\FastiTickCommand;

Schedule::command(FastiTickCommand::class)->everyMinute();

```

## Usage

Expand Down Expand Up @@ -83,11 +92,12 @@ Fasti includes a lightweight Eloquent model that stores essential job informatio

use \Bashtannik\Fasti\Facades\Fasti;

Fasti::all(); // Retrieve all scheduled tasks
Fasti::scheduled($at); // Retrieve all tasks scheduled at a specific time
Fasti::cancel($id); // Cancel a scheduled task
Fasti::cancelled(); // Retrieve all canceled tasks
Fasti::find($id); // Retrieve a scheduled task by ID
Fasti::all();
Fasti::schedule($job, $at);
Fasti::scheduled($at); // List all jobs scheduled for a specific time
Fasti::cancel($job);
Fasti::cancelled(); // List all cancelled jobs
Fasti::find($id);

// Or use the Eloquent model directly

Expand All @@ -104,23 +114,38 @@ Test your scheduled jobs with ease using Fasti's `Fasti::fake()` method and buil
```php
use \Bashtannik\Fasti\Facades\Fasti;

Fasti::fake();
// If you are using custom model or repository, avoid using this method and test real storage instead.

Fasti::fake();

// Test if the expected job is scheduled

Fasti::assertScheduled($job);
Fasti::assertNotScheduled($job);

Fasti::assertScheduledAt();
Fasti::assertNotScheduledAt();
// Or by job class name

Fasti::assertScheduled(SendGreetingEmail::class);

Fasti::assertCancelled()
// Add custom assertion by using callback

Fasti::assertScheduled(function ($job) {
return $job->type === 'send_greeting_email';
});

// Many other assertions are available

Fasti::assertNotScheduled($job);
Fasti::assertScheduledAt($job, '2024-12-31 23:59:59');
Fasti::assertNotScheduledAt($job, '2024-12-31 23:59:59');
Fasti::assertCancelled($job);

```

Fasti operates as a scheduling layer for your Laravel jobs, focusing solely on when to initiate them. It's important to note that Fasti doesn't manage job execution, queues, releases, attempts, or failures.

Instead, when the scheduled time arrives, Fasti simply hands off the job to Laravel's default `Bus`.

It means you are free to use well-known Bus assertion methods shipped with Laravel.
It means you are free to use well-known Bus assertion methods shipped with Laravel when the job is dispatched to the queue.

```php

Expand Down Expand Up @@ -149,7 +174,7 @@ class MyOwnModel extends Model implements SchedulableJob

```

Create your own repository that implements the `FastiRepository` interface and bind it in your app service provider `register()` method.
Create your own repository that implements the `FastiScheduledJobsRepository` interface and bind it in your app service provider `register()` method.

```php
use Bashtannik\Fasti\Repositories\FastiRepository;
Expand All @@ -159,20 +184,31 @@ $this->app->bind(
FastiScheduledJobsRepository::class,
MyOwnRepository::class
);
```

Or use more context-aware approach and switch your repository on the fly.

```php
use \Bashtannik\Fasti\Facades\Fasti;
// Or if you just need to switch the model

$repository = new MyOwnRepository(
user: $user,
company: $company,
$this->app->bind(
FastiScheduledJobsRepository::class,
function () {
$repository = new FastiEloquentRepository;
$respository::$model = MyOwnModel::class;

return $repository;
}
);
```

Fasti::setRepository($repository);
### Hint: store human-friendly job type name

When using `FastiEloquentRepository` repository, by default it stores class name in the `type` field.
However, you can define your own set of human-friendly names in your AppServiceProvider like you do with morph-many relationships.

```php
use Bashtannik\Fasti\Repositories\FastiEloquentRepository;

FastiEloquentRepository::enforceTypeMapping([
'fake_job' => FakeJob::class,
]);
```

You are done, now Fasti will use your custom repository to manage scheduled jobs.
This field doesn't affect the job execution but can be useful for debugging or logging purposes.
Binary file added assets/cover1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "a-bashtannik/fasti",
"description": "Laravel task scheduler with calendar-based management.",
"version": "dev-main",
"keywords": [
"laravel",
"package",
Expand Down Expand Up @@ -43,6 +44,16 @@
"Bashtannik\\Fasti\\Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"providers": [
"Bashtannik\\Fasti\\Providers\\FastiServiceProvider"
],
"aliases": {
"Fasti": "Bashtannik\\Fasti\\Facades\\Fasti"
}
}
},
"require": {
"php": "^8.2"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public function up(): void
{
Schema::create('scheduled_jobs', function (Blueprint $table) {
$table->id();
$table->string('type');
$table->longText('payload');
$table->dateTime('scheduled_at');
$table->dateTime('cancelled_at')->nullable();
Expand All @@ -18,6 +19,6 @@ public function up(): void

public function down(): void
{
Schema::dropIfExists('scheduled_tasks');
Schema::dropIfExists('scheduled_jobs');
}
};
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</testsuites>
<source>
<include>
<directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
20 changes: 9 additions & 11 deletions src/Console/Commands/FastiListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,26 @@

class FastiListCommand extends Command
{
protected $signature = 'fasti:list --all';
protected $signature = 'fasti:list';

protected $description = 'List all scheduled jobs, add --all to include cancelled jobs.';

public function handle(FastiService $fasti): void
{
$all = $this->option('all');

$jobs = $all
? $fasti->all()
: $fasti->scheduled(now());
$jobs = $fasti->all();

table(
array_values([
'ID',
'Type',
'Scheduled at',
'Canceled at',
'Cancelled at',
]),
$jobs->only([
'id',
'scheduled_at',
'canceled_at',
$jobs->map(fn ($job): array => [
'id' => $job->id,
'type' => $job->type,
'scheduled_at' => $job->scheduled_at,
'cancelled_at' => $job->cancelled_at ?? '-',
])->toArray()
);
}
Expand Down
12 changes: 12 additions & 0 deletions src/Contracts/JobDispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Bashtannik\Fasti\Contracts;

interface JobDispatcher
{
public function dispatch(mixed $job): mixed;

public function dispatchSync(mixed $job): mixed;
}
1 change: 1 addition & 0 deletions src/Contracts/SchedulableJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* A job that can be scheduled and canceled.
*
* @property int|string $id
* @property string $type
* @property string $payload
* @property CarbonInterface $scheduled_at
* @property CarbonInterface|null $cancelled_at
Expand Down
12 changes: 12 additions & 0 deletions src/Events/JobCancelled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Bashtannik\Fasti\Events;

use Bashtannik\Fasti\Contracts\SchedulableJob;

class JobCancelled
{
public function __construct(public SchedulableJob $job) {}
}
12 changes: 12 additions & 0 deletions src/Events/JobDispatched.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Bashtannik\Fasti\Events;

use Bashtannik\Fasti\Contracts\SchedulableJob;

class JobDispatched
{
public function __construct(public SchedulableJob $job) {}
}
12 changes: 12 additions & 0 deletions src/Events/JobScheduled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Bashtannik\Fasti\Events;

use Bashtannik\Fasti\Contracts\SchedulableJob;

class JobScheduled
{
public function __construct(public SchedulableJob $job) {}
}
Loading

0 comments on commit e1245fd

Please sign in to comment.