     :db/cardinality :db.cardinality/one
     :db.install/_attribute :db.part/db}
 
-    ;; Resource mapping attributes
+   ;; Resource mapping attributes
    {:db/id (d/tempid :db.part/db)
     :db/ident :resource.type/mesos-name
     :db/valueType :db.type/keyword

                     ;; Checking the validity of the target state transition
                     (when (get-in state-transitions [old-state new-state])
                       (into [[:db/add instance :instance/status new-state]]
                         (let [instance-ent (d/entity db instance)
                               job-ent (:job/_instance instance-ent)
                               job (:db/id job-ent)
                               other-instances (->> (:job/instance job-ent)
                                                    (remove #(= (:db/id %) (:db/id instance-ent))))
                               instance-states (->> other-instances
                                                    (map :instance/status)
                                                    (cons new-state))
                               any-success? (some #{:instance.status/success} instance-states)
                               any-running? (some #{:instance.status/running} instance-states)
                               any-unknown? (some #{:instance.status/unknown} instance-states)
                               all-failed? (every? #{:instance.status/failed} instance-states)
                               prior-state (:job/state job-ent)
                               reason (d/entity db reason)
                               all-attempts-consumed?
                               (d/invoke db :job/all-attempts-consumed? db
                                         (update-in (into {} job-ent) [:job/instance]
                                                    conj {:instance/status new-state
                                                          :instance/reason reason}))]
                           (cond
                             (= prior-state :job.state/completed)
                             []

                             (or (and all-failed?
                                      all-attempts-consumed?)
                                 any-success?)
                             [[:db/add job :job/state :job.state/completed]]

                             (or any-running?
                                 any-unknown?)
                             [[:db/add job :job/state :job.state/running]]

                             :else
                             [[:db/add job :job/state :job.state/waiting]])))))}}

   {:db/id (d/tempid :db.part/user)
    :db/ident :job/allowed-to-start?

                         mea-culpa-limit (or (when (:job/disable-mea-culpa-retries job-ent)
                                               0)
                                             (:scheduler.config/mea-culpa-failure-limit
                                               (d/entity db :scheduler/config))
                                             5)]
                     (->> job-ent
                          :job/instance
