1# Work Scheduler
2
3-   [Introduction](#section11660541593)
4-   [Directory Structure](#section161941989596)
5-   [Work Scheduler](#section1312121216216)
6    -   [Available APIs](#section114564657874)
7    -   [Usage Guidelines](#section129654513264)
8        -   [Restrictions on Using Work Scheduler](#section1551164914237)
9
10-   [Repositories Involved](#section1371113476307)
11
12## Introduction<a name="section11660541593"></a>
13
14When an application needs to perform tasks that do not require high real-time performance, such as data learning when the device is idle, the work scheduler can be used. When the application setting conditions are met, the mechanism will uniformly decide the scheduling time according to the current state of the system, such as memory, power consumption, heat, etc.
15
16![](figures/en-us_image001.png)
17
18## Directory Structure<a name="section161941989596"></a>
19
20```
21
22├── frameworks       # Frameworks
23├── interfaces
24│   └── kits         # External APIs
25├── sa_profile       # SA profile
26├── services         # Services
27└── utils            # Utilities
28
29```
30## Work Scheduler<a name="section1312121216216"></a>
31
32### Available APIs<a name="section114564657874"></a>
33
341. The members of WorkInfo
35
36 API                                                          | Description | Type
37  ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------
38workId | The id of work(required)|number
39bundleName | The bundle name(required)|string
40abilityName | The ability name(required)|string
41networkType | The type of network | NetworkType
42isCharging | Is charging. | bool
43chargerType | The charging type. | ChargingType
44batteryLevel | The  battery | number
45batteryStatus| The battery status |	BatteryStatus
46storageRequest|The storage status|	StorageRequest
47isRepeat|Is repeating work |	boolean
48repeatCycleTime | The repeating cycle time|	number
49repeatCount	| The repeating cycle count| number
50parameters    |Parameter information carried| {[key: string]: any}
51
52
532. The api of work scheduler for starting, stopping and querying.
54
55 API                                                          | Description
56 ------------------------------------------------------------ | ------------------------------------------------------------
57 function startWork(work: WorkInfo): void;                 | work scheduler application
58 function stopWork(work: WorkInfo, needCancel?: boolean): void; | work scheduler cancel
59 function getWorkStatus(workId: number, callback: AsyncCallback<WorkInfo>): void; | get the status of work(Callback form)
60 function getWorkStatus(workId: number): Promise<WorkInfo>;   | get the status of work(Promise form)
61 function obtainAllWorks(callback: AsyncCallback<void>): Array<WorkInfo>; | get all works(Callback form)
62 function obtainAllWorks(): Promise<Array<WorkInfo>>;         | get all works(Promise form)
63 function stopAndClearWorks(): void;                       | stop and clear work
64 function isLastWorkTimeOut(workId: number, callback: AsyncCallback<void>): boolean; | Get whether the last task has timed out(For repeat work. Callback form)
65 function isLastWorkTimeOut(workId: number): Promise<boolean>; | Get whether the last task has timed out(For repeat work. Promise form)
66
673. Callback api of work scheduler
68
69API                                                          | Description
70 ------------------------------------------------------------ | ------------------------------------------------------------
71 function onWorkStart(work: WorkInfo): void;                 | Callback when work starts.
72 function onWorkStop(work: WorkInfo): void;                 | Callback when work stops.
73
74### Usage Guidelines<a name="section129654513264"></a>
75
76When an application needs to perform tasks with low real-time performance, work scheduler can be used. When the conditions set by the application are met, the mechanism will make a unified decision and scheduling time based on the current state of the system, such as memory, power consumption, and heat.
77
78#### Restrictions on Using Work Scheduler<a name="section1551164914237"></a>
79
80Adhere to the following constraints and rules when using work scheduler:
81
82- **Timeout**:The longest running time is 120s each time.
83- **Execution frequency**:The system will perform hierarchical control on the work scheduler according to the bundle activity of the application, and limit the execution frequency of work scheduler.
84
85Application group             |    Execution frequency constraint
86--------------------|-------------------------
87active group | minimum interval 2 hours
88daily used group | minimum interval 4 hours
89fixed group | minimum interval 24 hours
90rare used group | minimum interval 48 hours
91restricted group | forbidden
92unused group | forbidden
93
94- **Restrict to WorkInfo**:
95
96(1) WorkId, bundleName and abilityName are required.
97
98(2) At least one condition must be set.
99
100(3) Repeatcycletime should be at least 20 minutes. When setting repeatcycletime, you must select one of isrepeat and repeatcount.
101
102(4) Parameters support type include number, string and bool.
103
104## Repositories Involved<a name="section1371113476307"></a>
105
106Resource Schedule subsystem
107
108[**resourceschedule_work_scheduler**](https://gitee.com/openharmony/resourceschedule_work_scheduler)
109
110[notification_ces_standard](https://gitee.com/openharmony/notification_ces_standard)
111
112[appexecfwk_standard](https://gitee.com/openharmony/appexecfwk_standard)
113
114[powermgr_battery_manager](https://gitee.com/openharmony/powermgr_battery_manager)