next-runtime icon indicating copy to clipboard operation
next-runtime copied to clipboard

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

Open RohitRajendran opened this issue 2 years ago • 4 comments

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)?

  • [ ] Yes, I am using the app directory

More information about your build

  • [ ] I am building using the CLI
  • [ ] I am building using file-based configuration (netlify.toml)

What OS are you using?

None

Your netlify.toml file

`netlify.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`
/** @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 Build logs for minimal reproduction repo

Function logs

Function logs

Function logs link

.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.

RohitRajendran avatar May 15 '23 04:05 RohitRajendran

Hi!

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

Thanks!

LekoArts avatar May 15 '23 14:05 LekoArts

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

RohitRajendran avatar May 17 '23 02:05 RohitRajendran

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

RohitRajendran avatar Jul 13 '23 01:07 RohitRajendran

@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.

RohitRajendran avatar Aug 11 '23 20:08 RohitRajendran

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!

MarcL avatar Apr 23 '24 12:04 MarcL