ruby-on-railsrubyforeman

Foreman terminates after the whole process and doesn't run accordingly as defined in Procfile


I am working on importing data from web based CSV into database, so I created a rake task that imports data into database. However I tried to make running my rails app a little seamless and I integrated the import rake task and running rails server into foreman.

However, when I run foreman start, the processes start but terminates after the rake task finishes. I also will like that rake task to start first before running rails s

Here is what I have done below:

lib/tasks/web_import.rake

require 'open-uri'
require 'csv'

namespace :web_import do
  desc 'Import users from csv'

  task users: :environment do
    url = 'http://blablabla.com/content/people.csv'
    # I forced encoding so avoid UndefinedConversionError "\xC3" from ASCII-8BIT to UTF-8
    csv_string = open(url).read.force_encoding('UTF-8')

    counter = 0
    duplicate_counter = 0

    user = []
    CSV.parse(csv_string, headers: true, header_converters: :symbol) do |row|
      next unless row[:name].present? && row[:email_address].present?
      user = CsvImporter::User.create row.to_h
      if user.persisted?
        counter += 1
      else
        duplicate_counter += 1
      end
    end
    p "Email duplicate record: #{user.email_address} - #{user.errors.full_messages.join(',')}" if user.errors.any?

    p "Imported #{counter} users, #{duplicate_counter} duplicate rows ain't added in total"
  end
end

Procfile

rake: rake web_import:users
server: rails s

when I run forman start, the image below shows the process

process image

I will like the rake task in the foreman to run first before running rails s command. I also don't want it to terminate by itself. I don't know what am doing wrong.

Any help is appreciated.


Solution

  • I solved this by refactoring the Procfile. Instead of having two tasks, I merged it to just one command using && so I can determine which command takes the prefix and which one takes the suffix.

    So I changed the profile to:

    tasks: rake web_import:users && rails s -p 3000
    

    With this I have my import run first and server command being the last.

    If you noticed, I added port with -p flap so as not make sure server is listening on port 3000. Note adding port is optional.

    I hope this helps someone as well.