Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Options for running cron job? #146

Closed
ranyefet opened this issue Nov 28, 2016 · 67 comments
Closed

Options for running cron job? #146

ranyefet opened this issue Nov 28, 2016 · 67 comments
Labels
triaged: feature request Issue or PR has been reviewed by a maintainer and work is being tracked

Comments

@ranyefet
Copy link

Hey,

What are the options for running Cron jobs?
I'm building a chat bot and I need a way to send messages not on the req/res cycle.

Do you have a plan to add support for cron jobs?
If not do you have a suggestion for workaround? alternative services?

@leo leo closed this as completed Nov 28, 2016
@ranyefet
Copy link
Author

What happen if my process crashes after setInterval?
Or I deploy a new version of my app before setInterval is triggered?

@leo
Copy link
Contributor

leo commented Nov 28, 2016

What happen if my process crashes after setInterval?

Since setInterval is part of the process, it will stop executing.

Or I deploy a new version of my app before setInterval is triggered?

The old instance of the app will just keep running as long as people are accessing it. If nobody accesses it, it will fall into a light sleep and setInterval will be paused.

@ranyefet
Copy link
Author

Thanks @leo but It looks like it's not good replacement for cron job.
Also I'm not sure how now handle scaling but if there's more then one process running it means that setInterval will be called for each node process.

Btw, why did you closed this issue? As it is, there's no solution for this problem yet.

@leo
Copy link
Contributor

leo commented Nov 28, 2016

@ranyefet You should be able to write the code so that setInterval only gets executed in one the processes that are running. I don't see a reason why it shouldn't be a good replacement for cron jobs. It does exactly what cron jobs do. You could even spawn a new process and use it only for running the intervals. What's the difference then? 😊

If you need an interface that looks more like the one of regular CronJobs, you can use modules like node-cron (there are hundreds of these on npm).

@OlliV
Copy link
Contributor

OlliV commented Nov 29, 2016

@leo I suppose the difference is that neither of the options can wakeup the deployment :(

@ranyefet the feature has been promised for the community but I can't tell any timeline for it.

@leo
Copy link
Contributor

leo commented Nov 29, 2016

@OlliV Ummm... If we add support for CronJobs waking up deployments, I don't think there would be a reason to keep the "fall into a light sleep" feature alive at all, because everyone will set CronJobs to prevent the deployment from falling asleep.

So IMHO, I don't think there's anything else we can do (because killing this sleep feature would result in a heavy perf impact from old/unused deployments)... 🤔

@mjsisley
Copy link

So to clarify, if I deploy an app that's only function is a scheduled job, it will go into "light sleep" after a few hours and never come out again?

I would like to host an app that's only purpose is interacting with Firebase and Stripe at a scheduled time each day. Does a response qualify as incoming traffic? Or is there no way to accomplish this with the current state of now?

@builtbylane
Copy link

something like this: https://devcenter.heroku.com/articles/scheduled-jobs-custom-clock-processes would be very helpful.

@alexmoreno
Copy link

Let's suppose I just want to create a simple social network bot. A few crons per day, that's it.
Isn't now a good place to do it? I'm not complaining at all, I just want to know if I can use your service with the light sleep.

@brandonmp
Copy link

@leo there's some middle ground here though right? ie no- or limited-sleep on any instance w/ an active alias pointed to it?

as it stands the next best option for low-friction scheduled tasks is AWS Lambda, and that's considerably more annoying to configure than now, so i'd love to see some flex in this policy to accommodate this use case </2¢>

@GlenTiki
Copy link

GlenTiki commented May 17, 2017

I would love to see the ability to create a service where I can schedule it to wake up and run a job every x amount hours, days, etc.

The interface for this could be described in the package.json for a deployed service. Scheduled services like this could run under the caveat that only the most recently deployed version can be woken up. I think that'd be a very fair caveat from Zeit. These can also be a direct draw for subscribers to your service - e.g. free tier gets only 1 scheduled service. Paid tiers could get 3, 5, unlimited, etc.

The interface for this might look like so:

{
  now: {
      schedule: `${ANY VALID CRON STRING}`
  }
}

EDIT: You could also create these services without an accessible url, so after they cleanly exit you can put them to sleep again because they don't listen for incoming connections

@rgoytacaz
Copy link

Is there such a thing in the roadmap for zeit's now? @leo @OlliV

@matheuss
Copy link
Member

matheuss commented May 18, 2017

We do plan to add some kind of cron jobs support – no ETA for now tho!

@matheuss matheuss reopened this May 18, 2017
@brandonmp
Copy link

it looks like the 'light sleep' can be prevented by setting a deployment's scale to 1 (https://zeit.co/docs/deployment-types/lifecycle#instances)

I didn't know about this feature, but it looks like it's a few months old

If I run a cron job on a server w/ a min scale of 1, will it stay up & behave as expected?

@abdulhannanali
Copy link

abdulhannanali commented Jan 11, 2018

Not associated with Azure or anything but I had to recently monitor timeline at set intervals since the Webhook based Twitter API is still in Beta and not available publicly. After sometime I went with Google Cloud Functions because of their generous plans and other services with free tiers such as Google Cloud DataStore but I also wanted Cron jobs which is not natively supported by Cloud Functions as of now.

Luckily, Azure provides this service with again a very generous plan, you can set up crons to run with it, and it works like BAM!

It's known as Time Trigger for Azure Functions

@pranaygp
Copy link
Contributor

pranaygp commented Jan 11, 2018

I just found this issue after running into the exact situation yesterday. I chose to go with AWS Lambda functions. Here's a good guide to get started.

I also think it'd be nice to be able to do with now and would've been my first choice if it was supported.

@matheuss @leo any update on this? Will @brandonmp's suggestion of using scale 1 and having a node script that uses setInterval do the trick, or will there be some native support for this?

@brandonmp
Copy link

yeah serverless functions are attractive for this, i just wish they ran a later version of node. I don't want to write promise/callback code like some kinda caveperson, but transpilation for simple scripts seems too cumbersome.

@abdulhannanali
Copy link

abdulhannanali commented Jan 12, 2018

@pranaygp I think Azure Timer Functions are more user friendly. AWS also has this limit of maximum 500 timer functions like what if I need to run 501 crons. Another really useful thing to do will be to use Google Cloud Pub/Sub and invoke the functions using the Pub/Sub events, it's faster and more secure than using just HTTPS and can be used to run multiple functions with just one event, CRAZIEST THING EVER.

However, if Zeit builds support for it natively, I don't see any reason to go to other places for my tiny pity hacks.

@brandonmp Azure plans to support new versions very soon. But last time I used Firebase Functions which are built on the top of Google Cloud Functions I could use async/await without any problems. However, babel-env makes transpilation process very easy. You should try it out sometime. It's just a one or two npm installs.

@justinmpier
Copy link

justinmpier commented Mar 17, 2018

It seems it should be possible:

  1. Preventing sleep:
    "The application never sleeps if minimum instances is set to a value larger than zero."
    https://zeit.co/docs/guides/app-lifecycle-and-scalability
    "How Do I Prevent My Deployment from Freezing?"
    https://zeit.co/docs/other/faq#how-do-i-prevent-my-deployment-from-freezing

  2. How to trigger:
    A) "Create a deployment with a basic HTTP service running and initialize a setInterval and a second deployment with the code of your tasks running behind an HTTP server."
    https://zeit.co/docs/other/faq#how-do-i-run-scheduled-tasks-on-the-now-platform
    B) A bit more tricky but should also work using Docker and related libraries.

@Zertz
Copy link

Zertz commented Apr 7, 2018

Another way to do it is to now scale <app> 1 1 and run node-cron on that app. It is then possible to either run tasks directly on that instance or have it call other microservices. It is also important to set "regions": ["sfo1"] in now.json in order to prevent tasks from being called twice.

@mxmzb
Copy link

mxmzb commented May 5, 2018

@Zertz how exactly do you write your cron app? cause mine will just timeout at the deploy as it stays in the cron process.

@Zertz
Copy link

Zertz commented May 6, 2018

Good point, you need to setup a dummy server for it to stay awake.

http.createServer(function(req, res) {
  res.writeHead(200, { "Content-Type": "text/plain" });
  res.end();
}).listen(3000);

@jonleung
Copy link

jonleung commented Aug 9, 2018

See this y'all: https://medium.com/@philmillman/deploy-a-simple-node-cron-server-in-minutes-with-now-c5926a49afab

@AutomationD
Copy link

AutomationD commented Aug 19, 2018

I'm getting this error, seems like there is no way to set a min number of running instances.

Error! Cloud v2 does not yet support setting a non-zero min number of instances.

UPD: I switched to "cloud": "v1" and it seems to be working .

@ankerbachryhl
Copy link

Yes but that only allows running cron jobs through CloudWatch which have a max of every 5 minutes. I was thinking more like node-cron where I can run events every second.

@gunar
Copy link

gunar commented Oct 21, 2019

We can use Google Cloud Scheduler in the interim (3 free jobs/month).

@leo
Copy link
Contributor

leo commented Dec 27, 2019

Thank you for creating this issue!

Please note that – in order for us to be able to handle each feature request with care – we have to consume all of them through a unified pipeline that makes it easier for us to prioritize, track and progress on the features our user base is interested in.

In turn, the Issues tab on this repository is best only be used for reporting bugs, which we can immediately act on.

For well-explained feature requests (like the one you just posted), please contact us at support@zeit.co, so that we can consider adding it to our roadmap.

In the case of your specific feature recommendation, you can rest assured that we're already tracking it on our roadmap and you can expect to see an update on our Twitter account or Blog soon.

@leo leo closed this as completed Dec 27, 2019
@jerrygreen
Copy link

Looks like Leo's closing a lot of issues lately, which haven't resulted into any solution :(

@danhollick
Copy link

This is super annoying. Every time I deploy my V1 instance I get a bunch of warnings screaming at me about V2 but I can't upgrade without a way to handle this. It's made worse by the fact that the only communication from the team about this has been to close the issue!

Similar lack of information here: https://spectrum.chat/zeit/general/in-v2-how-can-you-keep-a-node-server-cron-job-alive~18dc0165-507f-4d1e-874a-77d0bbafe20a?authed=true

@colllin
Copy link

colllin commented Jan 3, 2020

Can't we just use an "uptime monitoring" or "ping" service to achieve cron jobs with any serverless function?

  • UptimeRobot offers 5-minute ping intervals for free, or 1-minute ping intervals for ~$6/month

    disclaimer: I'd never heard of this provider before about 5 mins ago when I googled for "uptime monitoring", so I can't vouch for them, but it looks like there are many providers to choose from.

  • Point the monitoring url at your cron handler, e.g. myapp.now.sh/api/sync-all-the-things
  • If you want slower intervals, then check the time in your cron handler and decide whether to do anything, or record last run in a database and don't re-run until some time has passed.
  • Bonus: you'll get notified if your cron goes down!

I'm planning to try this soon and will let you know if anything goes astray.

@BrunoBernardino
Copy link

I've been using https://easycron.com for actual "serverless" crons and it's been going very well, I assume it could work similarly.

@ankerbachryhl
Copy link

So if I understand this right, I can use something like node-cron with now, as long as I make sure the now server doesn't sleep?

@danhollick
Copy link

@ankerbachryhl you could do that with now V1. But that is being deprecated.
V2 being serverless there isn't really a concept of a server to keep awake so node-cron doesn't really work with this architecture.

@danhollick
Copy link

I have been chatting to the Zeit team about this recently as it's a blocker for me moving to v2. I don't really want to use another 3rd party vendor to do something I could previously do with some node cron code.

Their official response was this:

We are extremely committed to removing lock-in, that’s why we invest in OSS so heavily (like Next.js and many other projects).

However, the idea of running a process continuously for the timing APIs to work and ultimately give you a very complex and brittle solution, even if embedded directly in our platform, is not something we intend to offer to our customers soon.

We will continue to consider this problem thoughtfully and will keep you posted of any news.

@ankerbachryhl
Copy link

@danhollick Thank you for your response. I guess I have to give up on now :/ So far I've been using a Heroku hobby server which actually works fine for the time giving, but must admit I would rather use now.

@amio
Copy link
Contributor

amio commented Jan 27, 2020

@danhollick GitHub Actions support running jobs on schedule, could be a solution for this.

I have an example setup executes hourly as a http trigger. If the job is simple enough, you may even execute it right within GitHub Action, just be careful not to violate these usage limitations.

@danhollick
Copy link

@amio thanks for the info. Currently use Gitlab for my repo, but could move it over if this is the way I want to go.
To be honest, I am waiting to evaluate whether I should make the architecture switch to a serverless backend or just move it over to heroku. There are other unknowns for me, like Stripe and NodeMailer integrations. The way I have them set up now is pretty node dependant so need to investigate how that would work.

@fabiosantoscode
Copy link

I think a lot of the posts here are focusing on keeping the process alive to run a setInterval. However, something like this would probably be better:

export default async (/* no arguments: not a request! */) => {
  await doMyThingWithoutTakingTooLong()
}

export const cron = 'daily'  // or standard cron format

The case for this, to me, is that you don't have to make your API endpoint public just so some external service can ping it periodically.

I understand this might be out of scope for what now is intended to be, but a lot of websites can benefit from daily deleting stuff from the database, sending out some report to slack, periodically refreshing some data, etc.

@donfn
Copy link

donfn commented Mar 31, 2020

Hello,
I'm kind of interested in a cron job feature.
Nothing too fancy. Maybe a panel at the Zeit dashboard
that lets me set cron jobs. There could be a limit for free accounts too (I'm on those).

For example I want a cron job that triggers a serverless function (every few hours or even every day) in order to do a check on our bank api and see if our clients have made their deposits.

That feature would be amazing, since we could just rely
on the Zeit platform and not have to worry about a zillion accounts and subscriptions
on other providers that would do that.

Thanks

@AutomationD
Copy link

I think the important thing to realize here is free plan cost and it's availability. If "always on" is the only way to run free version, it may not remain free any more.

So let's be mindful and keep the actual economics in mind.

@donfn
Copy link

donfn commented Mar 31, 2020

The option already exists via a connection with Google Cloud.
What I am saying is that it would be nice if it was built-in.

Sure, I really appreciate the fact that a Free plan even exists!
There could be a limit for the cron job (for ex. min frequency every 1 hour).
It could even be an add-on paid option! (though cheap hahaha)

You cannot build apps -or at least complex ones- without a cron
feature of some sorts. Every cloud provider that offers a serverless product,
has cron - and that's to overcome serverless' main drawback.

Update:
One thing I forgot to mention, is that I don't want to keep my app alive for ever.
I want to trigger an action every now and then.
Besides, serverless is dirt-cheap. That's why they "give it away" on their free plan.

@JoeDuncko
Copy link

Based on this thread I will not be using Now for a client's project that requires scheduling reminder notifications to users. Was hoping there's be a built in way to schedule events to trigger serverless endpoints. Pretty disappointed.

Hopefully this is resolved soon! The workarounds above seem a little unreasonable - having severless functions with their own cron sounds like it defeats the point of going serverless (even if they do sleep), and using a third party service to trigger the functions sounds like I should just use a third party service.

I still love the stuff the Zeit does, hopefully I'll have the opportunity to use Now for my next project!

@mxmzb
Copy link

mxmzb commented Apr 16, 2020

I've come to the realization, that you can use AWS lambda function as crons. They provide a schedule option, so it's the perfect solution for me (cost-efficient, performant, flexible in its programming language, etc.).

I am not even sure I would use Zeit cronjobs, even if they introduced them.

@redstrike
Copy link

Is there any official guided solutions for creating cron jobs?
It's 2020 and this is a road-blocking while developing usable apps on Vercel serverless platform.

  • Setting up a standalone cron server on another dedicated server to execute some scheduled tasks at Vercel Serverless seems complicated.
  • Using third party cron services also introduced more limitations. We have to pay them even if our use cases are stupidly simple and not for business purposes.

@MarvinMiles
Copy link

Strong and free solution: https://pipedream.com

@BrunoBernardino
Copy link

BrunoBernardino commented May 22, 2020

@redstrike No affiliation here, but https://easycron.com is very inexpensive. Starting at $12 / year enables you to do a lot with Zeit Vercel (the free plan should be more than enough for experimenting and testing your use cases).

If setting up a simple @now/node and hitting it with easycron seems complicated, you might want to consider using a more "integrated" workflow builder like https://zapier.com where you can run some code at a schedule.

@BrunoBernardino
Copy link

BrunoBernardino commented Jul 24, 2020

Another alternative to EasyCron I've been using (for crons that need to run less frequently than hourly) is GitHub Actions. I wrote about it here: https://news.brn.sh/using-github-actions-as-a-web-cron

@pyrsmk
Copy link

pyrsmk commented Aug 28, 2020

You can also use Cloud Scheduler from the GCloud integration on Vercel Marketplace : https://vercel.com/integrations/gcloud

@jerrygreen
Copy link

Idk guys, I think it’s mean to break resource optimization by such an abuse. I mean for what? If it appears your app doesn’t have enough visitors, - maybe such a slight improvement won’t make difference? I mean, it’s not Heroku, where frozen->warm translation required almost a minute or so, so even for “just first” visitor it was huge, too unpleasant. Here it’s 100ms, even less. You’re not making game servers, right?

@donfn
Copy link

donfn commented Aug 28, 2020

Ok, let's settle this.

We do not want to ping our app and keep it alive.
All we need is a call, every now and then, to a specific function that does something on our backend (for ex. Checks our bank API for any incoming payments and then marks the client order as paid or something along those lines).

We don't want to abuse the platform. A serverless function is a process with a begining and an end, designed to run for a few milliseconds and then exit.

Most implementations mentioned here feel awkward at best. Especially GCP for Vercel that allows you to choose a SPECIFIC deployment for Cron. This is very impractical, since with my rate of deployments, I would need to change this setting every 5 hours or so.

Anyhow, we know CRONaaS exists and most of us use it (myself included). Having it hardcoded in your app though or being able to manage everything from one place is a whole different thing (feels more stable and "correct") - besides as I have said in a previous post, most FaaS providers have a cron functionality, because most people don't want to mess with statefull servers and want to enjoy the stability, flexibility and low-cost of serverless. AWS for example - the service behind Vercel Serverless - already provides this.

@shc023
Copy link

shc023 commented Oct 13, 2020

@donfn @pyrsmk and the couple other people who've mentioned the Google Cloud integration - it looks like the Integration's Google Cloud Scheduler feature only allows scheduling deploy, not to call Serverless Functions on a schedule. Or am I doing something wrong?

@leerob
Copy link
Member

leerob commented Nov 18, 2020

We've added a new section to the docs with cron job recommendations!

https://vercel.com/docs/solutions/cron-jobs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triaged: feature request Issue or PR has been reviewed by a maintainer and work is being tracked
Projects
None yet
Development

No branches or pull requests