Madu Alikor
Madu Alikor

Reputation: 2662

How do you re-run the dnx . kestrel command on any file changes?

How do i execute my kestrel server to restart whenever any files are changed? and not just stop?

I have attempted

dnx --watch . kestrel

but the server just stops and i have to manually re-run the command

I have also attempted using the npm watch command but this appears to just trip over itself

watch 'dnx --watch . kestrel' .

Upvotes: 3

Views: 1450

Answers (5)

ssmith
ssmith

Reputation: 8962

There is a command you can use for this, called dnx-watch. It didn't exist when the original question was asked, but was added in beta 8 in September 2015. You install it using

dnu commands install Microsoft.Dnx.Watcher

You run it by simply passing to it the command you would have passed to dnx. So, replace

dnx web

with

dnx-watch web

You can learn more here: http://ardalis.com/get-started-with-dnx-watch

Upvotes: 5

Eugene
Eugene

Reputation: 11

this command is working better on windows (if you have kestrel command defined):

nodemon --ext "cs,json" --exec "dnx --watch kestrel"

> Blockquote
C:\Users\name\YoWebApp>**nodemon --ext "cs,json" --exec "dnx . kestrel"**
6 Oct 14:46:13 - [nodemon] 1.7.1
6 Oct 14:46:13 - [nodemon] to restart at any time, enter `rs`
6 Oct 14:46:13 - [nodemon] watching: *.*
6 Oct 14:46:13 - [nodemon] starting `dnx . kestrel`
System.InvalidOperationException: Unable to load application or execute command '.'. Available commands: kestrel, web, ef, mon.
   at Microsoft.Dnx.ApplicationHost.Program.ThrowEntryPointNotfoundException(DefaultHost host, String applicationName, Exception innerException)
   at Microsoft.Dnx.ApplicationHost.Program.ExecuteMain(DefaultHost host, String applicationName, String[] args)
   at Microsoft.Dnx.ApplicationHost.Program.Main(String[] args)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Dnx.Runtime.Common.EntryPointExecutor.Execute(Assembly assembly, String[] args, IServiceProvider serviceProvider)
   at Microsoft.Dnx.Host.Bootstrapper.RunAsync(List`1 args, IRuntimeEnvironment env, FrameworkName targetFramework)
   at Microsoft.Dnx.Host.RuntimeBootstrapper.ExecuteAsync(String[] args, FrameworkName targetFramework)
   at Microsoft.Dnx.Host.RuntimeBootstrapper.Execute(String[] args, FrameworkName targetFramework)
6 Oct 14:46:14 - [nodemon] app crashed - waiting for file changes before starting...
6 Oct 14:46:44 - [nodemon] restarting due to changes...
6 Oct 14:46:44 - [nodemon] starting `dnx . kestrel`
System.InvalidOperationException: Unable to load application or execute command '.'. Available commands: kestrel, web, ef, mon.
   at Microsoft.Dnx.ApplicationHost.Program.ThrowEntryPointNotfoundException(DefaultHost host, String applicationName, Exception innerException)
   at Microsoft.Dnx.ApplicationHost.Program.ExecuteMain(DefaultHost host, String applicationName, String[] args)
   at Microsoft.Dnx.ApplicationHost.Program.Main(String[] args)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Dnx.Runtime.Common.EntryPointExecutor.Execute(Assembly assembly, String[] args, IServiceProvider serviceProvider)
   at Microsoft.Dnx.Host.Bootstrapper.RunAsync(List`1 args, IRuntimeEnvironment env, FrameworkName targetFramework)
   at Microsoft.Dnx.Host.RuntimeBootstrapper.ExecuteAsync(String[] args, FrameworkName targetFramework)
   at Microsoft.Dnx.Host.RuntimeBootstrapper.Execute(String[] args, FrameworkName targetFramework)

Upvotes: 0

krizzzn
krizzzn

Reputation: 1482

Although the selected solution is shorter, here's what I'm doing in an angular application that is driven from grunt

The application has a frontend and an api backend.

First there is a shell task for starting the api:

shell: {
  api:{
    options: {
      execOptions : {
        cwd: '..\\$working_directory'
      },
      callback : function (err, stdout, stderr, cb) {
        restartServer('shell:api',err,cb);
      }
    },
    command: function() {
      return 'dnx --watch localfarm';
    }
  },

at the top of my gruntfile I define the function that restarts the server:

var restartServer = function (task, err, cb) {   

var timeoutInSec = 30;
if (err === null)
  timeoutInSec = 2;

grunt.log.write(task + ' Task ended. Retrying in ' + timeoutInSec + ' seconds');

setTimeout(function() {
  grunt.log.write('retrying ' + task );
  grunt.task.run(task);
  cb();
}, timeoutInSec*1000);   };

Everytime something changes in the code, the dnx --watch process is killed automatically. Grunt then waits for 2 seconds and restarts the dnx process. If dnx fails, it waits for 30 seconds until it tries again. This gives me time to fix the code. (In my version of the code I also use beep to get a notification when the api is reloaded.)

To run the farm (frontend and backend) I added a concurrent task that starts multiple threads:

concurrent: {
...
  farm: {
    tasks: ['shell:api', 'serve'],
    options: { logConcurrentOutput: true, limit: 10 }
  }
...
}

Upvotes: 0

Madu Alikor
Madu Alikor

Reputation: 2662

I found the following work around using nodemon from

https://github.com/johnpapa/aspnet5-starter-demo#dnxmon

nodemon --ext "cs,json" --exec "dnx . kestrel"

Upvotes: 1

Victor Hurdugaci
Victor Hurdugaci

Reputation: 28425

That's by design. Today there is no out of the box solution to automatically restart the server after it stops because of the file watcher.

Visual Studio has some special code that monitors the process and restarts it.

However, we have an issue tracking this and we plan to address it in a future iteration.

Upvotes: 0

Related Questions