Skip to content

Commit 452d643

Browse files
committed
Initial Commit
1 parent 5407a73 commit 452d643

28 files changed

+9517
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.phpunit.result.cache

README.md

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
# Laravel One-Time Operations
2+
3+
Run an operation once after each deployment - just like you do it with migrations!
4+
5+
This package is for you if...
6+
7+
- you often create jobs to use just one single time **after a deployment**
8+
- you sometimes forgot to execute that one specific job and stuff got crazy
9+
- your code gets cluttered with Jobs, that are no longer in use anymore
10+
- you seed or process data in a migration file (which is a big no-no!)
11+
12+
And the best thing: They work as easy as **Laravel migrations**!
13+
14+
## Installation
15+
16+
Require this package with composer:
17+
18+
```shell
19+
composer require timokoerber/laravel-one-time-operations
20+
```
21+
22+
Create the required table in your database:
23+
24+
```shell
25+
php artisan migrate
26+
```
27+
28+
Now you're all set!
29+
30+
## Commands
31+
32+
### Create operation files
33+
```shell
34+
php artisan operations:make <operation_name> // create operation file
35+
```
36+
37+
### Process operations
38+
```shell
39+
php artisan operations:process // process operation files
40+
php artisan operations:process --sync // force syncronously execution
41+
php artisan operations:process --async // force asyncronously execution
42+
php artisan operations:process --test // don't flag operations as "processed"
43+
php artisan operations:process <operation_name> // re-run one specific operation
44+
```
45+
46+
### Show operations
47+
```shell
48+
php artisan operations:show // show all operations
49+
php artisan operations:show pending // show pending operations
50+
php artisan operations:show processed // show processed operations
51+
php artisan operations:show disposed // show disposed operations
52+
53+
php artisan operations:show pending processed disposed // use multiple filters
54+
```
55+
56+
### Delete operation files
57+
```shell
58+
php artisan operations:dispose // delete all operation files (only on local environment)
59+
```
60+
61+
## Tutorials
62+
63+
### Edit config (optional)
64+
65+
By default, the next steps will create the following:
66+
- the table `operations` in your database
67+
- the directory `operations` in your project directory
68+
69+
If you want to use a different settings for _table_ or _directory_ publish the config file:
70+
71+
```shell
72+
php artisan vendor:publish --provider="TimoKoerber\LaravelOneTimeOperations\OneTimeOperationsServiceProvider"
73+
```
74+
75+
This will create the file `config/one-time-operations.php` with the following content.
76+
77+
```php
78+
// config/one-time-operation.php
79+
80+
return [
81+
'directory' => 'operations',
82+
'table' => 'operations',
83+
];
84+
```
85+
86+
Make changes as you like.
87+
88+
### Create One-Time Operation files
89+
90+
To create a new operations file execute the following command:
91+
92+
```shell
93+
php artisan operations:make SeedAclData
94+
```
95+
96+
This will create a file like `operations/XXXX_XX_XX_XXXXXX_seed_acl_data.php` with the following content.
97+
(If you ever used Laravel migrations you should be familiar with the convention)
98+
99+
```php
100+
<?php
101+
// operations/XXXX_XX_XX_XXXXXX_seed_acl_data.php
102+
103+
use TimoKoerber\LaravelOneTimeOperations\OneTimeOperation;
104+
105+
return new class extends OneTimeOperation
106+
{
107+
/**
108+
* Determine if the operation is being processed asyncronously.
109+
*/
110+
protected bool $async = true;
111+
112+
/**
113+
* Process the operation.
114+
*/
115+
public function process(): void
116+
{
117+
//
118+
}
119+
};
120+
121+
```
122+
123+
Provide your code in the `process()` method, for example:
124+
125+
126+
```php
127+
// operations/XXXX_XX_XX_XXXXXX_seed_acl_data.php
128+
129+
public function process(): void
130+
{
131+
(new AclDataSeeder())->run(); // fill acl tables with required data
132+
}
133+
```
134+
135+
By default, the operation is being processed ***asyncronously*** by dispatching the job `OneTimeOperationProcessJob`.
136+
137+
You can also execute the code syncronously by setting the `$async` flag to `false`.
138+
_(this is only recommended for small operations, since the processing of these operations will be part of the deployment process)_
139+
140+
### Processing the operations
141+
142+
Use the following call to process all new operation files.
143+
144+
```shell
145+
php artisan operations:process
146+
```
147+
148+
Your code will be executed and you will find all the processed operations in the `operations` table:
149+
150+
| id | name | dispatched | processed_at |
151+
|-----|--------------------------------|------------|---------------------|
152+
| 1 | XXXX_XX_XX_XXXXXX_seed_acl_data| async | 2015-10-21 07:28:00 |
153+
154+
After that this specific operation will not be processed anymore.
155+
156+
#### Force syncronously/asyncronously execution
157+
158+
By providing the `--sync` or `--async` option, the `$async` flag in all the files will be ignored and the operation will be executed based on the given flag.
159+
160+
```shell
161+
php artisan operations:process --sync
162+
php artisan operations:process --async
163+
```
164+
165+
#### Re-run an operation manually
166+
167+
If something went wrong, you can process an operation manually by providing the **name** of the operation.
168+
This will process the operation again, even if it was processed before.
169+
170+
```shell
171+
php artisan operations:process XXXX_XX_XX_XXXXXX_seed_acl_data
172+
```
173+
174+
#### Testing the operation
175+
176+
You might want to test your code a couple of times before flagging the operation as "processed". Provide the `--test` flag to run the command again and again.
177+
178+
```shell
179+
php artisan operations:process --test
180+
```
181+
182+
### Showing all operations
183+
184+
So you don't have to check the database or the directory for the existing operations,
185+
you can show a list with `operations:show`.
186+
Filter the list with the available filters `pending`, `processed` and `disposed`.
187+
188+
- `pending` - Operations, that have not been processed yet
189+
- `processed` - Operations, that have been processed
190+
- `disposed` - Operations, that have been processed and the files were already deleted (which is okay)
191+
192+
```shell
193+
php artisan operations:show pending // show only pending operations
194+
php artisan operations:show pending disposed // show only pending and disposed operations
195+
```
196+
197+
## Testing
198+
199+
```
200+
composer test
201+
```
202+
203+
## License
204+
205+
Copyright © Timo Körber
206+
207+
Laravel JSON Seeder is open-sourced software licensed under the [MIT license](LICENSE).
208+

composer.json

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"name": "timokoerber/laravel-one-time-operations",
3+
"description": "Run an operation once after each deployment - just like you do it with migrations",
4+
"keywords": [
5+
"laravel",
6+
"deployment",
7+
"migrations",
8+
"operations",
9+
"jobs"
10+
],
11+
"authors": [
12+
{
13+
"name": "timo.koerber",
14+
"email": "[email protected]",
15+
"homepage": "https://www.timokoerber.com",
16+
"role": "Developer"
17+
}
18+
],
19+
"homepage": "https://github.com/timokoerber/laravel-one-time-operations",
20+
"require": {
21+
"php": "^8.0",
22+
"illuminate/support": "^9.0",
23+
"laravel/pint": "^1.6"
24+
},
25+
"require-dev": {
26+
"phpunit/phpunit": "^9.4",
27+
"orchestra/testbench": "7"
28+
},
29+
"minimum-stability": "dev",
30+
"prefer-stable": true,
31+
"autoload": {
32+
"psr-4": {
33+
"TimoKoerber\\LaravelOneTimeOperations\\": "src/"
34+
}
35+
},
36+
"autoload-dev": {
37+
"psr-4": {
38+
"TimoKoerber\\LaravelOneTimeOperations\\Tests\\": "tests",
39+
"TimoKoerber\\LaravelOneTimeOperations\\Tests\\Feature\\": "tests/Feature"
40+
}
41+
},
42+
"scripts": {
43+
"post-autoload-dump": [
44+
"@php vendor/bin/testbench package:discover --ansi"
45+
],
46+
"test": "./vendor/bin/phpunit",
47+
"test-coverage": "./vendor/bin/phpunit --coverage-html coverage"
48+
},
49+
"config": {
50+
"sort-packages": true
51+
},
52+
"extra": {
53+
"laravel": {
54+
"providers": [
55+
"TimoKoerber\\LaravelOneTimeOperations\\Providers\\OneTimeOperationsServiceProvider"
56+
]
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)