Class: Bricolage::DAO::JobNet

Inherits:
Object
  • Object
show all
Includes:
SQLUtils
Defined in:
lib/bricolage/dao/jobnet.rb

Defined Under Namespace

Classes: Attributes

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(datasource) ⇒ JobNet

Returns a new instance of JobNet.



21
22
23
# File 'lib/bricolage/dao/jobnet.rb', line 21

def initialize(datasource)
  @datasource = datasource
end

Class Method Details

.for_record(r) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/bricolage/dao/jobnet.rb', line 9

def JobNet.for_record(r)
  Attributes.new(
    id: r['jobnet_id']&.to_i,
    subsystem: r['subsystem'],
    jobnet_name: r['jobnet_name']
  )
end

.for_records(jobnets) ⇒ Object



17
18
19
# File 'lib/bricolage/dao/jobnet.rb', line 17

def JobNet.for_records(jobnets)
  jobnets.map {|jobnet| JobNet.for_record(jobnet) }
end

Instance Method Details

#clear_lock(jobnet_id) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/bricolage/dao/jobnet.rb', line 143

def clear_lock(jobnet_id)
  connect {|conn|
    conn.execute_update(<<~EndSQL)
      update jobnets
      set
          executor_id = null
      where
          jobnet_id = #{jobnet_id}
      ;
    EndSQL
  }
end

#find_or_create(ref) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bricolage/dao/jobnet.rb', line 29

def find_or_create(ref)
  connect {|conn|
    jobnet = find(conn, ref)
    if jobnet
      return jobnet
    else
      begin
        return create(conn, ref)
      rescue UniqueViolationException
        jobnet = find(conn, ref) or raise "[BUG] Could not create jobnet record: #{ref}"
        return jobnet
      end
    end
  }
end

#lock(jobnet_id, executor_id) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/bricolage/dao/jobnet.rb', line 105

def lock(jobnet_id, executor_id)
  records = connect {|conn|
    conn.execute_update(<<~EndSQL)
      update jobnets
      set
          executor_id = #{s executor_id}
      where
          jobnet_id = #{jobnet_id}
          and executor_id is null
      returning jobnet_id
      ;
    EndSQL
  }
  if records.empty?
    raise DoubleLockError, "Could not lock jobnet: jobnet_id=#{jobnet_id}"
  end
end

#locked?(ref) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/bricolage/dao/jobnet.rb', line 87

def locked?(ref)
  value = connect {|conn|
    conn.query_value(<<~EndSQL)
      select
          count(*)
      from
          jobnets
      where
          "subsystem" = #{s ref.subsystem}
          and jobnet_name = #{s ref.name}
          and executor_id is not null
      ;
    EndSQL
  }

  value.to_i > 0
end

#unlock(jobnet_id, executor_id) ⇒ Object

Unlock jobnet lock. Returns true if unlocked successfully, otherwise false. FIXME: raise exception?



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/bricolage/dao/jobnet.rb', line 126

def unlock(jobnet_id, executor_id)
  records = connect {|conn|
    conn.execute_update(<<~EndSQL)
      update jobnets
      set
          executor_id = null
      where
          jobnet_id = #{jobnet_id}
          and executor_id = #{s executor_id}
      returning jobnet_id
      ;
    EndSQL
  }

  not records.empty?
end