opennextjs / opennextjs-netlify

Open Next.js adapter for Netlify
https://opennext.js.org/netlify
679 stars 87 forks source link

[Bug]: Scheduled api routes don't seem to run #2112

Closed RohitRajendran closed 7 months ago

RohitRajendran commented 1 year ago

Summary

I've enabled scheduled api routes for one of my api routes. I can see the function in the functions tab for Netlify with the correct schedule and the logs seem to indicate that it has run but I'm not seeing any indication that it actually has. None of the console logs I've added inside the function appear in the logs nor does it seem to be doing what it's supposed to. Any idea why or how to further debug this? I've tried running it locally and it works fine.

Logs: May 14, 08:00:08 AM: faa69117 INFO - info Loaded env from /var/task/.env.production May 14, 08:00:08 AM: faa69117 INFO [POST] /.netlify/functions/_api_user-subscription-analysis-handler (API) May 14, 08:00:13 AM: faa69117 Duration: 3567.34 ms Memory Usage: 175 MB Init Duration: 634.92 ms

Steps to reproduce

Haven't tried reproducing in a clean repo yet but general steps:

  1. Create an api route that's supposed to run on a schedule
  2. Deploy
  3. See if it actually runs and does what it is supposed to

A link to a reproduction repository

https://github.com/RohitRajendran/runtime-issue-repro

Next Runtime version

4.36.1

Is your issue related to the app directory (beta)?

More information about your build

What OS are you using?

None

Your netlify.toml file

`netlify.toml` ```toml # Paste content of your `netlify.toml` file here ```

Your public/_redirects file

`_redirects` ``` # Paste content of your `_redirects` file here ```

Your next.config.js file

`next.config.js` ```js /** @type {import('next').NextConfig} */ /* eslint-disable @typescript-eslint/no-var-requires */ const { withSentryConfig } = require('@sentry/nextjs'); const withRoutes = require('nextjs-routes/config')({ outDir: 'src/types', }); const securityHeaders = [ { key: 'X-DNS-Prefetch-Control', value: 'on', }, { key: 'Strict-Transport-Security', value: 'max-age=63072000; includeSubDomains; preload', }, { key: 'X-XSS-Protection', value: '1; mode=block', }, { key: 'X-Frame-Options', value: 'SAMEORIGIN', }, { key: 'X-Content-Type-Options', value: 'nosniff', }, { key: 'Referrer-Policy', value: 'origin-when-cross-origin', }, ]; const nextConfig = { pageExtensions: ['page.tsx', 'api.ts'], async headers() { return [ { // Apply these headers to all routes in your application. source: '/:path*', headers: securityHeaders, }, ]; }, sentry: { // Use `hidden-source-map` rather than `source-map` as the Webpack `devtool` // for client-side builds. (This will be the default starting in // `@sentry/nextjs` version 8.0.0.) See // https://webpack.js.org/configuration/devtool/ and // https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/#use-hidden-source-map // for more information. hideSourceMaps: true, }, }; module.exports = async () => { /** * @type {import('next').NextConfig} */ const plugins = [ withRoutes, (config) => withSentryConfig(config, { silent: true, // For all available options, see: // https://github.com/getsentry/sentry-webpack-plugin#options. }), ]; return plugins.reduce((acc, next) => next(acc), nextConfig); }; ```

Builds logs (or link to your logs)

Build logs [Build logs link](https://app.netlify.com/sites/should-i-refinance-yet/deploys/6460f71bf6a2580009323f48) [Build logs for minimal reproduction repo](https://app.netlify.com/sites/stalwart-truffle-9f47de/functions/_api_hello-handler)

Function logs

Function logs [Function logs link](https://app.netlify.com/sites/should-i-refinance-yet/functions/_api_user-subscription-analysis-handler)

.next JSON files

generated .next JSON files ``` # Paste file contents here. Please check there isn't any private info in them # You can either build locally, or download the deploy from Netlify by clicking the arrow next to the deploy time. ```
LekoArts commented 1 year ago

Hi!

Please provide a minimal reproduction and deploy it to Netlify so that we can check both the code & logs.

Thanks!

RohitRajendran commented 1 year ago

Hey @LekoArts! You can find a minimal reproduction here and logs here.

RohitRajendran commented 1 year ago

Any suggestions on what to do or how to debug? I still haven't been able to get this scheduled functions to work.

RohitRajendran commented 1 year ago

@LekoArts @nickytonline - I've still been having issues with this. Any pointers on how to debug or fix? Happy to help investigate but not exactly sure how to debug with scheduled functions.

MarcL commented 7 months ago

Hey @RohitRajendran.

The team at Netlify have now released the new v5 Next.js runtime which has support for both pages and app router, on-demand and time-based revalidation, automatic fine-grained cache control, and automatic image optimization using Netlify's image CDN.

We're not currently supporting the scheduled/background function Next API routes in the new runtime. You can refactor them to use standard scheduled functions. Here's an example repo for you with the migration path: https://github.com/netlify/next-scheduled-bg-function-migration

You can find the documentation and additional information on the new runtime here: Next.js on Netlify

The v4 runtime is now in the maintenance support phase with no new features being added. Occasional bug fixes and security patches will be applied when needed.

Thanks!