runfile-exec 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 15d56df010391a63ea61f17a1564a2a0afae3b28
4
- data.tar.gz: e78be7898db3321895f8fbeaab7a5f178de8cb3d
3
+ metadata.gz: f82365fc82e880332bfa0c9c20699e881eae14bf
4
+ data.tar.gz: bb3dcf888a6059cd2e511d02da78dfa1a7d627ec
5
5
  SHA512:
6
- metadata.gz: 5a4ebece6872330365477846ca39796b5dbecf13f8bc86b7b658720931fb68094dd90da496ceb7cb90b83bf86ddb1d7f4fc83525c783297c7ba07c853ec5d375
7
- data.tar.gz: 21e318e3764dbfbf2b0c22f562314468a3f242260e6669686aa0445bd9aae6c30fbb33eeac5b234a5d6b0115139c66e17c3a5567e55a3a394a35799c9f8a2e72
6
+ metadata.gz: c8612e1f6e3949c0bf7f24d24e9556847a173d8005dc3f7d7abfcf6fe1ef1fe0f6cbdb70e0c2ec724a5b93070206e2361a240bd2efd864d7bbd7a1cd3daf96ae
7
+ data.tar.gz: 67a5e6c9292846c62cf7b7d80210bae2f3854a3bc6d6f386cfdf91f295b11a546b4df267cc15b301a7ce4a089b6f9d71e80d5190460f9af02ecfa6ec50bc2bc6
data/README.md CHANGED
@@ -27,15 +27,29 @@ run 'pwd'
27
27
  run! 'pwd'
28
28
 
29
29
  # Run a command in the background
30
- run_bg 'some-long-running --daemon'
30
+ run_bg 'some/long-running/process'
31
31
 
32
32
  # Run a command in the background, log to a log file and save the process
33
33
  # ID in a pid file for later reference
34
- run_bg 'some-long-running --daemon', log: 'my.log', pid: 'daemon'
34
+ run_bg 'some/long-running/process', log: 'my.log', pid: 'daemon'
35
35
 
36
36
  # Stop a command started with 'run_bg'. Provide the name of he pid file you
37
37
  # used in 'run_bg'
38
38
  stop_bg 'daemon'
39
+
40
+ # Intercept each call before executed. Can be used to modify the command, run
41
+ # something before it, or cancel it altogether.
42
+ # Your block receives the command as argument, and should return a command to
43
+ # run or false to stop execution.
44
+ before_run do |command|
45
+ cmd.gsub /^(rails|rake|cucumber)/, "bin/\\1"
46
+ end
47
+
48
+ # Intercept each call before it exits. Note this is only useful with `run` and
49
+ # `run_bg` but not with `run!` which exits after execution.
50
+ after_run do |command|
51
+ puts "Finished #{command}"
52
+ end
39
53
  ```
40
54
 
41
55
  ## About PID files ##
@@ -4,8 +4,6 @@
4
4
  # adds functions for running background tasks with ease.
5
5
 
6
6
  module RunfileExec
7
- @@pid_dir = nil
8
-
9
7
  def self.pid_dir=(dir)
10
8
  @@pid_dir = dir
11
9
  end
@@ -16,25 +14,31 @@ module RunfileExec
16
14
 
17
15
  # Run a command, wait until it is done and continue
18
16
  def run(cmd)
17
+ cmd = @before_run_block.call(cmd) if @before_run_block
18
+ return false unless cmd
19
19
  say "!txtgrn!> #{cmd}"
20
20
  system cmd
21
+ @after_run_block.call(cmd) if @after_run_block
21
22
  end
22
- module_function :run
23
23
 
24
24
  # Run a command, wait until it is done, then exit
25
25
  def run!(cmd)
26
+ cmd = @before_run_block.call(cmd) if @before_run_block
27
+ return false unless cmd
26
28
  say "!txtgrn!> #{cmd}"
27
29
  exec cmd
28
30
  end
29
- module_function :run!
30
31
 
31
32
  # Run a command in the background, optionally log to a log file and save
32
33
  # the process ID in a pid file
33
34
  def run_bg(cmd, pid: nil, log: '/dev/null')
35
+ cmd = @before_run_block.call(cmd) if @before_run_block
36
+ return false unless cmd
34
37
  full_cmd = "exec #{cmd} >#{log} 2>&1"
35
38
  say "!txtgrn!> #{full_cmd}"
36
39
  process = IO.popen "exec #{cmd} >#{log} 2>&1"
37
40
  File.write pidfile(pid), process.pid if pid
41
+ @after_run_block.call(cmd) if @after_run_block
38
42
  return process.pid
39
43
  end
40
44
 
@@ -51,6 +55,16 @@ module RunfileExec
51
55
  end
52
56
  end
53
57
 
58
+ # Set a block to be called before each run
59
+ def before_run(&block)
60
+ @before_run_block = block
61
+ end
62
+
63
+ # Set a block to be called after each run
64
+ def after_run(&block)
65
+ @after_run_block = block
66
+ end
67
+
54
68
  private
55
69
 
56
70
  def pidfile(pid)
@@ -1,3 +1,3 @@
1
1
  module RunfileExec
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: runfile-exec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-04 00:00:00.000000000 Z
11
+ date: 2016-01-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colsole