Skip to main content

Date Field Tasks

Date Field Tasks are scheduled tasks that are automatically triggered based on date-time field values in models. When the specified date-time field reaches the set time, the task logic is automatically executed. It is responsible for monitoring date-time fields in model data, automatically triggering execution when field values expire, and providing flexible time offset configuration, supporting precise time control for early or delayed execution.

The Date Field Task element has a hierarchical structure of Meta (tasks.Meta) → Type (tasks.DateFieldType) → Instance. Developers can quickly create date field task instance elements through JitAi's visual development tools.

Of course, developers can also create their own Type elements or modify the official tasks.DateFieldType element provided by JitAi in their own App to implement their own encapsulation.

Quick Start

Creating Instance Elements

Directory Structure

tasks/
└── ExampleDateTask/ # Task name, customizable
├── e.json # Element configuration file
├── inner.py # Task execution logic (optional)
└── __init__.py # Package initialization file

e.json File

e.json
{
"type": "tasks.DateFieldType",
"title": "Example Date Field Task",
"funcType": "Inner",
"modelPath": "models.OrderModel",
"timerCfg": {
"startField": "deliveryTime",
"startOffset": {
"offsetType": 1,
"offset": 0,
"offsetUnit": "hours"
},
"repeat": {
"repeatType": "day",
"period": 1
},
"endTimeType": 0,
"skipHoliday": 1
},
"enable": 1,
"backendBundleEntry": "."
}

Business Logic Code

inner.py
def main(app, taskInstance, rowData):
"""
Task execution main function

Args:
app: Application instance
taskInstance: Task instance
rowData: Data row that triggered the task
"""
# Get order information
order_id = rowData.id
delivery_time = rowData.deliveryTime

# Execute business logic
print(f"Order {order_id} delivery time {delivery_time} has arrived")

# Can call other services for processing
# service = app.getElement("services.NotificationService")
# service.sendDeliveryNotification(order_id)

Usage Example

Using Date Field Tasks
# Get task instance
task = app.getElement("tasks.ExampleDateTask")

# Task will automatically monitor deliveryTime field in OrderModel
# When field value reaches the set time, automatically execute main function in inner.py
# System will calculate actual execution time based on timerCfg.startOffset configuration

Element Configuration

e.json Configuration

Parameter NameTypeRequiredDefault ValueDescription
titlestringYes-Task title
typestringYes-Must be tasks.DateFieldType
funcTypestringYes-Function type, fixed as Inner
modelPathstringYes-FullName of target model
timerCfgobjectYes-Timer configuration object
timerCfg.startFieldstringYes-Date-time field name in target model
timerCfg.startOffsetobjectNo-Start time offset configuration
timerCfg.startOffset.offsetintNo0Time offset amount, can be negative
timerCfg.startOffset.offsetUnitstringNohoursTime offset unit: seconds/minutes/hours/days
timerCfg.startOffset.offsetTypeintNo1Offset type, fixed as 1
timerCfg.repeatobjectNo-Repeat configuration
timerCfg.endTimeTypeintNo0End time type
timerCfg.skipHolidayintNo1Whether to skip holidays, 1 skip, 0 don't skip
enableintNo1Whether to enable, 1 enable, 0 disable
backendBundleEntrystringNo"."Backend code entry directory

Business Configuration File Configuration

Date Field Tasks do not require additional business configuration files, all configuration is completed in e.json.

Methods

main

Main method for task execution, defined in inner.py.

Parameter Details

Parameter NameJitAi TypeNative TypeRequiredDescription
appAppobjectYesApplication instance, used to get other elements
taskInstanceTaskInstanceobjectYesCurrent task instance
rowDataRowDataobjectYesData row object that triggered the task

Return Value

No return value required, function completion indicates task completion.

Usage Example

Processing Order Delivery Reminders
def main(app, taskInstance, rowData):
# Get order data
order_id = rowData.id
customer_name = rowData.customerName
delivery_time = rowData.deliveryTime

# Get notification service
notification_service = app.getElement("services.NotificationService")

# Send delivery reminder
notification_service.sendNotification({
"type": "delivery_reminder",
"orderId": order_id,
"customerName": customer_name,
"deliveryTime": delivery_time
})

# Update order status
order_model = app.getElement("models.OrderModel")
order_model.id = order_id
order_model.status = "notified"
order_model.save()

Attributes

config

Task configuration object, containing all configuration information from e.json, read-only attribute.

TaskModel

Task model instance, inherited from parent class, used for managing task records.

TaskHistoryModel

Task history model instance, inherited from parent class, used for recording task execution history.

Advanced Features

Time Offset Configuration

Flexible time offset can be achieved through offset and offsetUnit parameters:

Early Execution Configuration

Execute 1 Hour Early
{
"type": "tasks.DateFieldType",
"modelPath": "models.MeetingModel",
"timerCfg": {
"startField": "startTime",
"startOffset": {
"offsetType": 1,
"offset": -1,
"offsetUnit": "hours"
}
}
}

Delayed Execution Configuration

Execute 30 Minutes Later
{
"type": "tasks.DateFieldType",
"modelPath": "models.TaskModel",
"timerCfg": {
"startField": "deadline",
"startOffset": {
"offsetType": 1,
"offset": 30,
"offsetUnit": "minutes"
}
}
}

Multiple Time Units

Second-level Offset
{
"timerCfg": {
"startOffset": {
"offset": 5,
"offsetUnit": "seconds"
}
}
}
Day-level Offset
{
"timerCfg": {
"startOffset": {
"offset": -1,
"offsetUnit": "days"
}
}
}

Complex Business Logic Processing

Comprehensive Business Processing Example
def main(app, taskInstance, rowData):
# Get related services
email_service = app.getElement("services.EmailService")
sms_service = app.getElement("services.SmsService")
log_service = app.getElement("services.LogService")

try:
# Record task start
log_service.info(f"Starting to process delivery reminder for order {rowData.id}")

# Check customer preference settings
customer_model = app.getElement("models.CustomerModel")
customer = customer_model.get(f"Q(id={rowData.customerId})", [])

# Choose notification method based on preference
if customer.notificationPreference == "email":
email_service.sendDeliveryReminder(rowData)
elif customer.notificationPreference == "sms":
sms_service.sendDeliveryReminder(rowData)
else:
# Default to email
email_service.sendDeliveryReminder(rowData)

# Update notification status
order_model = app.getElement("models.OrderModel")
order_model.id = rowData.id
order_model.notificationSent = True
order_model.notificationTime = app.getElement("datatypes.Datetime")().getValue()
order_model.save()

log_service.info(f"Delivery reminder for order {rowData.id} sent successfully")

except Exception as e:
log_service.error(f"Error occurred while processing delivery reminder for order {rowData.id}: {str(e)}")
raise
JitAI AssistantBeta
Powered by JitAI