Skip to Content

gofasta g job

Generates a background job file for handling recurring work such as sending emails, processing uploads, generating reports, or any task that should run on a schedule. Jobs implement the scheduler.Job interface with Name() and Run() methods, and use GORM for database access and slog for structured logging.

Usage

gofasta g job <Name> [schedule]

The optional second positional argument specifies a cron schedule expression. This command has no flags.

Examples

Generate a job for sending welcome emails:

gofasta g job SendWelcomeEmail

Generate a job with a specific cron schedule:

gofasta g job GenerateMonthlyReport "0 0 1 * *"

Generate a job that runs every 5 minutes:

gofasta g job SyncExternalData "*/5 * * * *"

What It Generates

Running gofasta g job SendWelcomeEmail creates one file and patches two:

FileDescription
app/jobs/send_welcome_email.goJob implementation (created)
cmd/serve.goRegisters the job in the job registry (patched)
config/config.yamlAdds the job schedule entry (patched)

Generated Code

// app/jobs/send_welcome_email.go package jobs import ( "context" "log/slog" "gorm.io/gorm" ) // SendWelcomeEmailJob handles sending welcome emails on a schedule. type SendWelcomeEmailJob struct { db *gorm.DB } // NewSendWelcomeEmailJob creates a new instance of SendWelcomeEmailJob. func NewSendWelcomeEmailJob(db *gorm.DB) *SendWelcomeEmailJob { return &SendWelcomeEmailJob{db: db} } // Name returns the unique name of this job. func (j *SendWelcomeEmailJob) Name() string { return "SendWelcomeEmail" } // Run executes the job logic. func (j *SendWelcomeEmailJob) Run(ctx context.Context) error { slog.Info("Running SendWelcomeEmail job") // TODO: Implement job logic here return nil }

Job Interface

All generated jobs implement the scheduler.Job interface:

type Job interface { Name() string Run(ctx context.Context) error }
  • Name() returns a unique identifier used for logging and the job registry
  • Run() contains the job logic and receives a context for cancellation support

Cron Expression Format

The schedule uses standard cron syntax with five fields:

* * * * * | | | | | | | | | +-- Day of week (0-6, Sunday=0) | | | +---- Month (1-12) | | +------ Day of month (1-31) | +-------- Hour (0-23) +---------- Minute (0-59)
ExpressionDescription
0 * * * *Every hour at minute 0
0 0 * * *Daily at midnight
0 9 * * 1Every Monday at 9:00 AM
*/5 * * * *Every 5 minutes
0 0 1 * *First day of every month at midnight
Last updated on