Skip to main content
This is a technology preview. It demonstrates how you can launch fly machines dynamically to perform background tasks from within a Rails application.

Deploying a Rails project as a Fly.io Machine

Now use your favorite editor to make a one line change to config/routes.rb:
Install fly.io-rails gem:
Source to this gem is on GitHub. If adopted, it will move to the superfly organization. Optionally configure your project:
The above command will configure your application to scale to zero whenever it has been idle for 5 minutes. See generator options for more details. Feel free to tailor the generated files further to suit your needs. If you don’t run the fly:app generator, the files necessary to deploy your application will be generated on your first deploy using default options. Deploy your project
You have now successfully deployed a trivial Rails app Fly.io machines platform. You can verify that this is running on the machines platform via fly status. You can also run commands like fly apps open to bring your application up in the browser. Now lets make that application launch more machines.

Installing fly on the Rails Machine

Since we will be using fly services from within our Rails application, we will need to install the fly executable. We do that by adding the following lines to our Dockerfile:
A good place to put these lines is immediately before the # Deploy your application comment. Next we need to make a Fly token available to our application:

Add a controller

Lets generate a controller with three actions:
The three actions will be as follows:
  • job/start will be the URL you will load that will kick off a job.
  • job/complete will be the URL that job will fetch once it is complete.
  • job/status will be the URL you will load once the job is complete to see the results.
To keep things simple, all we are going to do is have these tasks write timestamps to a file, one when the job starts, and one when the job completes. Status will return the results of the file. The code to do this is straightforward:
Note that the start method provides the complete URL of the complete action as a parameter to the machine job. Before moving on, lets make sure that the file exists:

Add a Job

We start by generating a job:
Overall the tasks to be performed by this job:
  • Specify a machine configuration. For simplicity we will use the same Fly application name and the same Fly image as our Rails application. The server command will be curl specifying the URL that was passed as an argument to the job.
  • Start a machine using this configuration, and check for errors, and log the results.
  • Query the status of the machine every 10 seconds for a maximum of 5 minutes, checking to see if the machine has exited.
  • Extract the exit code and log the state. If the machine has exited successfully we delete the machine, otherwise we leave it around so that further forensics can be performed.
The implementation of this plan is as follows:
Note in particular the calls to Fly::Machines: Each of the lines in the list above is a link to the documentation for that API. The key difference is that instead of passing in and receiving back a JSON object, you pass in and receive back a Ruby hash. And all of the URLs and HTTP headers are taken care of for you by the Fly::Machine module. For those interested in the inner workings, the source to Fly::Machine is on GitHub. Again, all this is beta, and subject to change.

Trying it out

We are now ready to deploy, but before we do in a separate window start watching the log::
Now deploy the application:
If you run fly apps open you will arrive at your application’s welcome page. Take a note of the URL. Either in the browser or in a command window add /job/start. As a response (either in your browser or terminal window you will see something like:
Switching to your log window you should see output similar to the following:
And, finally, visit /job/status to see the results. Durations will vary, but I’m currently seeing a total elapsed time of anywhere from about two and a half seconds to five seconds.

Recap

While not exactly a realistic application, this application demonstrates a number of important features. Scheduling a job. Launching, monitoring, and removing a machine. Inter-machine communications. With the right parameters, you can start machines in remote geographic locations or with volumes attached. These machines will have access to your application’s secrets so they can access databases or other cloud services. And if you go back and look at the main.tf file in your application directory you can get an indication of what steps are required, and what options are required for each step, to launch a Rails application. The possibilities are unlimited. At the moment, this is only a preview, so API names and options may change. But do try this out, and if you have questions or come up with an exciting usage of this, let us know on community.fly.io.