Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Download files in laravel using Response::download Ask Question. Asked 7 years, 11 months ago. Active 1 year, 2 months ago. Viewed k times. Improve this question. Add a comment. Active Oldest Votes. Try this. Improve this answer.
Anam Anam Any way to return a file download AND update the view? Can I change the file permissions while downloading only? Anam — Eswara Reddy. To define the time at which a job should no longer be attempted, add a retryUntil method to your job class. This method should return a DateTime instance:. Sometimes you may wish to specify that a job may be attempted many times, but should fail if the retries are triggered by a given number of unhandled exceptions as opposed to being released by the release method directly.
To accomplish this, you may define a maxExceptions property on your job class:. In this example, the job is released for ten seconds if the application is unable to obtain a Redis lock and will continue to be retried up to 25 times. However, the job will fail if three unhandled exceptions are thrown by the job. Often, you know roughly how long you expect your queued jobs to take.
For this reason, Laravel allows you to specify a "timeout" value. If a job is processing for longer than the number of seconds specified by the timeout value, the worker processing the job will exit with an error. Typically, the worker will be restarted automatically by a process manager configured on your server. The maximum number of seconds that jobs can run may be specified using the --timeout switch on the Artisan command line:.
You may also define the maximum number of seconds a job should be allowed to run on the job class itself. If the timeout is specified on the job, it will take precedence over any timeout specified on the command line:. Therefore, when using these features, you should always attempt to specify a timeout using their APIs as well. For example, when using Guzzle, you should always specify a connection and request timeout value. If an exception is thrown while the job is being processed, the job will automatically be released back onto the queue so it may be attempted again.
The job will continue to be released until it has been attempted the maximum number of times allowed by your application. The maximum number of attempts is defined by the --tries switch used on the queue:work Artisan command. Alternatively, the maximum number of attempts may be defined on the job class itself. More information on running the queue worker can be found below.
Sometimes you may wish to manually release a job back onto the queue so that it can be attempted again at a later time. You may accomplish this by calling the release method:. By default, the release method will release the job back onto the queue for immediate processing. However, by passing an integer to the release method you may instruct the queue to not make the job available for processing until a given number of seconds has elapsed:.
Occasionally you may need to manually mark a job as "failed". To do so, you may call the fail method:. If you would like to mark your job as failed because of an exception that you have caught, you may pass the exception to the fail method:. Laravel's job batching feature allows you to easily execute a batch of jobs and then perform some action when the batch of jobs has completed executing.
Before getting started, you should create a database migration to build a table to contain meta information about your job batches, such as their completion percentage. This migration may be generated using the queue:batches-table Artisan command:. This trait provides access to a batch method which may be used to retrieve the current batch that the job is executing within:. To dispatch a batch of jobs, you should use the batch method of the Bus facade. Of course, batching is primarily useful when combined with completion callbacks.
So, you may use the then , catch , and finally methods to define completion callbacks for the batch. In this example, we will imagine we are queueing a batch of jobs that each process a given number of rows from a CSV file:. Some tools such as Laravel Horizon and Laravel Telescope may provide more user-friendly debug information for batches if batches are named. To assign an arbitrary name to a batch, you may call the name method while defining the batch:.
If you would like to specify the connection and queue that should be used for the batched jobs, you may use the onConnection and onQueue methods.
All batched jobs must execute within the same connection and queue:. You may define a set of chained jobs within a batch by placing the chained jobs within an array. For example, we may execute two job chains in parallel and execute a callback when both job chains have finished processing:. Sometimes it may be useful to add additional jobs to a batch from within a batched job.
This pattern can be useful when you need to batch thousands of jobs which may take too long to dispatch during a web request. So, instead, you may wish to dispatch an initial batch of "loader" jobs that hydrate the batch with even more jobs:. In this example, we will use the LoadImportBatch job to hydrate the batch with additional jobs. To accomplish this, we may use the add method on the batch instance that may be accessed via the job's batch method:.
This makes it convenient to display information about the batch's completion progress in your application's UI. To retrieve a batch by its ID, you may use the Bus facade's findBatch method:. Sometimes you may need to cancel a given batch's execution. As you may have noticed in previous examples, batched jobs should typically check to see if the batch has been cancelled at the beginning of their handle method:.
When a batched job fails, the catch callback if assigned will be invoked. This callback is only invoked for the first job that fails within the batch. When a job within a batch fails, Laravel will automatically mark the batch as "cancelled". If you wish, you may disable this behavior so that a job failure does not automatically mark the batch as cancelled. This may be accomplished by calling the allowFailures method while dispatching the batch:.
For convenience, Laravel provides a queue:retry-batch Artisan command that allows you to easily retry all of the failed jobs for a given batch. The queue:retry-batch command accepts the UUID of the batch whose failed jobs should be retried:.
To mitigate this, you should schedule the queue:prune-batches Artisan command to run daily:. By default, all finished batches that are more than 24 hours old will be pruned. You may use the hours option when calling the command to determine how long to retain batch data. For example, the following command will delete all batches that finished over 48 hours ago:. You may instruct the queue:prune-batches command to prune these unfinished batch records using the unfinished option:.
Instead of dispatching a job class to the queue, you may also dispatch a closure. This is great for quick, simple tasks that need to be executed outside of the current request cycle.
When dispatching closures to the queue, the closure's code content is cryptographically signed so that it can not be modified in transit:. Using the catch method, you may provide a closure that should be executed if the queued closure fails to complete successfully after exhausting all of your queue's configured retry attempts :. Laravel includes an Artisan command that will start a queue worker and process new jobs as they are pushed onto the queue.
You may run the worker using the queue:work Artisan command. Note that once the queue:work command has started, it will continue to run until it is manually stopped or you close your terminal:. Remember, queue workers, are long-lived processes and store the booted application state in memory. As a result, they will not notice changes in your code base after they have been started. So, during your deployment process, be sure to restart your queue workers. In addition, remember that any static state created or modified by your application will not be automatically reset between jobs.
Alternatively, you may run the queue:listen command. When using the queue:listen command, you don't have to manually restart the worker when you want to reload your updated code or reset the application state; however, this command is significantly less efficient than the queue:work command:. To assign multiple workers to a queue and process jobs concurrently, you should simply start multiple queue:work processes. This can either be done locally via multiple tabs in your terminal or in production using your process manager's configuration settings.
When using Supervisor , you may use the numprocs configuration value. You may also specify which queue connection the worker should utilize. By default, the queue:work command only processes jobs for the default queue on a given connection. A queue worker processes the queued jobs and runs the tasks associated with them just by a simple artisan command. To start the queue worker, run the above artisan command. It will keep on running until it is stopped manually or the terminal is closed.
After it is stopped you have to run it again. It is a long-lived process and stores the booted application state in memory so after any changes to your code, you have to restart the worker. To automatically start the queue worker again, you have to install a process manager like Supervisor to manage queue workers.
Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 2 years, 9 months ago.
Active 2 years ago. Viewed 9k times. Improve this question. Another error could be caused by that stackoverflow.
I use Laravel 5. Theire are no difference in queue:work command — dev strange. Did you test the steps under "Starting Supervisor" in the docs?
0コメント