Naturaily logo with transparent background

Run Services in the Background

Some time ago Marcin wrote about basic design patterns in Ruby on Rails. I’d like to go back to Service pattern in this post. I really like it, it’s a good idea to place application’s business logic there. At Naturaily we use it quite often.

Colorful terminal screen displaying a manual page for the "man" command with highlighted syntax in various colors.

Some time ago Marcin wrote about basic design patterns in Ruby on Rails. I’d like to go back to Service pattern in this post. I really like it, it's a good idea to place application’s business logic there. At Naturaily we use it quite often.

It happens that we need to run our service in the background. What's the easiest way to achieve this? Wrap our Service in a Job of course! Right... But we don't want to end up with dozens of very similar jobs. So... Here’s the solution. Let’s create universal Job for all the services.

Create the Job

Our job will be called ServiceInvocationJob. It's a good name for our universal job. To create a new Job we need to run one simple task:

tsx

And now we should edit newly created file. Right now it looks like this:

tsx

What do we need to run some service? Service's arguments, of course, and Service class itself! We can't pass class as argument, so we'll pass class' name. At the end our Job will be as simple as that:

tsx

Very nice? Don't you think?

Update BaseService class

We already run our services in the background jobs! Everywhere, where we want to run our service in background, we need to change classic invocation:

tsx

to this

tsx

But it doesn't look well. I think this one would be better:

tsx

Right?

So now we need to update our BaseService a bit! Nothing big. Just add a new static method called async_call. There's the updated file:

tsx

Summary

It is very easy, nothing fancy. However, there is a hidden message: you have to be lazy and remember that future-you is also lazy! You can create code that will solve your current problem (e. g. wrapper-job for your service), it's easy and fast, you don't need to think at all. But you should think a bit more and create code that will solve your problem and prevent the emergence of similar problems in the future(e. g. our ServiceInvocationJob). Future-you will be thankful.

Thank you! Bye!

Let’s Create a Great Website Together

We'll shape your web platform the way you win it!

More posts in this category

  • Cover photo to article titled: Top Next.js agencies. Three people collaborate in an office setting with a computer displaying charts. NEXT.js logo is visible. Background is green.

    October 13, 2025 • 8 min read

    Top 10 Next.js Agencies for Your Project

    The best digital products feel effortless: fast to load, easy to use, and ready to grow. Behind successful ones is a Next.js agency that knows how to turn performance, scalability, and design into a real competitive edge. This guide introduces ten such agencies shaping the future of modern web development.

    READ MORE
  • Core Web Vitals increase in traffic and sales. Graphic presenting man checking the website's metrics.

    September 30, 2025 • 9 min read

    How Much Will Improving Core Web Vitals Actually Increase My Traffic or Sales?

    Are you one of those business owners asking yourself, ‘Why isn’t my site ranking or converting as well as my competitors’? Even after investing in SEO and marketing, something can still feel missing. The problem often goes beyond keywords or ad spend and comes down to how fast and seamless the website feels to users. This is where Core Web Vitals optimization becomes essential.

    READ MORE
  • Illustration of people interacting with a large screen displaying analytics and a magnifying glass highlighting "#1," on a green background. It is a cover photo of a blogpost about Next.js.

    September 24, 2025 • 8 min read

    Next.js Websites for SaaS Scale-Ups: Why They’re the Growth Engine You Need

    SaaS companies in their scale-up phase face a bottleneck that rarely comes from a lack of product vision. The real slowdown lies in their digital infrastructure, most often their marketing and website layer.

    READ MORE