Cron
The Cron module provides a comprehensive job scheduling system for FiveM resources with support for both cron expressions and interval-based scheduling.
schedule
Creates a job using a cron expression.
B2Lib.Cron.schedule(expression, callback, options)Parameters:
expression(string): Cron expression (minute hour day month weekday)callback(function): Function to executeoptions(table, optional): Job configuration optionsname(string): Job name for identificationcontext(table): Data passed to callbackmaxRuns(number): Maximum number of executionstimeout(number): Execution timeout in millisecondsmaxRetries(number): Retry attempts on failureretryDelay(number): Delay between retries in millisecondspriority(number): Job priority (higher = more important)tags(table): Array of tags for organizationonError(function): Error callbackonComplete(function): Completion callback
Returns: string - Job ID for management
Example:
every
Creates a job that runs at regular intervals.
Parameters:
interval(number): Interval in millisecondscallback(function): Function to executemaxRuns(number, optional): Maximum number of executionsoptions(table, optional): Additional job options
Returns: string - Job ID for management
Example:
daily
Creates a job that runs daily at a specific time.
Parameters:
hour(number): Hour (0-23)minute(number): Minute (0-59)callback(function): Function to executeoptions(table, optional): Additional job options
Returns: string - Job ID for management
Example:
weekly
Creates a job that runs weekly on a specific day and time.
Parameters:
weekday(number): Day of week (0=Sunday, 1=Monday, etc.)hour(number): Hour (0-23)minute(number): Minute (0-59)callback(function): Function to executeoptions(table, optional): Additional job options
Returns: string - Job ID for management
Example:
monthly
Creates a job that runs monthly on a specific day and time.
Parameters:
day(number): Day of month (1-31)hour(number): Hour (0-23)minute(number): Minute (0-59)callback(function): Function to executeoptions(table, optional): Additional job options
Returns: string - Job ID for management
Example:
after
Creates a one-time job that runs after a delay.
Parameters:
delay(number): Delay in millisecondscallback(function): Function to executeoptions(table, optional): Additional job options
Returns: string - Job ID for management
Example:
setActive
Pauses or resumes a job.
Parameters:
jobId(string): The job ID to modifyactive(boolean): True to activate, false to pause
Returns: boolean - True if successful, false if job not found
remove
Removes a job from the scheduler.
Parameters:
jobId(string): The job ID to remove
Returns: boolean - True if successfully removed, false if job not found
executeNow
Manually executes a job immediately.
Parameters:
jobId(string): The job ID to execute
Returns: boolean - True if executed, false if job not found
getJob
Gets information about a specific job.
Parameters:
jobId(string): The job ID to query
Returns: table|nil - Job information or nil if not found
id(string): Job IDname(string): Job nametype(string): Job type (cron, interval, etc.)active(boolean): Whether job is activerunCount(number): Number of times executedlastRun(number): Last execution timestampnextRun(number): Next execution timestampaverageExecutionTime(number): Average execution time in ms
getAllJobs
Gets information about all jobs.
Returns: table - Array of job information objects
updateContext
Updates the context data for a job.
Parameters:
jobId(string): The job ID to updatenewContext(table): New context data
Returns: boolean - True if updated, false if job not found
getStats
Gets scheduler statistics.
Returns: table - Scheduler statistics
totalJobs(number): Total number of jobsactiveJobs(number): Number of active jobstotalExecutions(number): Total executions across all jobsaverageExecutionTime(number): Average execution timememoryUsage(number): Estimated memory usage
cleanup
Removes completed and expired jobs.
Returns: number - Number of jobs cleaned up
getExpressions
Gets predefined cron expressions.
Returns: table - Table of predefined expressions
EVERY_MINUTE: "* * * * *"EVERY_5_MINUTES: "*/5 * * * *"EVERY_15_MINUTES: "*/15 * * * *"EVERY_30_MINUTES: "*/30 * * * *"EVERY_HOUR: "0 * * * *"EVERY_DAY: "0 0 * * *"WEEKDAYS: "0 9 * * 1-5"BUSINESS_HOURS: "0 9-17 * * 1-5"MIDNIGHT: "0 0 * * *"NOON: "0 12 * * *"
Example:
Configuration
Configure the cron system in config.lua:
Cron Expression Format
Cron expressions use five fields: minute hour day month weekday
Field Ranges
Minute: 0-59
Hour: 0-23 (24-hour format)
Day: 1-31
Month: 1-12
Weekday: 0-6 (0=Sunday, 1=Monday, etc.)
Special Characters
*- Any value (wildcard),- List separator (e.g.,1,3,5)-- Range (e.g.,1-5)/- Step values (e.g.,*/5or10-20/2)
Common Examples
* * * * *- Every minute*/5 * * * *- Every 5 minutes0 * * * *- Every hour0 0 * * *- Every day at midnight0 9 * * 1-5- Weekdays at 9 AM0 0 1 * *- First day of every month
Troubleshooting
Jobs Not Executing
Check that the cron expression is valid
Verify the job is active with
getJob()Ensure the callback function doesn't throw errors
Check console for error messages
Performance Issues
Limit the number of concurrent jobs
Use appropriate intervals to avoid excessive execution
Monitor execution times with
getStats()Clean up completed jobs regularly
Memory Leaks
Remove jobs when no longer needed
Enable automatic cleanup in configuration
Monitor job count with
getAllJobs()Use
cleanup()to remove expired jobs
Timing Issues
Verify server time is correct for cron expressions
Account for server timezone in scheduling
Use intervals for precise timing requirements
Test with
executeNow()for immediate verification
Last updated