blob: 001d857d7bcfdbf19774d0f8964ed50045d002b1 [file]
/**
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
// Package model holds the process entities: a process, the batch-job specifics some
// processes carry, its status history and its tasks.
package model
import (
application "github.com/apache/airavata/api/application/model"
iammodel "github.com/apache/airavata/api/iam/model"
"github.com/google/uuid"
"gorm.io/gorm"
)
type TemplateInputMapping struct {
TemplateInputMappingID string `gorm:"column:template_input_mapping_id;primaryKey;type:varchar(36)" json:"templateInputMappingId"`
// The template this mapping belongs to.
TemplateInputID *string `gorm:"column:template_input_id;type:varchar(36);index" json:"templateInputId,omitempty"`
TemplateInput *application.TemplateInput `gorm:"references:ID;constraint:OnDelete:CASCADE,OnUpdate:CASCADE" json:"templateInput,omitempty"`
// Owned by BatchJobProcess.InputMappings, which declares the cascade; only the
// key is held here.
BatchProcessID *string `gorm:"column:batch_process_id;type:varchar(36);index" json:"batchProcessId,omitempty"`
// Value is a JSON document, either {"value": "..."} for a single value or
// {"values": [...]} for a list.
Value *string `gorm:"column:value;type:text" json:"value,omitempty"`
}
type TemplateOutputMapping struct {
TemplateOutputMappingID string `gorm:"column:template_output_mapping_id;primaryKey;type:varchar(36)" json:"templateOutputMappingId"`
// The template this mapping belongs to.
TemplateOutputID *string `gorm:"column:template_output_id;type:varchar(36);index" json:"templateOutputId,omitempty"`
TemplateOutput *application.TemplateOutput `gorm:"references:ID;constraint:OnDelete:CASCADE,OnUpdate:CASCADE" json:"templateOutput,omitempty"`
// Owned by BatchJobProcess.OutputMappings, which declares the cascade; only the
// key is held here.
BatchProcessID *string `gorm:"column:batch_process_id;type:varchar(36);index" json:"batchProcessId,omitempty"`
// Value is a JSON document, either {"value": "..."} for a single value or
// {"values": [...]} for a list.
Value *string `gorm:"column:value;type:text" json:"value,omitempty"`
}
// TrackInPlace is the value a file output mapping carries to ask that the output be
// registered where the job writes it, rather than copied to an existing data product.
// Launching swaps it for the id of the data product it registers.
const TrackInPlace = "TRACK_IN_PLACE"
type ProcessType string
const (
ProcessTypeBatchJob ProcessType = "BATCH_JOB"
ProcessTypeTask ProcessType = "CLOUD_JOB"
)
// Valid reports whether t is a recognised process type.
func (t ProcessType) Valid() bool {
switch t {
case ProcessTypeBatchJob, ProcessTypeTask:
return true
}
return false
}
// Process is one run: the single entity every other row in this package hangs off.
//
// What a run needs beyond an owner and a type depends on the kind of run it is, and
// that lives in a per-kind child rather than in columns here — BatchProcess for a
// BATCH_JOB, and whatever a later kind requires. The children are not resources of
// their own: a caller creates, reads and deletes them through the process.
type Process struct {
ID string `gorm:"column:process_id;primaryKey;type:varchar(36)" json:"processId"`
OwnerID *string `gorm:"column:user_id;type:varchar(255);index" json:"userId,omitempty"`
Owner *iammodel.User `gorm:"references:ID;constraint:OnDelete:RESTRICT,OnUpdate:CASCADE" json:"-"`
// The process type, e.g. "BATCH_JOB". This is a plain string rather than an enum
// because the set of process kinds is expected to grow.
ProcessType *ProcessType `gorm:"column:process_type;type:varchar(32)" json:"processType,omitempty"`
// The last status recorded for this process, if any.
//
// It carries no foreign key, deliberately. A status already points at its process,
// and a constraint in this direction as well would make the two tables mutually
// dependent — a cycle PostgreSQL cannot create, since it validates a referenced
// table exists when the referencing table is declared. This column is a cache of
// the newest status row, maintained by ProcessStatus.AfterCreate.
LastStatusID *string `gorm:"column:last_status_id;type:varchar(36);index" json:"lastStatusId,omitempty"`
// The last workflow recorded for this process, if any. Like LastStatusID it
// carries no foreign key, for the same reason, and is maintained by
// ProcessWorkflow.AfterCreate.
LastWorkflowID *string `gorm:"column:last_workflow_id;type:varchar(36);index" json:"lastWorkflowId,omitempty"`
// BatchProcess is present exactly when ProcessType is BATCH_JOB. It is owned by
// the process, not shared with it: deleting the process deletes it too.
BatchProcess *BatchJobProcess `gorm:"foreignKey:ProcessID;references:ID;constraint:OnDelete:CASCADE,OnUpdate:CASCADE" json:"batchProcess,omitempty"`
}
// TableName returns the table backing Process.
func (Process) TableName() string { return "processes" }
// TableName returns the table backing TemplateInputMapping.
func (TemplateInputMapping) TableName() string { return "process_template_input_mappings" }
// TableName returns the table backing TemplateOutputMapping.
func (TemplateOutputMapping) TableName() string { return "process_template_output_mappings" }
// BeforeCreate assigns a UUID when none was supplied.
func (p *Process) BeforeCreate(*gorm.DB) error {
if p.ID == "" {
p.ID = uuid.NewString()
}
return nil
}
// BeforeDelete removes what the database will not.
//
// The statuses and workflows have to go first: their own ProcessID foreign keys are
// RESTRICT, so the server would refuse to delete a process any of them still points at.
// LastStatusID and LastWorkflowID need no clearing — they hold no constraint — and the
// row carrying them is about to be deleted in this same transaction.
//
// The batch process is deleted through GORM rather than left to its ON DELETE CASCADE
// so that its own AfterDelete fires and takes the owned BatchJobConfig with it. A
// cascade in the database removes the row without ever calling into Go, which would
// leave the config orphaned. Deleting it also takes the template mappings, which hang
// off the batch process and cascade with it.
//
// The tasks need neither: their foreign key cascades and they own nothing outside
// themselves.
func (p *Process) BeforeDelete(tx *gorm.DB) error {
if err := tx.Where("process_id = ?", p.ID).Delete(&ProcessStatus{}).Error; err != nil {
return err
}
if err := tx.Where("process_id = ?", p.ID).Delete(&ProcessWorkflow{}).Error; err != nil {
return err
}
var batch []BatchJobProcess
if err := tx.Where("parent_process_id = ?", p.ID).Find(&batch).Error; err != nil {
return err
}
for i := range batch {
if err := tx.Delete(&batch[i]).Error; err != nil {
return err
}
}
return nil
}
// OwnedBy reports whether userID launched this process. A process with no owner is
// owned by nobody, not by the empty principal.
func (p *Process) OwnedBy(userID string) bool {
return p.OwnerID != nil && *p.OwnerID == userID
}
// BeforeCreate assigns a UUID when none was supplied.
func (m *TemplateInputMapping) BeforeCreate(*gorm.DB) error {
if m.TemplateInputMappingID == "" {
m.TemplateInputMappingID = uuid.NewString()
}
return nil
}
// BeforeCreate assigns a UUID when none was supplied.
func (m *TemplateOutputMapping) BeforeCreate(*gorm.DB) error {
if m.TemplateOutputMappingID == "" {
m.TemplateOutputMappingID = uuid.NewString()
}
return nil
}
type ProcessStatusType string
const (
ProcessStatusTypeCreated ProcessStatusType = "CREATED"
ProcessStatusTypeSubmitted ProcessStatusType = "SUBMITTED"
ProcessStatusTypeRunning ProcessStatusType = "RUNNING"
ProcessStatusTypeCompleted ProcessStatusType = "COMPLETED"
ProcessStatusTypeFailed ProcessStatusType = "FAILED"
)
type ProcessStatus struct {
ID string `gorm:"column:process_status_id;primaryKey;type:varchar(36)" json:"processStatusId"`
ProcessID *string `gorm:"column:process_id;type:varchar(36);index" json:"processId,omitempty"`
Process *Process `gorm:"references:ID;constraint:OnDelete:RESTRICT,OnUpdate:CASCADE" json:"-"`
Status *ProcessStatusType `gorm:"column:status;type:varchar(255)" json:"status,omitempty"`
Log *string `gorm:"column:log;type:text" json:"log,omitempty"`
// The time the status was recorded, in milliseconds since the epoch.
Timestamp *int64 `gorm:"column:timestamp" json:"timestamp,omitempty"`
}
// TableName returns the table backing ProcessStatus.
func (ProcessStatus) TableName() string { return "process_statuses" }
func (s *ProcessStatus) BeforeCreate(*gorm.DB) error {
if s.ID == "" {
s.ID = uuid.NewString()
}
return nil
}
func (s *ProcessStatus) AfterCreate(tx *gorm.DB) error {
if s.ProcessID == nil || *s.ProcessID == "" {
return nil
}
return tx.Model(&Process{}).
Where("process_id = ?", *s.ProcessID).
Update("last_status_id", s.ID).Error
}
type ProcessWorkflowType string
const (
ProcessWorkflowTypeBatchJobSubmission ProcessWorkflowType = "BATCH_JOB_SUBMISSION"
ProcessWorkflowTypeJobMonitoring ProcessWorkflowType = "JOB_MONITORING"
ProcessWorkflowTypeJobCompletion ProcessWorkflowType = "BATCH_JOB_COMPLETION"
)
type ProcessWorkflowStatusType string
const (
ProcessWorkflowStatusTypeRunning ProcessWorkflowStatusType = "RUNNING"
ProcessWorkflowStatusTypeSucceeded ProcessWorkflowStatusType = "SUCCEEDED"
ProcessWorkflowStatusTypeFailed ProcessWorkflowStatusType = "FAILED"
)
// ProcessWorkflow records one workflow run started on behalf of a process — the job
// submission, monitoring or completion workflow — by the id the workflow engine knows
// it by.
type ProcessWorkflow struct {
ID string `gorm:"column:process_workflow_id;primaryKey;type:varchar(36)" json:"processWorkflowId"`
ProcessID *string `gorm:"column:process_id;type:varchar(36);index" json:"processId,omitempty"`
Process *Process `gorm:"references:ID;constraint:OnDelete:RESTRICT,OnUpdate:CASCADE" json:"-"`
// Workflow id or monitoring task id
WorkflowID string `gorm:"column:workflow_id;type:varchar(36)" json:"workflowId"`
WorkflowType *ProcessWorkflowType `gorm:"column:workflow_type;type:varchar(255)" json:"workflowType,omitempty"`
// Where the run stands: RUNNING once started, then SUCCEEDED or FAILED.
Status *ProcessWorkflowStatusType `gorm:"column:status;type:varchar(255)" json:"status,omitempty"`
// The time the workflow was recorded, in milliseconds since the epoch.
Timestamp *int64 `gorm:"column:timestamp" json:"timestamp,omitempty"`
}
// TableName returns the table backing ProcessWorkflow.
func (ProcessWorkflow) TableName() string { return "process_workflows" }
func (p *ProcessWorkflow) BeforeCreate(*gorm.DB) error {
if p.ID == "" {
p.ID = uuid.NewString()
}
return nil
}
func (p *ProcessWorkflow) AfterCreate(tx *gorm.DB) error {
if p.ProcessID == nil || *p.ProcessID == "" {
return nil
}
return tx.Model(&Process{}).
Where("process_id = ?", *p.ProcessID).
Update("last_workflow_id", p.ID).Error
}