Report this

What is the reason for this report?

Laravel scheduled tasks

Posted on April 18, 2023
Axel

By Axel

Hello,

I’m trying to setup scheduled tasks for Laravel. I did follow this guide: https://docs.digitalocean.com/developer-center/how-to-setup-a-job-scheduler-on-digitalocean-app-platform/ but it seems I have no access to my Laraval app code so I don’t understand how I’m supposed to run the php artisan schedule:run command. On your documentation, you mention a curl http://sample-nodejs:8080 example. Does it mean that the app can only be accessed via a private URL?

Thanks



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

The documentation that you’ve shared gives a Node.js sample app as an example. In order for this to work, you need to get the files from the docker-cron repo and use the Dockerfile from the example repo as a blueprint and adjust it for your Laravel project.

That way, rather than running the example curl command from the demo Node app, the Worker component will clone your Laravel project and will have access to all of the necessary files.

Here is an example of an extended Dockerfile which might need some adjustments based on your app:

# Use the official PHP image as the base image
FROM php:8.0-fpm

# Install necessary packages for Laravel, including cron and unzip
RUN apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get -y --no-install-recommends install -y cron curl unzip libpq-dev \
    # Remove package lists for smaller image sizes
    && rm -rf /var/lib/apt/lists/* \
    && docker-php-ext-install pdo pdo_mysql \
    # Install Composer
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Set the working directory for the Laravel app
WORKDIR /var/www

# Copy the Laravel app into the container
COPY . /var/www

# Install the Laravel app dependencies
RUN composer install

# Set proper permissions for the storage and bootstrap/cache directories
RUN chown -R www-data:www-data /var/www/storage /var/www/bootstrap/cache \
    && chmod -R 755 /var/www/storage /var/www/bootstrap/cache

# Copy the crontab file and entrypoint script into the container
COPY crontab /hello-cron
COPY entrypoint.sh /entrypoint.sh

# Install the crontab and make the entrypoint script executable
RUN crontab /hello-cron \
    && chmod +x /entrypoint.sh

# Run the entrypoint script
ENTRYPOINT ["/entrypoint.sh"]

# https://manpages.ubuntu.com/manpages/trusty/man8/cron.8.html
# -f | Stay in foreground mode, don't daemonize.
# -L loglevel | Tell  cron  what to log about jobs (errors are logged regardless of this value) as the sum of the following values:
CMD ["cron", "-f", "-L", "2"]

An alternative option is to use the suggestion here with a standard worker and a while loop:

https://www.digitalocean.com/community/questions/laravel-app-platform-cron-job

Let me know how it goes!

Best,

Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.