JOBS
MindsDB enables you to automate any pipeline using JOBS, which grant you the power to schedule any query at any frequency. Additionally, it introduces the keyword LAST, offering the capability for a JOB to act solely on new data, essentially treating any data source as a stream.
Description
The CREATE JOB
statement lets you schedule the execution of queries by providing relevant parameters, such as start date, end date, or repetition frequency.
Syntax
CREATE JOB
Here is the syntax:
Where:
Expression | Description |
---|---|
[project_name.]job_name | Name of the job preceded by an optional project name where the job is to be created. If you do not provide the project_name value, then the job is created in the default mindsdb project. |
<statement_1>[; <statement_2>][; ...] | One or more statements separated by ; to be executed by the job. |
[START <date>] | Optional. The date when the job starts its periodical or one-time execution. If not set, it is the current system date. |
[END <date>] | Optional. The date when the job ends its periodical or one-time execution. If it is not set (and the repetition rules are set), then the job repeats forever. |
[EVERY [number] <period>] | Optional. The repetition rules for the job. If not set, the job runs once, not considering the end date value. If the number value is not set, it defaults to 1. |
[IF (<statement_1>[; <statement_2>][; ...])] | Optional. If the last statement returns one or more rows, only then the job will execute. |
Available <date>
formats
Here are the supported <date>
formats:
'%Y-%m-%d %H:%M:%S'
'%Y-%m-%d'
Please note that the default time zone is UTC.
Available <period>
values
And the supported <period>
values:
minute
/minutes
/min
hour
/hours
day
/days
week
/weeks
month
/months
Further, you can query all jobs and their execution history like this:
LAST
MindsDB provides a custom LAST
keyword that enables you to fetch data inserted/updated after the last time you queried for it. It is a convenient way to select only the newly added data rows when running a job.
Imagine you have the fruit_data
table that contains the following:
When you run the SELECT
query with the LAST
keyword for the first time, it’ll give an empty output.
This query returns:
Now imagine you inserted a new record into the fruit_data
table:
When you run the SELECT
query with the LAST
keyword again, you’ll get only the newly added record as output.
This query returns:
From this point on, whenever you add new records into the fruit_data
table, it’ll be returned by the next run of the SELECT
query with the LAST
keyword. And, if you do not add any new records between the query runs, then the output will be null.
Examples
Example 1: Retrain a Model
In this example, we create a job in the current project to retrain the home_rentals_model
model and insert predictions into the rentals
table.
Please note that the join_learn_process
parameter in the USING
clause of the RETRAIN
statement ensures that the retraining process completes before inserting predictions into a table. In general, this parameter is used to prevent several retrain processes from running simultaneously.
The retrain_model_and_save_predictions
job starts its execution on the current system date and ends on the 1st of April 2023. The job is executed every 2 days.
Example 2: Save Predictions
In this example, the job creates a table named as result_{{START_DATETIME}}
and inserts predictions into it.
Please note that the uniqueness of the created table name is ensured here by using the {{START_DATETIME}}
variable that is replaced at runtime by the date and time of the current run.
You can use the following variables for this purpose:
PREVIOUS_START_DATETIME
is replaced by date and time of the previous run of this job.START_DATETIME
is replaced by date and time of the current job run.START_DATE
is replaced by date of the current job run.
The save_predictions
job starts its execution on the current system date and repeats every 2 hours until it is manually disabled.
Example 3: Drop a Model
In this example, we create a job to drop the home_rentals_model
model scheduled on the 1st of April 2023.
This job runs once on the 1st of April 2023.
Example 4: Twitter Chatbot
You can easily create a chatbot to respond to tweets using jobs. But before you get to it, you should connect your Twitter account to MindsDB following the instructions here.
Follow the tutorial on how to create a Twitter chatbot to learn the details.
Let’s create a job that runs every hour, checks for new tweets, and responds using the OpenAI model.
The SELECT
statement joins the data table with the model table to get responses for newly posted tweets, thanks to the LAST
keyword. Then, the INSERT INTO
statement writes these responses to the tweets
table of the my_twitter
integration.
To learn more about OpenAI integration with MindsDB, visit our docs here.
Additional Configuration
Here is the template of the config.json
file that you can pass as a parameter when starting your local MindsDB instance:
The disable
parameter defines whether the scheduler is active (true
) or not (false
). By default, in the MindsDB Cloud Editor, the scheduler is active.
The check_interval
parameter defines the interval in seconds between consecutive checks of the scheduler table. By default, in the MindsDB Cloud Editor, it is 30 seconds.
You can modify the default configuration in your local MindsDB installation by creating a config.json
file and starting MindsDB with this file as a parameter. You can find detailed instructions here.