Backup failed in Browser, works with php artisan backup:run - laravel

I am new to Laravel, I have created a driver with the create function to create backups to my database on xampp server, but my problem is that in the browser it does not show me any errors but no backup of my database is created. Help me please. Here I leave the code of my create function.
public function create()
{
try {
// start the backup process
Artisan::call('backup:run', ['--only-db'=> true,'--disable-notifications'=> true]);
$output = Artisan::output();
// log the results
Log::info("Backpack\BackupManager -- new backup started from admin interface \r\n" . $output);
// return the results as a response to the ajax call
Alert::success('Nuevo Backup Creado');
return redirect()->back();
} catch (Exception $e) {
Flash::error($e->getMessage());
return redirect()->back();
}
}

Artisan::call('backup:run'); does not work use queue instead of call
working syntax
Artisan::queue('backup:run');

Related

Laravel: Check if new items added to db table and schedule a job to email user

I want to trigger an email when new rows are added to a table in my Laravel application. However I want to add a buffer of sorts, so if 5 rows are added in quick succession then only 1 email is sent.
The method I've chosen is to schedule a check every 15 minutes and see if there are new rows added. If there are then I will queue an email.
Currently I'm getting an error on the schedule. I'll run through my code below:
In Kernel.php where we setup schedules I have:
$schedule->job(new ProcessActivity)
->everyFifteenMinutes()
->when(function () {
return \App\JobItem::whereBetween('created_at', array(Carbon::now()->subMinutes(15), Carbon::now()))->exists();
})
->onSuccess(function () {
Log::debug(
'Success'
);
})
->onFailure(function () {
Log::debug(
'Fail'
);
});
Which I use to trigger the Job found in: App\Jobs\ProcessActivity.php :
public function __construct()
{
$this->jobs = \App\JobItem::whereBetween('created_at', array(Carbon::now()->subMinutes(15), Carbon::now()))->get();
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
Log::debug('Activity Job Run', ['jobs' => $this->jobs]);
$this->jobs->each(function ($item, $key) {
Log::debug('loop');
// get project
$project = $item->project;
// get project email
$user_id = $project->user_id;
$email = \App\User::find($user_id)->email;
// get project UUID
$projectUuid = $project->public_id;
// emails
$subscriberEmails = \App\ProjectSubscription::where('project_id', $project->id)->get();
// create activity email
Notification::route('mail', $subscriberEmails)->notify(new Activity($project, $projectUuid));
});
return true;
}
I've posted my full code above which also shows a relationship between my JobItems and Project models. I won't elaborate on that as I've commented in the code.
The problem
When I add a new row to my JobItem table I can see the job is scheduled and processed (using Laravel Telescope to inspect this).
However, I can also see in my log that for each job I get two log messages:
First: 'Fail' and then 'Activity Job Run'
My email is not sent and I'm uncertain how to determine why this is failing.
So it seems that onFailure is being triggered and there is a problem with my ProcessActivity.
Any clues on where I am going wrong and how to determine the error would be much appreciated.
I have a fix, but first, here are some things I learnt that hampered my progress:
I was using this artisan command to process my scheduled jobs:
php artisan queue:work
The problem with developing while using that command is that if there are code changes then those changes are not recognised.
So you can either Command+C to return to the console and use this every time there is a code change:
php artisan queue:restart
php artisan queue:work
Or you can just use this and it will allow code changes:
php artisan queue:listen
As you can imagine without knowing this you will have a slow debugging process!
As a result of this and adding an exception to my Job I made some progress. I'll paste in the code below to compare against the original code:
public function __construct()
{
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
try {
$jobs = \App\JobItem::whereBetween('created_at', array(Carbon::now()->subMinutes(20), Carbon::now()))->get();
Log::debug('Activity Job', ['jobs' => $jobs]);
// collection start
$collection = collect();
// loop jobs to get emails
foreach ($jobs as $key => $value) {
// get project UUID
$project = $value->project;
$projectUuid = $project->public_id;
// get emails subscribed to projects
$subscriberEmails = \App\ProjectSubscription::where('project_id', $project->id)->get();
// merge into a single collection via the loop
if ($key != 0) {
$merge = $collection->merge($subscriberEmails);
$collection = collect($merge);
} else {
$collection = $subscriberEmails;
}
// Log::debug('emails_project in loop', ['emails' => $subscriberEmails]);
};
// clean object with uniques only
$subscriberEmailsCleaned = $collection->unique();
// debug
Log::debug('Project Emails to Notify', ['emails' => $subscriberEmailsCleaned]);
// create activity email
Notification::route('mail', $subscriberEmailsCleaned)->notify(new Activity($project, $projectUuid));
} catch (\Exception $e) {
\Log::info($e->getMessage());
}
}
First thing to note, is that as __construct() is run initially and is serialised. Then the handle method is called when the job is processed. So I had to move my eloquent query into the handle method.
I also used a foreach rather than .each to loop through and create a new collection of emails. Perhaps there is a more elegant way, but I needed to create a collection of emails and this way allowed me to move the variables in the loop outside to be used in the method.
You can see me merge these at the bottom of the loop.
I have also added a few Log:: items which is useful for debugging.
Not fixed 100%
With this code I can now auto schedule an email every x minutes when new items are added. However, I am still getting the log Fail from the onFailure()from my Kernal.php file:
->onFailure(function () {
Log::debug(
'Fail'
);
I am still confused as to what that indicates and how I can determine more information about how this has failed and what that means. However, it does work so I will cautiously move forward (with one eye open on the comments, in case someone has an idea that can help!)

Return flash message if duplicate id on database

I want to create if else on controller laravel. if no error on database, data insert and return flash message success, and if have error on database (duplicate id) return error message i use laravel 5.3
This is exactly what I want
its my code
you can do try-catch with DB Transaction
try {
DB::beginTransaction();
// your code
// redirect with success message
DB::commit();
}catch (Exception $e) {
DB::rollback();
// other actions
// redirect with error message
}

Image:make(path) Image source not readable when i use artisan custom command

i hope you can help me with my problem.
I have a Laravel application and i wanna optimize all my images.
I use a controller for that, but i have a lot of images, and i get execution_maxim_time_exceed.
I think, the best way to do that is to creat an artisan command.
So, i creat my with php artisan make:command Name command:example.
After, I move my code from controller to handle() from artisan command.
I can use Storage:move($oldPath, $newPath), but i can't use Image:make($filePath).
My file storage is "storage/app/images/image.png" and filePath is 'images/image.png'
When i use the controller, the method isFilePath() from Intervention\Image\AbstractDecoder return true, but in artisan command return false.
Method isFilePath() call the function is_file()
public function isFilePath()
{
if (is_string($this->data)) {
try {
return is_file($this->data);
} catch (\Exception $e) {
return false;
}
}
return false;
}
Why i get false from the function is_file() with the same path for file in artisan command, and in controller i get true? (for the same path).
Thanks
Ok, i found the solution.
In the controller is_file('images/image.png') return true, but in the cli, i need to use the full path
so, in CLI i use 'storage/app/images/image.png'.

Getting Queued Jobs response Laravel 5.2

currently I have the following set up, a route that is calling a function in my controller that is in turn queuing a job.
//My Route
Route::get('/testJob', 'Controller#testJob');
//My Controller
public function testJob()
{
$job = (new testJob())->delay(5);
$this->dispatch($job);
}
//My job
public function handle()
{
require 'testAPICall.php';
// echo $response;
return $response;
}
//testAPICall.php
$response = 'this is the response';
//Queue After
Queue::after(function (JobProcessed $event) {
echo var_dump($event->data);
});
What I would like to be able to do, is access the response returned by the job in Queue::after, or alternatively, pass a callback into the queue to be execute after the job, again with access to the response from the job.
Is this something that is possible with Laravel Queues, and if so how would I go about this?
Cheers, Jack.
Queue::after() is a global callback, that will run after each job. So this might not what you want.
In your case, I would depend on Events/Listeners to be triggered after finishing the job.
public function handle(Mailer $mailer)
{
//Your code
event(new JobDone($data));
}
Please let me know if you need more details for implementation.
I have done something like yours that log a message "queue.txt" in laravel 5 "app" folder
This code I've got from a youtube video and not my code , but I have tested it successfully
First thing you have to code in "Routes.php" as below
Route::get('/',function()
{
//$queue = Queue::push('LogMessage',array('message'=>'Time: '.time()));
$queue = Queue::later(20,'LogMessage',array('message'=>'Time: '.time()));
return $queue;
});
class LogMessage{
public function fire($job,$data){
File::append(app_path().'/queue.txt',$data['message'].PHP_EOL);
$job->delete();
}
}
Then you can run your project folder using "php -S localhost:8888 -t public"
at the same time you must open a another terminal window in windows or linux environment and pointed to same folder and issue the command "php artisan queue:listen"
I think this will be helpful for you!

Throwing errors in Laravel

In my package, before I perform a query on my database I check if some of the params provided are valid:
//check
if(!$this->checkId($id)) //error
//do the query
$user = User::find($id);
What's the best way to handle this in a laravel package?
Throw an error? If so, which kind? Do an app abort?
Using findOrFail()
There is a pretty good method that Laravel provides just for this case called findOrFail().
What you can do is use that and then catch the exception.
try {
$user = User::findOrFail($queryParam);
} catch(ModelNotFoundException $e) {
// Do things that should happen here if your query parameter does not exist.
}
If you do not wish to catch it here, you can setup a listener for this exception. You can place it anywhere you wish as long as it's being autoloaded.
use Illuminate\Database\Eloquent\ModelNotFoundException;
App::error(function(ModelNotFoundException $e)
{
return Response::make('Not Found', 404);
});
Throwing an exception
If all you want to do is throw an error if a query param isn't available, you can throw any of the exceptions found here... https://github.com/symfony/symfony/tree/master/src/Symfony/Component/HttpKernel/Exception
If none suit you, you can create your own and just extend Symfony\Component\HttpKernel\Exception\HttpException and throw new YourCustomException($statusCode, $message);. Just follow what they've done in the other exception classes and it should be fairly easy.
Then you can modify the the "exception listener" to catch YourCustomException.
Here is a tutorial I found... http://fideloper.com/laravel4-error-handling
I would use Event Listeners for the most control.
Create an events.php in your app directory and include it in app/start/global.php
Or, you can register anywhere in your package.
Create a listener:
Event::listen('paramCheck.noId', function($id)
{
// do some logic, log something, return something, throw an error...
});
Then fire the event from whenever you need to:
if(!$this->checkId($id))
$returnFromEvent = Event::fire('paramCheck.noId', array($id));

Resources