Skip to content

Commit 5179139

Browse files
committed
updated readme as per new changes
1 parent 04928cd commit 5179139

File tree

1 file changed

+49
-62
lines changed

1 file changed

+49
-62
lines changed

README.md

Lines changed: 49 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/devsrv/laravel-scheduled-action.svg?style=flat-square)](https://packagist.org/packages/devsrv/laravel-inplace)
44
[![Total Downloads](https://img.shields.io/packagist/dt/devsrv/laravel-scheduled-action.svg?style=flat-square)](https://packagist.org/packages/devsrv/laravel-inplace)
55

6-
Handle scheduled one time or recurring tasks associated with Eloquent models.
6+
Handle scheduled tasks associated with Eloquent models.
77

88
<p align="center"><img src="https://i.ibb.co/QDgztXV/Screen-Recording-09-07-2021-03-14-00-PM.gif" width="480" alt="example app" /></p>
99

1010
> imagine a job portal where if an application gets rejected the app needs to notify the applicant via automated email after 3 days of that event, during that period the admin may change his mind and prevent the auto mail sending or modify the mail content
1111
1212
For any scheduled task we can directly use Laravel's [queue](https://laravel.com/docs/8.x/queues) but what if that task needs to be modified in some way before it gets executed?
1313

14-
This package stores all the tasks that needs to run on a future date & time / recurringly and perform a task only before few moments when it is scheduled to run so that we get the chance to modify the task before it gets executed.
14+
This package stores all the tasks that needs to run on a future date & time and executes each only on the day when it is scheduled to run so that we get the chance to modify the task before it gets executed.
1515

1616
It uses Laravel's task [scheduling](https://laravel.com/docs/8.x/scheduling) to figure out & handle the tasks that needs to be run for the current day at the specified time for that task, and sends the task payload to a [receiver class](https://github.com/devsrv/laravel-scheduled-model-action#step---3--receiver-class-gets-task-payload--passes-the-task-to-classes-based-on-task-action-for-this-example-sending-email) of your app ([configurable](https://github.com/devsrv/laravel-scheduled-model-action#step---3--receiver-class-gets-task-payload--passes-the-task-to-classes-based-on-task-action-for-this-example-sending-email)). So how to perform the task is totally up to you.
1717

@@ -25,7 +25,7 @@ composer require devsrv/laravel-scheduled-action
2525

2626
#### ✔️ publish migrations
2727
```shell
28-
php artisan vendor:publish --provider="Devsrv\ScheduledAction\ActionableServiceProvider" --tag="migrations"
28+
php artisan vendor:publish --provider="Devsrv\ScheduledAction\ScheduledActionServiceProvider" --tag="migrations"
2929
```
3030

3131
#### ✔️ run migrations
@@ -35,7 +35,7 @@ php artisan migrate
3535

3636
#### ✔️ publish config
3737
```shell
38-
php artisan vendor:publish --provider="Devsrv\ScheduledAction\ActionableServiceProvider" --tag="config"
38+
php artisan vendor:publish --provider="Devsrv\ScheduledAction\ScheduledActionServiceProvider" --tag="config"
3939
```
4040

4141
```php
@@ -48,8 +48,6 @@ return [
4848
#### ✔️ Add Scheduled Task to `app/Console/Kernel.php`
4949
```php
5050
$schedule->command('scheduledaction:poll --tasks=10')->hourly(); // poll pending tasks (10 tasks every hour & sends payload to your receiver, customize as per your app)
51-
52-
$schedule->command('scheduledaction:reset')->dailyAt('12:01'); // resets previously finished recurring tasks' status that needs to run today, skip this if your app doesn't need recurring task handling
5351
```
5452

5553
#### ✔️ Use the `HasScheduledAction` trait in your models
@@ -65,12 +63,11 @@ class Candidate extends Model
6563
```
6664

6765
### 💡 Note :
68-
- This package creates two tables `model_actions` and `model_action_recurring`
66+
- This package creates one table `model_actions`
6967
- Every task has 4 satatus `PENDING` `FINISHED` `CANCELLED` `DISPATCHED`
7068
- The `scheduledaction:poll` artisan command polls `PENDING` tasks for the present day and passes the tasks payload to your receiver class.
71-
- Set how often you want the poll to happen of how many tasks needs to be passed to your receiver (the above [example](#%EF%B8%8F-add-scheduled-task-to-appconsolekernelphp) shows 10 per hour)
69+
- Set how often you want the poll to happen and how many tasks needs to be passed to your receiver (the above [example](#%EF%B8%8F-add-scheduled-task-to-appconsolekernelphp) shows 10 per hour)
7270
- `PENDING` tasks gets run at specified date & time, remember to mark the task as `FINISHED` or `CANCELLED` based on how it was handled [check example](#step---4--email-sending-task-payload-gets-received-via-previous-receiver-class-and-mail-is-sent).
73-
- The `scheduledaction:reset` command updates only `FINISHED` recurring tasks to `PENDING` if it needs to run today i.e. resets the recurring tasks which is basically just a query so we can run it once a day
7471
- Most likely you'll use queue to run a task at a specified time so after dispatching to a queued job you might want to set the status as `DISPATCHED`
7572

7673
## There are many fluent methods to interact with the tables
@@ -89,40 +86,40 @@ $model->scheduledActions()->finished()->get();
8986
$model->scheduledActions()->cancelled()->first();
9087
$model->scheduledActions()->pending()->get();
9188
$model->scheduledActions()->dispatched()->paginate();
92-
$model->scheduledActions()->pending()->toActBetween(Carbon $yesterday, Carbon $today)->get();
93-
$model->scheduledActions()->pending()->whereExtraProperty('type', 'info')->get();
94-
$model->scheduledActions()->whereExtraProperty('type', 'success')->get();
95-
$model->scheduledActions()->whereExtraProperties(['type' => 'info', 'applicant' => 27])->get();
89+
$model->scheduledActions()->pending()->toActBetweenTime(Carbon::createFromTimeString('14:00:00'), Carbon::createFromTimeString('16:30:00'))->get();
90+
$model->scheduledActions()->pending()->whereExtraProperty('prefers', 'mail')->get();
91+
$model->scheduledActions()->whereExtraProperty('channel', 'slack')->get();
92+
$model->scheduledActions()->whereExtraProperties(['prefers' => 'mail', 'applicant' => 27])->get();
9693
$model->scheduledActions()->wherePropertyContains('languages', ['en', 'de'])->get();
9794
$model->scheduledActions()->first()->isPending();
9895
$model->scheduledActions()->first()->getExtraProperty('customProperty');
9996

100-
$action = ModelAction::find(1);
97+
$task = ModelAction::find(1);
10198

102-
$action->getExtraProperty('customProperty');
103-
$action->act_at; // 10:22:15
104-
$action->actionable; // associated model
99+
$task->getExtraProperty('customProperty');
100+
$task->act_time;
101+
$task->action;
102+
$task->actionable; // associated model
105103

106-
$action->isPending(); // bool
107-
$action->isFinished(); // bool
108-
$action->isDispatched(); // bool
109-
$action->isCancelled(); // bool
110-
$action->isRecurring(); // bool
104+
$task->isPending; // bool
105+
$task->isFinished; // bool
106+
$task->isDispatched; // bool
107+
$task->isCancelled; // bool
108+
$task->isRecurring; // bool
111109

112110
ModelAction::finished()->get();
113-
ModelAction::recurring()->get();
114-
ModelAction::for($modlel)->pending()->get();
111+
ModelAction::forModel($modlel)->pending()->get();
115112
ModelAction::forClass(Candidate::class)->get();
116113
ModelAction::whereAction('EMAIL')->get();
117114
ModelAction::forClass(Candidate::class)->modelId(10)->get();
118115
ModelAction::modelIdIn([10, 11, 12])->get();
119-
ModelAction::for($modlel)->whereProperty('mailable', \App\Mail\RejectMail::class)->get();
120-
ModelAction::for($modlel)->whereProperties(['type' => 'info', 'applicant' => 27])->get();
116+
ModelAction::forModel($modlel)->whereProperty('mailable', \App\Mail\RejectMail::class)->get();
117+
ModelAction::forModel($modlel)->whereProperties(['type' => 'info', 'applicant' => 27])->get();
121118
ModelAction::wherePropertyContains('languages', ['en', 'de'])->get();
122119
ModelAction::where('properties->type', 'success')->get();
123120

124-
\Devsrv\ScheduledAction\Facades\Action::needsToRunOn(Carbon::now(), 10, 'asc');
125-
\Devsrv\ScheduledAction\Facades\Action::needsToRunToday(10, 'desc');
121+
\Devsrv\ScheduledAction\Facades\Action::needsToRunOn(now()->tomorrow(), 10, 'asc');
122+
\Devsrv\ScheduledAction\Facades\Action::needsToRunToday(3);
126123
```
127124

128125
### Create
@@ -132,31 +129,26 @@ $action = $model->scheduledActions()->create([
132129
'action' => 'EMAIL',
133130
'properties' => ['color' => 'silver'],
134131
'status' => \Devsrv\ScheduledAction\Enums\Status::PENDING,
135-
'recurring' => 1,
136-
'act_at' => Carbon::now()->addDays(3)->setHour(11),
137-
]);
138-
139-
$action->recurringDays()->create([
140-
'day' => \Devsrv\ScheduledAction\Enums\Days::SUNDAY
132+
'act_date' => now(),
133+
'act_time' => now()->setHour(11),
141134
]);
142135

143136
$model->scheduledActions()->createMany([]);
144137

145-
ModelAction::actWith('EMAIL')
146-
->forModel(Candidate::find(10))
147-
->actAt(Carbon::tomorrow()->setHour(11)) // date + time will be set unless runsOnEvery applied
148-
->setExtraProperties(['foo' => 'bar'])
149-
->createSchedule();
150-
151-
ModelAction::actWith('EMAIL')
152-
->forModel(Candidate::find(10))
153-
->actTime(now()->setHour(14)->setMinute(20))
154-
->setExtraProperties(['foo' => 'bar'])
155-
->runsOnEvery([\Devsrv\ScheduledAction\Enums\Days::SUNDAY, \Devsrv\ScheduledAction\Enums\Days::FRIDAY])
156-
->createSchedule();
157-
158-
ModelAction::actWith('EMAIL')->forModel(Candidate::find(10))->actDate($carbon)->actTime($carbon)->createSchedule();
159-
ModelAction::actTime($carbon)->createSchedule();
138+
ModelAction::for(Candidate::find(10))
139+
->actWith('EMAIL')
140+
->actAt(Carbon::tomorrow()->setHour(11)) // date + time will be set together
141+
->setExtraProperties(['foo' => 'bar'])
142+
->createSchedule();
143+
144+
ModelAction::for($model)
145+
->actWith('EMAIL')
146+
->actDate($actDate)
147+
->actTime(Carbon::createFromTimeString('20:00:00'))
148+
->setExtraProperties(['foo' => 'bar'])
149+
->createSchedule();
150+
151+
ModelAction::for($model)->actWith('EMAIL')->actTime($carbon)->asDispatched()->createSchedule();
160152
```
161153

162154
### Update
@@ -168,15 +160,11 @@ $action->setDispatched()->save();
168160
$action->setFinished()->save(); // default sets finished_at as now()
169161
$action->setFinished($carbon)->save(); // set a finished_at time
170162
$action->mergeExtraProperties(['key' => 'val'])->save(); // merges extra properties
171-
$action->withExtraProperties([])->save();/
172-
$action->setActOn($carbon)->save(); // date and time will be extracted
163+
$action->withExtraProperties([])->save(); // overwrites existing
164+
$action->setPending()->setActAt($actAt)->save();
165+
$action->setActAt($carbon)->save(); // date and time will be extracted
173166
$action->setActDate($carbon)->save(); // only date will be used
174167
$action->setActTime($carbon)->save(); // only time will be used
175-
176-
$action->markAsRecurringRunsOnEvery([Days::SUNDAY, ..]); // stores only the given days removeing any existing day that was previously set as recurring day
177-
$action->syncWithoutDetachingRunsOnEvery([Days::SUNDAY, ..]); // doesnt remove anything yet adds any new days given
178-
179-
$action->setNonRecurring()->setActOn($carbon)->save();
180168
```
181169

182170
## Example
@@ -186,13 +174,12 @@ $action->setNonRecurring()->setActOn($carbon)->save();
186174
<summary>Some event happend and a task is created to execute on future day & time</summary>
187175

188176
```php
189-
ModelAction::actWith('MAIL')
190-
->forModel($application)
177+
ModelAction::for($application)
178+
->actWith('MAIL')
191179
->actAt($inThreeDays)
192180
->setExtraProperties([
193-
'mailable' => RejectApplication::class,
194-
'template' => $template,
195-
'role' => $application->job->role
181+
'mailable' => ApproveApplication::class,
182+
'template' => $template
196183
])
197184
->createSchedule();
198185
```
@@ -208,7 +195,7 @@ public function modifyScheduledTask() {
208195

209196
$this->task
210197
->setActDate(Carbon::createFromFormat('m/d/Y', $this->act_date))
211-
->setActTime(Carbon::createFromFormat('H:i:s', $this->act_time))
198+
->setActTime(Carbon::createFromTimeString($this->act_time))
212199
->mergeExtraProperties([
213200
'template' => $this->templateid,
214201
'extra_data' => $this->role

0 commit comments

Comments
 (0)