0

Can I invoke "rake jobs:work" automatically after running "rails s" in console?

Currently, after running rails s in cmd I will also run rake jobs:work in the other console, what i want to happen is After running "rails s" the jobs:work will automatically start.

Luke Peterson
  • 8,584
  • 8
  • 45
  • 46
hey
  • 90
  • 10
  • What do you mean saying 'after running' ? When it finished (it's a server so you'll have to stop it to do so) ? Or when it starts (you can never be sure it's already up and ready) ? Please describe more what are your needs. What that job do and do you need the server running when you run that job really ? – pawel7318 Jan 30 '15 at 06:18
  • hi pawel, what i want to do is when i run "rails s" in console the "rake jobs:work" will (also) automatically run. – hey Jan 30 '15 at 06:31

2 Answers2

1

The right way to go about this would be to use a process manager, like Invoker or Foreman. There is ample documentation on the links, but it boils down to the following steps:

  1. Install the software
  2. Create a configuration file where you declare what processes do you intend to run. Both support Procfile style declaration.
  3. Use the command line client to start the process manager.

Based on my personal experience, I highly recommend Invoker, it goes beyond just a process manager, and packs in a few more handy features, like support for .dev local domain.

Community
  • 1
  • 1
Swanand
  • 12,317
  • 7
  • 45
  • 62
0

One you can do is simply:

rails server & rake jobs:work

It'll run rails server as background job, which you can get back to foreground with fg. It can be annoying that you'll get output from both processes mixed.

I'm not sure what are your needs and what you expect but maybe it would be good for you to use screen (or tmux) to run them in parallel and be able to switch between.

You can do your own .screenrc script which will run the server and any other commands when automatically for you.

There is a little problem that if you run the server from it and you close it (ctrl+c) than you'll loose it's screen window. Fortunately there is a solution for that as well (worked-out on the SO as well - you can read more about it here)

So, I use some helper script for that .run_screen (don't forget to chmod +x it):

#!/bin/bash
/bin/bash -i <<<"$*; exec </dev/tty" 

Than I have .screenrc_rails file:

#shell -${SHELL}
caption always "%n(%t) %= %{b}@%H[%l] : %{r}%c:%s"
termcapinfo xterm ti@:te@
termcap xterm 'AF=\E[3%dm:AB=\E[4%dm'
terminfo xterm 'AF=\E[3%p1%dm:AB=\E[4%p1%dm'
startup_message off

screen -t server 2 ${HOME}/.run_screen rails s
screen -t spork 3 ${HOME}/.run_screen bundle exec spork
screen -t dev_log 4 ${HOME}/.run_screen tail -f ./log/development.log
screen -t test_log 5 ${HOME}/.run_screen tail -f ./log/test.log
screen -t bash 0
screen -t bash 1 

And an alias ( screenr(ails) ) defined at .bash_profile:

alias screenr='screen -c ~/.screenrc_rails'

If you don't know screen than start from ctrl+a, ". ctrl+a, ? will give you some help.

I hope you'll enjoy it.

Community
  • 1
  • 1
pawel7318
  • 3,383
  • 2
  • 28
  • 44
  • thanks for your answer but its not working, if I run "rails server & rake jobs:work" only the rails server is running the delayed_job didnt run, vice versa. – hey Feb 02 '15 at 03:21
  • Are you sure it doesn't work in the background ? Use `bg` to check and `fg` to pull it to the foreground. Check that example to see how it should perform: `(sleep 5 ; echo foo) & echo bar` – pawel7318 Feb 02 '15 at 08:22
  • RESULT: sleep: invalid time interval `5;' sleep: invalid time interval `echo' sleep: invalid time interval `foo' Try `sleep --help' for more information. bar. I think using '&' will run both task, but the second task will only run after finishing the first task. – hey Feb 04 '15 at 06:40
  • Isn't it linux and bash ? – pawel7318 Feb 04 '15 at 16:44
  • sorry i forgot to indicate, I'm using windows command prompt with rails – hey Feb 05 '15 at 09:06