DEV Community

Ben Halpern
Ben Halpern

Posted on • Updated on

What's the simplest way to run a Ruby script in the cloud every so often?

I come across the situation where I want to re-run a Ruby script every so often. I'd like to know the simplest way to run this in the cloud. All it needs to do is execute a script every x seconds or minutes, etc. It should be able to require gems installed via a Gemfile and that's it. If anything requires any special builds outside of Ruby I don't need it.

I have some ways I've done this in the past, but always with a bit of extra overhead and boilerplate I feel like I could do without. What I'm looking for is the best possible developer experience.

Of things I haven't really tried, AWS Lambda + Scheduled Events seems like it could be a nice go-to. It's not clear to me at this moment whether I can run Ruby with gem dependencies in Lambda. I've used the service, but not with Ruby.

I'm also not stuck on Ruby but it's my go-to language for simple scripts and I'd love if I could deploy these with equal simplicity. All input is welcome, including non-Ruby things to try that really do accomplish the goal of simplicity.

Top comments (14)

Collapse
 
rhymes profile image
rhymes • Edited

What about Now + Docker?

Maybe you can deploy a Ruby script + a Dockerfile with cron configured and run a script repeatedly.

Anytime you change the script or the scheduling configuration you can just deploy a new one.

Another option could be Microsoft Azure time triggered functions but it doesn't support Ruby

Collapse
 
ben profile image
Ben Halpern

What about Now + Docker?
Great thought! I look forward to any other ideas folks have.

Collapse
 
jvarness profile image
Jake Varness

I was actually going to suggest a cron job as well lol.

Collapse
 
ryankilleen profile image
Ryan Killeen • Edited

I know it's not what you asked, but I'm curious about the nature of what the script's trying to achieve, sometimes recurring event type things can benefit from being redesigned.

Keep-alive or "ping this endpoint" type recurring things meant to cause side-effects aren't ideal, and the orgs have moved away from un-trackable, invisible Cron job type work.

Collapse
 
ben profile image
Ben Halpern

The scripts are definitely in the realm of "ping this endpoint" kind of scripts. I could see how this would be a problem at a certain scale, but for automating flows of my own the downside doesn't seem so troubling.

That's my defense of the idea, but I'd love some elaboration on your general concern.

Collapse
 
ryankilleen profile image
Ryan Killeen

The general concern is this:

Something that is necessary to the life of an app or process shouldn't live separate from it, both in codebase and infrastructure. That gets lost among teams / individuals. They tend to go undocumented, forgotten by time, and then you're dependent on something without realizing it. I've had to deal with lots of legacy migrations that get thwarted and delayed by something.

Typically there are better ways to achieve something set up like this. If it's a keep awake ping, there are almost always better ways of configuring Always Awake.

Sometimes it's unavoidable, it's possible that this doesn't fit your situation, and also maybe it isn't a critical thing. But if you can keep it closer to where it's necessary, future you / future devs will appreciate it being clearer, and it won't be an ambush when it's gone.

Collapse
 
preciselyalyss profile image
Alyss 💜 • Edited

I was previously looking at doing almost exactly this a year or so back.

Rancher has container-crontab which will perform actions on a docker container based on a cron schedule.

Dockron allows scheduled docker runs but it is kind of stale. Last commit was in 2015.

AWS Elastic Container Service also supports service scheduler, but I have no context for the overhead required to use that service.

Collapse
 
phlash profile image
Phil Ashby

Scheduling / executing is probably less complex than wiring it into whatever it acts upon? Follow the data...?

If (as alluded to) you are 'pinging an endpoint' then I'd be tempted to find a solution outside your own code, there are many good monitoring services to choose from.

Do we get more details @ben , or is part of the fun us guessing what you are actually doing here? :)

Collapse
 
6temes profile image
Daniel

Just create a free Heroku machine and use the Scheduller add-on.

Collapse
 
mtbsickrider profile image
Enrique Jose Padilla
Collapse
 
6temes profile image
Daniel

I get your point. But, in this context, the "just" was in reference to the other answers given, that involved quite more convoluted methods to solve the same problem.

Collapse
 
jballanc profile image
Joshua Ballanco

Assuming that whatever gems you need to depend on are supported by Java, you could just use JRuby, compile your script to a JAR, and run it periodically on AWS Lamda.

Collapse
 
developius profile image
Finnian Anderson

I may be wrong but I think rake tasks + heroku can do this

Collapse
 
mikesimons profile image
Mike Simons

docs.hyper.sh/Feature/container/cr... looks nice. Haven't used it though.