All services are availabe in app/Services
and contains:
This service developed to mock stores API and can easily changed to a real API client using config file available in its folder. to make request you can use MarketAPI
facade:
use App\Models\Subscription;
use App\Services\MarketAPI\MarketAPI;
$subscription = Subscription::find(1);
$status = MarketAPI::forSubscription($subscription)->checkSubscription();
This module utilize laravel notifications system to send web request when a subscription status changes. it can bind to a model using SendCallback
trait like this:
use App\Services\Callback\CallbackAttributes;
use App\Services\Callback\SendCallback;
class Subscription extends Pivot implements CallbackAttributes
{
use SendCallback;
protected static function booted()
{
static::saving(self::sendCallback());
}
on every change to status of subscription a notification will be sent to specified url and also notifications can be queued and run. notifications will be retried in case of failure.
Worker module contains two jobs which can add subscription checks to queue. when AddSubscriptionsJob
dispached it adds all active subscriptions to queue. but beacuase of large number of records it uses a cursor to reduce memory usage and also includes jobs chunking to prevent high number of jobs add at the same time:
Subscription::active()
->cursor()
->map(fn (Subscription $subscription) => $this->createCheckSubscriptionJob($subscription))
->filter()
->chunk(1000)
->each(function (LazyCollection $jobs) use ($batch) {
$batch->add($jobs);
});
as soon as first chunk added to queue CheckSubscriptionJob
starts for each subscription and checks the status using MarketAPI
. to speed up adding and invoking jobs redis
driver is used. every job in case of failure will be retried twice.
To start woker use:
php artisan horizon
more details are availabe here: Laravel Horizon
- DB schema in SQL fromat is available in
database/db.sql
. - Model factories added for all models
git clone https://github.com/mozafar/masm-api.git masm-api
cd masm-api
composer install
php artisan test