Skip to content

Commit 0982873

Browse files
committed
Spider - tweak life cycle hooks
The life cycle hooks as they stand don't particularly sit well with the architecture, but do currently perform an important functionality and work. So, rather than making radical changes, just add a bit more safety, checking and logging. This stuff will likely just be removed after a sufficient amount of movement of functionality over to tau.
1 parent bea5ec4 commit 0982873

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

app/server/ruby/lib/sonicpi/lifecyclehooks.rb

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111
# notice is included.
1212
#++
1313

14+
require_relative "util"
15+
1416
module SonicPi
1517
class LifeCycleHooks
1618

19+
include Util
20+
1721
# Life cycle hooks for jobs
1822

1923
def initialize
@@ -23,65 +27,91 @@ def initialize
2327
@exited_blocks = []
2428
@all_completed_blocks = []
2529
@mut = Mutex.new
30+
@started = false
31+
end
32+
33+
def ensure_not_started!
34+
if @started
35+
raise StandardError "Cannot modify life cycle hooks once started"
36+
end
2637
end
2738

2839
def on_init(&blck)
2940
@mut.synchronize do
41+
ensure_not_started!
3042
@started_blocks << blck
3143
end
3244
end
3345

3446
def on_completed(&blck)
3547
@mut.synchronize do
48+
ensure_not_started!
3649
@completed_blocks << blck
3750
end
3851
end
3952

4053
def on_killed(&blck)
4154
@mut.synchronize do
55+
ensure_not_started!
4256
@killed_blocks << blck
4357
end
4458
end
4559

4660
def on_exit(&blck)
4761
@mut.synchronize do
62+
ensure_not_started!
4863
@exited_blocks << blck
4964
end
5065
end
5166

5267
def on_all_completed(&blck)
5368
@mut.synchronize do
69+
ensure_not_started!
5470
@all_completed_blocks << blck
5571
end
5672
end
5773

5874
def init(job_id, arg={})
59-
@started_blocks.each do |b|
60-
b.call(job_id, arg)
75+
log "Lifecycle Hooks Init"
76+
@mut.synchronize do
77+
@started = true
78+
@started_blocks.each do |b|
79+
b.call(job_id, arg)
80+
end
6181
end
6282
end
6383

6484
def completed(job_id, arg={})
65-
@completed_blocks.each do |b|
66-
b.call(job_id, arg)
85+
log "Lifecycle Hooks Completed"
86+
@mut.synchronize do
87+
@completed_blocks.each do |b|
88+
b.call(job_id, arg)
89+
end
6790
end
6891
end
6992

7093
def all_completed(silent=false)
71-
@all_completed_blocks.each do |b|
72-
b.call(silent)
94+
log "Lifecycle Hooks All Completed"
95+
@mut.synchronize do
96+
@all_completed_blocks.each do |b|
97+
b.call(silent)
98+
end
7399
end
74100
end
75101

76102
def killed(job_id, arg={})
77-
@killed_blocks.each do |b|
78-
b.call(job_id, arg)
103+
@mut.synchronize do
104+
@killed_blocks.each do |b|
105+
b.call(job_id, arg)
106+
end
79107
end
80108
end
81109

82110
def exit(job_id, arg={})
83-
@exited_blocks.each do |b|
84-
b.call(job_id, arg)
111+
@mut.synchronize do
112+
@exited_blocks.each do |b|
113+
b.call(job_id, arg)
114+
end
85115
end
86116
end
87117
end

0 commit comments

Comments
 (0)