8

I'm working on moving us from ant to gulp, and as part of the effort I want to write timing stats to Graphite. We're doing this in ant as well (no idea how, beside the point anyway). My question is, I'd prefer to not have to add some or other plugin manually to every task we have (we have over 60), but rather have some sort of global behavior, where for every task, before the task is run a timer is start, and when it signals completion we push some data to Graphite (over statsd).

Can someone point me in the right direction where to hook into gulp for this? I couldn't find anything particularly useful in the docs / recipes...

We're running gulp@4.

Ben Smith
  • 19,589
  • 6
  • 65
  • 93
Steven
  • 1,566
  • 2
  • 16
  • 38
  • Why not overwrite gulp.task with your own logic ? – elad.chen May 09 '17 at 08:20
  • `gulp` relies on the [`orchestrator` module](https://github.com/robrich/orchestrator) which exposes internal events. So you can do `gulp.on("task_stop", ...)` and listen to each task completition event. I don't think it is a good practice, but technically doable. – MarcoL May 09 '17 at 09:01
  • @elad.chen because there are multiple ways for signaling completion in gulp (stream complete, promise resolved, callback), if I see this correctly I would have to account for all these behaviors. – Steven May 09 '17 at 13:50
  • Did my answer help @Steven? How did you resolve this problem? – Ben Smith Jul 17 '17 at 08:45

1 Answers1

0

Instead of adding timing code to your numerous tasks, you could make use of the NPM gulp-duration package.

A snippet of an example of it's use is shown below:

function rebundle() {
 var uglifyTimer = duration('uglify time')
 var bundleTimer = duration('bundle time')

 return bundler.bundle()
   .pipe(source('bundle.js'))
   .pipe(bundleTimer)
   // start just before uglify recieves its first file 
   .once('data', uglifyTimer.start)
   .pipe(uglify())
   .pipe(uglifyTimer)
   .pipe(gulp.dest('example/'))
}

gulp-duration's duration function:

Creates a new pass-through duration stream. When this stream is closed, it will log the amount of time since its creation to your terminal.

will then allow you to log the duration of the task.

Whilst this is not a global behaviour solution, at least you can specify the timing code in your gulp file, as opposed to having to modify all 60+ of your tasks.

Ben Smith
  • 19,589
  • 6
  • 65
  • 93