Cron tasks, database connections and KnexTimeoutError

I have not found the reason for the problem, but I have reduced it.

First, I discovered that database connections are automatically released. Adding the following this line to .env file: DEBUG=knex:client

Now I see that a connection has been requested and released.

Using pgadmin I can see the server connections. Here the graph shows the total, active and idle connections. When one of my jobs start, I notice a spike in the total number of connections. Interestingly, the number of idle connections follows the spike in total connections.

After a while, the total and idle connections go down again.

One situation is that my job finishes just fine, but when the next one starts the timeout for releasing the connections has not been reached. I think that the time for releasing the connections can be adjusted.

I have also found that Strapi is not following the recommendations from Knex. Strapi is using Knex which, in turn, is using tarn.js for managing the pool of connections. Reading the documentation on Knex, I found something interesting related to how the connections are released. The default value for min pool in Strapi is 2. In Knex doc it says:

Note that the default value of min is 2 only for historical reasons. It can result in problems with stale connections, despite tarn’s default idle connection timeout of 30 seconds, which is only applied when there are more than min active connections. It is recommended to set min: 0 so all idle connections can be terminated.

So I have set min to 0 as recommended by Knex. And I think that Strapi probably should set min to 0 as default as well.

The other workaround I have done is to reduce the requests to the database. I’m using a table, mergeJob, to keep track of the status of cron jobs. This prevents several instances of the same job from running. This table is read into a global array variable (defined in ./config/globals.ts).
So now cron jobs just query the global array variable instead of the database.

To keep the global array variable up to date when changes are done to the mergeJob table, I have added lifecycle functions afterCreate and afterUpdate that update the global array variable.