Debugging cron on Docker

I had the issue of cronjobs not working (correctly) on my Docker instances.

This is what I did to fix it:

  1. Set correct permissions: chmod 0600 /etc/cron.d/cronjob
  2. Set correct owner: chown root /etc/cron.d/cronjob

When it failed, I could not find the logs of why it failed.
In order to see the output of the failed cronjobs, I installed postfix (because the output of cronjobs is being mailed) and I installed rsyslog

  1. Install postfix:
    apt-get update; apt-get install -y postfix; mkfifo /var/spool/postfix/public/pickup; service postfix restart
  2. Install rsyslog :
    apt-get update; apt-get install -y rsyslog; rsyslogd &

Now, whenever a cronjob failed, I could find output in either two locations:

  1. In the syslog: /var/log/syslog
  2. In the mail sent to root: /var/mail/root

CakePHP SplFileInfo::openFile(../tmp/cache/..) failed to open stream: Permission denied

You need to configure CakePHP to create the cachefiles with the right permissions.

You do this with setting 'mask' => 0666 in file app.php for the Cache setting:

// file src/config/app.php (AND app.default.php!)
...
    /**
     * Configure the cache adapters.
     */
    'Cache' => [
        'default' => [
            'className' => 'Cake\Cache\Engine\FileEngine',
            'path' => CACHE,
            'url' => env('CACHE_DEFAULT_URL', null),
            'mask' => 0666,
        ],

        ...

       '_cake_core_' => [
            'mask' => 0666,
            ...
        ],
       '_cake_model_' => [
            'mask' => 0666,
            ...
        ],
       '_cake_routes_' => [
            'mask' => 0666,
            ...
        ],

        ...

Docker Ubuntu cron doesn’t work

Most probably:

  • cron service is not running
  • your cron-file in /etc/cron.d is not in file mode 0600
  • your cron-file in /etc/cron.d is not owned by root

To fix the above:

service cron start
chmod 0600 /etc/cron.d/<your file>
chown root /etc/cron.d/<your file>

If that does not work, install rsyslog on your container and debug:

apt-get update; apt-get install -y rsyslog
rsyslogd

Now keep track of your /var/log/syslog file. It will log all issues it receives from cron.

I found the answer here: https://stackoverflow.com/a/33734915

Install Docker on Ubuntu 20.04

That’s an easy and quick think to do!

sudo apt update
sudo apt install docker.io

# start docker now, and on system reboot
sudo systemctl start docker
sudo systemctl enable docker

# check the version
docker --version
  Docker version 19.03.8, build afacb8b7f0

Don’t forget to add your user to the docker group, otherwise you’ll get permission denied errors

docker ps
  Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.40/containers/json: dial unix /var/run/docker.sock: connect: permission denied

To add your account to the docker group:

sudo usermod -a -G docker paul

Don’t forget to logout and login again!

Typescript: Not all constituents of type ‘boolean | (() => void)’ are callable

The full error:

This expression is not callable.  Not all constituents of type 'boolean | (() => void)' are callable.    Type 'false' has no call signatures.  TS2349

This is the code that was triggering the error:

export const useLoading = (defaultValue: boolean = false) => {
    const [loading, setLoading ] = useState(defaultValue)

    const startLoading = () => setLoading(true)
    const stopLoading = () => setLoading(false)

    return [
        loading,
        startLoading,
        stopLoading
    ]
}

CAUSE: the return of the array. Having arrays without a specific type gave me this error as wel: Line 0: Parsing error: : Cannot read property ‘map’ of undefined. The solution was to typehint the array.

SOLUTION: don’t return the array just like that, add ‘ as const ` at the end of that return statement.

export const useLoading = (defaultValue: boolean = false) => {
    const [loading, setLoading ] = useState(defaultValue)

    const startLoading = () => setLoading(true)
    const stopLoading = () => setLoading(false)

    return [
        loading,
        startLoading,
        stopLoading
    ] as const
}

This is the post that showed me where to find it: https://github.com/microsoft/TypeScript/issues/35423

Line 0: Parsing error: : Cannot read property ‘map’ of undefined

the error displayed: Cannot read property 'map' of undefined

If you’re using React and typescript, try these few actions:

  1. Reinstall the dependencies: rm -rf node_modules; yarn install
  2. Remove the cache that’s inside the build directory: remove the build directory and restart the node service
  3. This did it for me (and Trat Westerholt) : don’t type your variables/constants as ‘bare’ arrays:

This failed:

// don't do this - a 'bare' array as type
const customers: [] = []

SOLUTION: It was fixed by:

// add a type to the array
const customers: string[] = []

Add typescript to your React project

If you’re working with a Create React App (CRA) and you want to add typescript to it, run the following command:

yarn add \
  typescript \
  @types/node \
  @types/react \
  @types/react-dom \
  @types/react-router-dom

Now, change the file you want to use typescript in to have the extension .tsx

Add toast messages to your React app

It’s nice to have good looking messages in your app. You can have this too with react-toastify.

react-toastify example

To have these toast messages in your app, do the following:

yarn add react-toastify

Then, in your toplevel file (sth like index.js or App.js, etc.);

import React from 'react';

  import { ToastContainer, toast } from 'react-toastify';
  import 'react-toastify/dist/ReactToastify.css';
  
  function App(){
    const notify = () => toast("Wow so easy !");

    return (
      <div>
        <button onClick={notify}>Notify !</button>
        <ToastContainer />
      </div>
    );
  }

NGINX is not showing PHP 500 error log (nor does php-fpm)

Problem

I received a 500-error on my API request to a PHP backend. The result was returned as a 500 html status.

When searching the logs, there was no mention of this 500 error, other than in the access log of nginx.

Where does nginx or php-fpm put the backtrace of PHP errors?

Solution

Turns out, you need to enable catch_workers_output = yes in your www.conf file, typically located over at /etc/php/7.4/fpm/pool.d/

# file /etc/php/7.4/fpm/pool.d/www.conf
...
catch_workers_output = yes
...

The comments above that line explains it pretty well:

; Redirect worker stdout and stderr into main error log. If not set,
; stdout and stderr will be redirected to /dev/null according to
; FastCGI specs.
; Note: on highloaded environement, this can cause some delay in
; the page process time (several ms).
; Default Value: no

>