To debug and ensure that your scheduled task is working correctly, including logging and deleting image files, you can follow these steps:
-
Ensure Logging is Working: First, verify that your
lg()helper function is working correctly outside of the scheduled task. You can do this by calling it directly in a route or controller. -
Check Task Schedule Execution: Ensure that your task schedule is being executed. You can do this by adding a simple log statement at the beginning of the scheduled function.
-
Check Model Events: Ensure that the model events are being triggered correctly. You can add log statements in the
deletedevent to verify this. -
Check Storage Disk Configuration: Ensure that your S3 disk configuration is correct and that you have the necessary permissions to delete files.
Here is an updated version of your code with additional logging to help you debug the issue:
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use App\Models\Image;
use App\Models\Seed;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Schedule;
// Schedule the task
Schedule::call(function () {
Log::info('Scheduled task started');
Image::whereDoesntHaveMorph('imageable', [Seed::class])->delete();
Log::info('Scheduled task completed');
})->everyMinute();
// In your Image model
protected static function boot(){
parent::boot();
// this is to delete image files after deleting the records
static::deleted(function ($model) {
Log::info('Image model deleted event triggered', ['model' => $model]);
lg('deleted', $model); // custom log function
$model->delete_image_sizes();
});
}
private function delete_image_sizes(){
$file_name = $this->data; // image-name.jpg
// original
$file_path = 'images/' . $file_name;
$response = Storage::disk('s3')->delete($file_path);
Log::info('Deleting original image', ['file_path' => $file_path, 'response' => $response]);
lg('', $response);
lg('', $file_path);
// thumb
// $micro_image_name = Str::replaceLast('.', '-micro.', $file_name);
// $micro_image_path = 'images/' . $micro_image_name;
// Storage::disk('s3')->delete($micro_image_path);
}
Steps to Debug:
-
Check Laravel Logs: Look at the
storage/logs/laravel.logfile to see if the log statements are being written. This will help you determine if the scheduled task is running and if the model events are being triggered. -
Check Custom Logs: Ensure that your
lg()helper function is writing to the correct log file. You can add a simple test log statement in a route or controller to verify this. -
Check S3 Configuration: Ensure that your S3 disk configuration in
config/filesystems.phpis correct and that you have the necessary permissions to delete files. -
Run Scheduler Manually: You can run the scheduler manually using the following Artisan command to see if there are any errors:
php artisan schedule:run
By following these steps and adding the additional logging, you should be able to identify where the issue lies and ensure that your scheduled task is working correctly.