From 9d774f0a0dabfb97746c1721faffd9ca3a23ef2a Mon Sep 17 00:00:00 2001 From: Volker Schukai <volker.schukai@schukai.com> Date: Thu, 16 Nov 2023 00:01:31 +0100 Subject: [PATCH] feat: new runJob method #32 --- job-log.go | 22 +++++++++++----------- job-stat.go | 14 +++++++------- manager.go | 18 ++++++++++++++++++ persistence.go | 4 ++-- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/job-log.go b/job-log.go index 53b8bb5..a59ef8f 100644 --- a/job-log.go +++ b/job-log.go @@ -6,20 +6,20 @@ import ( ) type JobLog struct { - LogID uint `gorm:"primarykey;autoIncrement:true" json:"log_id"` - JobID JobID `json:"job_id" gorm:"type:varchar(255);foreignKey:JobID;references:ID"` - ProcessID int `json:"process_id"` - StartTime time.Time `json:"start_time"` - EndTime time.Time `json:"end_time"` - ExitCode int `json:"exit_code"` + LogID uint `gorm:"primarykey;autoIncrement:true" json:"logId"` + JobID JobID `json:"jobId" gorm:"type:varchar(255);foreignKey:JobID;references:ID"` + ProcessID int `json:"processId"` + StartTime time.Time `json:"startTime"` + EndTime time.Time `json:"endTime"` + ExitCode int `json:"exitCode"` Result string `json:"output" gorm:"type:TEXT"` // Assuming serialized JSON for any type - ResourceUsage ResourceUsage `json:"resource_usage" gorm:"embedded;embeddedPrefix:resource_usage_"` + ResourceUsage ResourceUsage `json:"resourceUsage" gorm:"embedded;embeddedPrefix:resource_usage_"` IO IO `json:"io" gorm:"embedded;embeddedPrefix:io_"` - ErrorMsg string `json:"error_msg"` - IsSuccessful bool `json:"is_successful"` + ErrorMsg string `json:"errorMsg"` + IsSuccessful bool `json:"isSuccessful"` - CreatedAt time.Time `gorm:"column:created_at" json:"created_at" yaml:"created_at"` - UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" yaml:"updated_at"` + CreatedAt time.Time `gorm:"column:created_at" json:"createdAt" yaml:"createdAt"` + UpdatedAt time.Time `gorm:"column:updated_at" json:"updatedAt" yaml:"updatedAt"` DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;index" json:"-" yaml:"-"` } diff --git a/job-stat.go b/job-stat.go index 2a784cc..75c643a 100644 --- a/job-stat.go +++ b/job-stat.go @@ -7,14 +7,14 @@ import ( // important: if you want to add fields to this struct, you have to add them to the ResetStats() method as well type JobStats struct { - JobID JobID `json:"job_id" gorm:"primaryKey"` - RunCount int `json:"run_count"` - SuccessCount int `json:"success_count"` - ErrorCount int `json:"error_count"` - TimeMetrics TimeMetrics `json:"time_metrics" gorm:"embedded;embeddedPrefix:time_metrics_"` + JobID JobID `json:"jobId" gorm:"primaryKey"` + RunCount int `json:"runCount"` + SuccessCount int `json:"successCount"` + ErrorCount int `json:"errorCount"` + TimeMetrics TimeMetrics `json:"timeMetrics" gorm:"embedded;embeddedPrefix:time_metrics_"` - CreatedAt time.Time `gorm:"column:created_at" json:"created_at" yaml:"created_at"` - UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" yaml:"updated_at"` + CreatedAt time.Time `gorm:"column:created_at" json:"createdAt" yaml:"createdAt"` + UpdatedAt time.Time `gorm:"column:updated_at" json:"updatedAt" yaml:"updatedAt"` DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;index" json:"-" yaml:"-"` } diff --git a/manager.go b/manager.go index 99c60c0..9056c3c 100644 --- a/manager.go +++ b/manager.go @@ -481,12 +481,30 @@ func (m *Manager) SetLogger(logger Logger) *Manager { return m } +// GetLogger returns the logger func (m *Manager) GetLogger() Logger { m.mu.Lock() defer m.mu.Unlock() return m.logger } +// RunJob runs a job immediately and does not schedule it +func (m *Manager) RunJob(job GenericJob) error { + m.mu.Lock() + defer m.mu.Unlock() + + if m.state != ManagerStateRunning { + return ErrManagerNotRunning + } + + if !job.IsPaused() { + m.eventBus.Publish(QueueJob, job) + } + + return nil + +} + // ScheduleJob schedules a job func (m *Manager) ScheduleJob(job GenericJob, scheduler Scheduler) error { m.mu.Lock() diff --git a/persistence.go b/persistence.go index 1921cb8..9b09bed 100644 --- a/persistence.go +++ b/persistence.go @@ -30,8 +30,8 @@ type JobPersistence struct { Logs []JobLog `gorm:"foreignKey:JobID;references:ID" json:"-" yaml:"-"` Stats JobStats `gorm:"foreignKey:JobID" json:"stats" yaml:"stats"` - CreatedAt time.Time `gorm:"column:created_at" json:"created_at" yaml:"created_at"` - UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" yaml:"updated_at"` + CreatedAt time.Time `gorm:"column:created_at" json:"createdAt" yaml:"createdAt"` + UpdatedAt time.Time `gorm:"column:updated_at" json:"updatedAt" yaml:"updatedAt"` DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;index" json:"-" yaml:"-"` } -- GitLab