async-cron 1.6.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- async_cron-1.6.3/LICENSE +21 -0
- async_cron-1.6.3/PKG-INFO +165 -0
- async_cron-1.6.3/README.md +129 -0
- async_cron-1.6.3/async_cron/__init__.py +1 -0
- async_cron-1.6.3/async_cron/_event_loop.py +15 -0
- async_cron-1.6.3/async_cron/job.py +274 -0
- async_cron-1.6.3/async_cron/job_loader.py +193 -0
- async_cron-1.6.3/async_cron/schedule.py +61 -0
- async_cron-1.6.3/async_cron/units.py +6 -0
- async_cron-1.6.3/async_cron.egg-info/PKG-INFO +165 -0
- async_cron-1.6.3/async_cron.egg-info/SOURCES.txt +15 -0
- async_cron-1.6.3/async_cron.egg-info/dependency_links.txt +1 -0
- async_cron-1.6.3/async_cron.egg-info/requires.txt +1 -0
- async_cron-1.6.3/async_cron.egg-info/top_level.txt +1 -0
- async_cron-1.6.3/setup.cfg +4 -0
- async_cron-1.6.3/setup.py +74 -0
- async_cron-1.6.3/tests/test_event_loop.py +221 -0
async_cron-1.6.3/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 aohan237
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: async_cron
|
|
3
|
+
Version: 1.6.3
|
|
4
|
+
Summary: asyncio crontab utils
|
|
5
|
+
Home-page: https://github.com/aohan237/async_cron
|
|
6
|
+
Author: aohan237
|
|
7
|
+
Author-email: aohan237@gmail.com
|
|
8
|
+
License: MIT
|
|
9
|
+
Platform: POSIX
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Programming Language :: Python
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.4
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.5
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.6
|
|
17
|
+
Classifier: Operating System :: POSIX
|
|
18
|
+
Classifier: Environment :: Web Environment
|
|
19
|
+
Classifier: Intended Audience :: Developers
|
|
20
|
+
Classifier: Topic :: Software Development
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: arrow
|
|
25
|
+
Dynamic: author
|
|
26
|
+
Dynamic: author-email
|
|
27
|
+
Dynamic: classifier
|
|
28
|
+
Dynamic: description
|
|
29
|
+
Dynamic: description-content-type
|
|
30
|
+
Dynamic: home-page
|
|
31
|
+
Dynamic: license
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
Dynamic: platform
|
|
34
|
+
Dynamic: requires-dist
|
|
35
|
+
Dynamic: summary
|
|
36
|
+
|
|
37
|
+
# async_cron
|
|
38
|
+
[](https://pepy.tech/project/async-cron)
|
|
39
|
+
[](https://badge.fury.io/py/async-cron)
|
|
40
|
+
|
|
41
|
+
this repo is influenced by schedule.
|
|
42
|
+
|
|
43
|
+
we supply a async scheduler and async function support
|
|
44
|
+
|
|
45
|
+
you can easily integrate this lib to you async program,with no blocking
|
|
46
|
+
|
|
47
|
+
## Install
|
|
48
|
+
|
|
49
|
+
--------------
|
|
50
|
+
|
|
51
|
+
pip install async-cron
|
|
52
|
+
|
|
53
|
+
## Usage examples
|
|
54
|
+
|
|
55
|
+
--------------
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
import asyncio
|
|
60
|
+
|
|
61
|
+
from async_cron.job import CronJob
|
|
62
|
+
from async_cron.schedule import Scheduler
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
async def test(*args, **kwargs):
|
|
66
|
+
print(args, kwargs)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def tt(*args, **kwargs):
|
|
70
|
+
print(args, kwargs)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
msh = Scheduler(locale="zh_CN")
|
|
74
|
+
myjob = CronJob(name='test', run_total=3).every(
|
|
75
|
+
5).second.go(test, (1, 2, 3), name=123)
|
|
76
|
+
job2 = CronJob(name='exact', tolerance=100).at(
|
|
77
|
+
"2019-01-15 16:12").go(tt, (5), age=99)
|
|
78
|
+
job3 = CronJob(name='very_hour').every().hour.at(
|
|
79
|
+
":44").go(tt, (5), age=99)
|
|
80
|
+
|
|
81
|
+
job3 = CronJob(name='hour').every().hour.at(
|
|
82
|
+
":00").go(tt, (5), age=99)
|
|
83
|
+
job4 = CronJob(name='minute').every(1).minute.go(tt, (5), age=99)
|
|
84
|
+
job5 = CronJob(name='weekday').weekday(2).at("11:18").go(tt, (5), age=99)
|
|
85
|
+
job6 = CronJob(name='monthday').monthday(16).at("11:22").go(tt, (5), age=99)
|
|
86
|
+
job7 = CronJob(name='monthday').every(5).monthday(
|
|
87
|
+
16).at("11:22").go(tt, (5), age=99)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
msh.add_job(myjob)
|
|
91
|
+
msh.add_job(job2)
|
|
92
|
+
msh.add_job(job3)
|
|
93
|
+
msh.add_job(job4)
|
|
94
|
+
msh.add_job(job5)
|
|
95
|
+
msh.add_job(job6)
|
|
96
|
+
msh.add_job(job7)
|
|
97
|
+
|
|
98
|
+
# jobload is only a special job,who gen jobs from config
|
|
99
|
+
# below means, this job load will check every 1 second for cron updates
|
|
100
|
+
# if any updates found,new job will be add to scheduler
|
|
101
|
+
# you dont have the direct way to delete jobs
|
|
102
|
+
# but you can modify the crons total_times to 0 or 1 to delete it
|
|
103
|
+
# by default,FileJobLoader use MultiThread,you can use MultiProcess by add
|
|
104
|
+
# thread=False
|
|
105
|
+
|
|
106
|
+
f_cron = FileJobLoader(name='f_cron', file_path='t_cron', log_path='.',thread=False)
|
|
107
|
+
|
|
108
|
+
fjob = CronJob(name='fjob', run_total=1).every(
|
|
109
|
+
1).second.go(f_cron.run, msh)
|
|
110
|
+
|
|
111
|
+
msh.add_job(fjob)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
loop = asyncio.get_event_loop()
|
|
115
|
+
|
|
116
|
+
try:
|
|
117
|
+
loop.run_until_complete(msh.start())
|
|
118
|
+
except KeyboardInterrupt:
|
|
119
|
+
print('exit')
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## cron file useage:
|
|
123
|
+
|
|
124
|
+
parameter separate by blank.in item separate by comma
|
|
125
|
+
|
|
126
|
+
cron|name|job|env|run_times
|
|
127
|
+
-|-|-|-|-
|
|
128
|
+
`*/1,*,*,*,*`|test|/bin/python,tt.py|aa=123,bb=345|10
|
|
129
|
+
|
|
130
|
+
example as follow:
|
|
131
|
+
|
|
132
|
+
common cron
|
|
133
|
+
|
|
134
|
+
`*/1,*,*,*,*` test /bin/python,tt.py aa=123,bb=345 1
|
|
135
|
+
|
|
136
|
+
delete cron
|
|
137
|
+
`*/1,*,*,*,*` test /bin/python,tt.py aa=123,bb=345 0
|
|
138
|
+
|
|
139
|
+
cron only support:
|
|
140
|
+
`*`,`10`,`*/10` format. which fulfills mostly screen
|
|
141
|
+
|
|
142
|
+
## Event loops
|
|
143
|
+
|
|
144
|
+
`CronJob`, `Scheduler`, `JobLoader`, and `FileJobLoader` reuse an explicitly
|
|
145
|
+
supplied loop or the current event loop. When no loop exists, they create and
|
|
146
|
+
install one, including on Python 3.14 and in worker threads. If you construct
|
|
147
|
+
these objects synchronously, run them on that same loop and close the loop on
|
|
148
|
+
shutdown. When using `asyncio.run(main())`, construct them inside `main()` so
|
|
149
|
+
their asynchronous work uses the running loop.
|
|
150
|
+
|
|
151
|
+
## Tests
|
|
152
|
+
|
|
153
|
+
Install the package and run either test entry point:
|
|
154
|
+
|
|
155
|
+
```sh
|
|
156
|
+
python -m pip install -e .
|
|
157
|
+
python runtests.py -v
|
|
158
|
+
python -m unittest discover -s tests -p 'test_*.py' -v
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
License
|
|
163
|
+
-------
|
|
164
|
+
|
|
165
|
+
The async_cron is offered under MIT license.
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# async_cron
|
|
2
|
+
[](https://pepy.tech/project/async-cron)
|
|
3
|
+
[](https://badge.fury.io/py/async-cron)
|
|
4
|
+
|
|
5
|
+
this repo is influenced by schedule.
|
|
6
|
+
|
|
7
|
+
we supply a async scheduler and async function support
|
|
8
|
+
|
|
9
|
+
you can easily integrate this lib to you async program,with no blocking
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
--------------
|
|
14
|
+
|
|
15
|
+
pip install async-cron
|
|
16
|
+
|
|
17
|
+
## Usage examples
|
|
18
|
+
|
|
19
|
+
--------------
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
import asyncio
|
|
24
|
+
|
|
25
|
+
from async_cron.job import CronJob
|
|
26
|
+
from async_cron.schedule import Scheduler
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
async def test(*args, **kwargs):
|
|
30
|
+
print(args, kwargs)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def tt(*args, **kwargs):
|
|
34
|
+
print(args, kwargs)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
msh = Scheduler(locale="zh_CN")
|
|
38
|
+
myjob = CronJob(name='test', run_total=3).every(
|
|
39
|
+
5).second.go(test, (1, 2, 3), name=123)
|
|
40
|
+
job2 = CronJob(name='exact', tolerance=100).at(
|
|
41
|
+
"2019-01-15 16:12").go(tt, (5), age=99)
|
|
42
|
+
job3 = CronJob(name='very_hour').every().hour.at(
|
|
43
|
+
":44").go(tt, (5), age=99)
|
|
44
|
+
|
|
45
|
+
job3 = CronJob(name='hour').every().hour.at(
|
|
46
|
+
":00").go(tt, (5), age=99)
|
|
47
|
+
job4 = CronJob(name='minute').every(1).minute.go(tt, (5), age=99)
|
|
48
|
+
job5 = CronJob(name='weekday').weekday(2).at("11:18").go(tt, (5), age=99)
|
|
49
|
+
job6 = CronJob(name='monthday').monthday(16).at("11:22").go(tt, (5), age=99)
|
|
50
|
+
job7 = CronJob(name='monthday').every(5).monthday(
|
|
51
|
+
16).at("11:22").go(tt, (5), age=99)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
msh.add_job(myjob)
|
|
55
|
+
msh.add_job(job2)
|
|
56
|
+
msh.add_job(job3)
|
|
57
|
+
msh.add_job(job4)
|
|
58
|
+
msh.add_job(job5)
|
|
59
|
+
msh.add_job(job6)
|
|
60
|
+
msh.add_job(job7)
|
|
61
|
+
|
|
62
|
+
# jobload is only a special job,who gen jobs from config
|
|
63
|
+
# below means, this job load will check every 1 second for cron updates
|
|
64
|
+
# if any updates found,new job will be add to scheduler
|
|
65
|
+
# you dont have the direct way to delete jobs
|
|
66
|
+
# but you can modify the crons total_times to 0 or 1 to delete it
|
|
67
|
+
# by default,FileJobLoader use MultiThread,you can use MultiProcess by add
|
|
68
|
+
# thread=False
|
|
69
|
+
|
|
70
|
+
f_cron = FileJobLoader(name='f_cron', file_path='t_cron', log_path='.',thread=False)
|
|
71
|
+
|
|
72
|
+
fjob = CronJob(name='fjob', run_total=1).every(
|
|
73
|
+
1).second.go(f_cron.run, msh)
|
|
74
|
+
|
|
75
|
+
msh.add_job(fjob)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
loop = asyncio.get_event_loop()
|
|
79
|
+
|
|
80
|
+
try:
|
|
81
|
+
loop.run_until_complete(msh.start())
|
|
82
|
+
except KeyboardInterrupt:
|
|
83
|
+
print('exit')
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## cron file useage:
|
|
87
|
+
|
|
88
|
+
parameter separate by blank.in item separate by comma
|
|
89
|
+
|
|
90
|
+
cron|name|job|env|run_times
|
|
91
|
+
-|-|-|-|-
|
|
92
|
+
`*/1,*,*,*,*`|test|/bin/python,tt.py|aa=123,bb=345|10
|
|
93
|
+
|
|
94
|
+
example as follow:
|
|
95
|
+
|
|
96
|
+
common cron
|
|
97
|
+
|
|
98
|
+
`*/1,*,*,*,*` test /bin/python,tt.py aa=123,bb=345 1
|
|
99
|
+
|
|
100
|
+
delete cron
|
|
101
|
+
`*/1,*,*,*,*` test /bin/python,tt.py aa=123,bb=345 0
|
|
102
|
+
|
|
103
|
+
cron only support:
|
|
104
|
+
`*`,`10`,`*/10` format. which fulfills mostly screen
|
|
105
|
+
|
|
106
|
+
## Event loops
|
|
107
|
+
|
|
108
|
+
`CronJob`, `Scheduler`, `JobLoader`, and `FileJobLoader` reuse an explicitly
|
|
109
|
+
supplied loop or the current event loop. When no loop exists, they create and
|
|
110
|
+
install one, including on Python 3.14 and in worker threads. If you construct
|
|
111
|
+
these objects synchronously, run them on that same loop and close the loop on
|
|
112
|
+
shutdown. When using `asyncio.run(main())`, construct them inside `main()` so
|
|
113
|
+
their asynchronous work uses the running loop.
|
|
114
|
+
|
|
115
|
+
## Tests
|
|
116
|
+
|
|
117
|
+
Install the package and run either test entry point:
|
|
118
|
+
|
|
119
|
+
```sh
|
|
120
|
+
python -m pip install -e .
|
|
121
|
+
python runtests.py -v
|
|
122
|
+
python -m unittest discover -s tests -p 'test_*.py' -v
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
License
|
|
127
|
+
-------
|
|
128
|
+
|
|
129
|
+
The async_cron is offered under MIT license.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = '1.6.3'
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def get_event_loop(loop=None):
|
|
5
|
+
"""Reuse the supplied/current loop, or install a new thread-local loop."""
|
|
6
|
+
if loop is not None:
|
|
7
|
+
return loop
|
|
8
|
+
try:
|
|
9
|
+
return asyncio.get_event_loop()
|
|
10
|
+
except RuntimeError:
|
|
11
|
+
# Python 3.14 no longer creates a loop implicitly. This also handles
|
|
12
|
+
# worker threads and construction after asyncio.run() clears the loop.
|
|
13
|
+
loop = asyncio.new_event_loop()
|
|
14
|
+
asyncio.set_event_loop(loop)
|
|
15
|
+
return loop
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import arrow
|
|
2
|
+
import inspect
|
|
3
|
+
import functools
|
|
4
|
+
import uuid
|
|
5
|
+
from dateutil import tz
|
|
6
|
+
import datetime
|
|
7
|
+
from .units import SECOND, MINUTE, HOUR, DAY, MONTH, WEEK
|
|
8
|
+
import logging
|
|
9
|
+
from ._event_loop import get_event_loop
|
|
10
|
+
|
|
11
|
+
logger = logging.getLogger(__package__)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class CronJob:
|
|
15
|
+
"""
|
|
16
|
+
periodic job
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
def __init__(self,
|
|
20
|
+
name: str = None,
|
|
21
|
+
interval: int = 1,
|
|
22
|
+
scheduler=None,
|
|
23
|
+
loop=None,
|
|
24
|
+
tz: str = None,
|
|
25
|
+
run_total: int = None,
|
|
26
|
+
tolerance: int = 10):
|
|
27
|
+
"""
|
|
28
|
+
:param name: crontab name
|
|
29
|
+
:param interval: crontab apply interval
|
|
30
|
+
:param scheduler: crontab scheduler instance for now ,it is useless
|
|
31
|
+
:param loop: asyncio running loop
|
|
32
|
+
:param tz: timezone info. support string tz format
|
|
33
|
+
:param run_total: crontab task total running times,
|
|
34
|
+
: with this parameter,you can limit its cron task count
|
|
35
|
+
:param tolerance: crontab tolerance. time tolerance, within is range,
|
|
36
|
+
: task will still be applied
|
|
37
|
+
"""
|
|
38
|
+
self.name = name or str(uuid.uuid1())
|
|
39
|
+
self.interval = interval
|
|
40
|
+
self.job_func = None
|
|
41
|
+
self.unit = None
|
|
42
|
+
self.at_time = (None, None)
|
|
43
|
+
self.at_exact_time = None
|
|
44
|
+
self.last_run = None
|
|
45
|
+
self.next_run = None
|
|
46
|
+
self.run_count = 0
|
|
47
|
+
self.run_total = run_total
|
|
48
|
+
self.start_hour = None
|
|
49
|
+
self.end_hour = None
|
|
50
|
+
self.period = None
|
|
51
|
+
self.week_day = None
|
|
52
|
+
self.month_day = None
|
|
53
|
+
self.scheduler = scheduler
|
|
54
|
+
self.loop = get_event_loop(loop)
|
|
55
|
+
self.gte_day = False
|
|
56
|
+
self.tz = tz
|
|
57
|
+
# tolerance means if now - at_exact_time <= tolerance,
|
|
58
|
+
# this job will still be applied
|
|
59
|
+
self.tolerance = datetime.timedelta(seconds=tolerance)
|
|
60
|
+
|
|
61
|
+
def __repr__(self):
|
|
62
|
+
return f"{self.name}-{self.interval}:{self.unit}-{self.at_time}\
|
|
63
|
+
-{self.at_exact_time}-{self.run_total}"
|
|
64
|
+
|
|
65
|
+
def __eq__(self, job=None):
|
|
66
|
+
if (self.interval == job.interval and self.unit == job.unit
|
|
67
|
+
and self.at_time == job.at_time
|
|
68
|
+
and self.run_total == job.run_total
|
|
69
|
+
and self.at_exact_time == job.at_exact_time):
|
|
70
|
+
return True
|
|
71
|
+
else:
|
|
72
|
+
return False
|
|
73
|
+
|
|
74
|
+
def check_gte_day(self):
|
|
75
|
+
if self.unit in (SECOND, MINUTE, HOUR):
|
|
76
|
+
self.gte_day = False
|
|
77
|
+
else:
|
|
78
|
+
self.gte_day = True
|
|
79
|
+
|
|
80
|
+
def get_now(self):
|
|
81
|
+
now = arrow.utcnow()
|
|
82
|
+
if self.tz:
|
|
83
|
+
now = now.to(self.tz)
|
|
84
|
+
else:
|
|
85
|
+
now = now.to(tz.tzlocal())
|
|
86
|
+
return now
|
|
87
|
+
|
|
88
|
+
def get_tz_time(self, arw=None):
|
|
89
|
+
if self.tz:
|
|
90
|
+
arw = arw.to(self.tz)
|
|
91
|
+
else:
|
|
92
|
+
arw = arw.to(tz.tzlocal())
|
|
93
|
+
return arw
|
|
94
|
+
|
|
95
|
+
def decide_run(self):
|
|
96
|
+
now = self.get_now()
|
|
97
|
+
if self.at_exact_time:
|
|
98
|
+
# check if it is a exact run func
|
|
99
|
+
if (now >= self.at_exact_time
|
|
100
|
+
and now - self.at_exact_time <= self.tolerance):
|
|
101
|
+
return True
|
|
102
|
+
else:
|
|
103
|
+
return False
|
|
104
|
+
|
|
105
|
+
if self.next_run:
|
|
106
|
+
if now >= self.next_run:
|
|
107
|
+
# IMPORTANT BUGFIX: Always check weekday conditions for weekly jobs,
|
|
108
|
+
# even after next_run is set. The original code only checked weekdays
|
|
109
|
+
# on the first run, then ignored them completely for subsequent runs.
|
|
110
|
+
if self.week_day is not None and self.week_day != now.weekday():
|
|
111
|
+
return False
|
|
112
|
+
if self.month_day and self.month_day != now.day:
|
|
113
|
+
return False
|
|
114
|
+
return True
|
|
115
|
+
else:
|
|
116
|
+
if self.month_day and self.month_day != now.day:
|
|
117
|
+
return False
|
|
118
|
+
if self.week_day is not None and self.week_day != now.weekday():
|
|
119
|
+
return False
|
|
120
|
+
now_datetime = now.datetime
|
|
121
|
+
hour, minute = self.at_time
|
|
122
|
+
if hour is not None and minute is not None:
|
|
123
|
+
if (now_datetime.hour == hour
|
|
124
|
+
and now_datetime.minute == minute):
|
|
125
|
+
return True
|
|
126
|
+
elif hour is not None:
|
|
127
|
+
if now_datetime.hour == hour:
|
|
128
|
+
if self.start_hour is not None and self.end_hour is not None:
|
|
129
|
+
if int(self.start_hour) <= hour <= int(self.end_hour):
|
|
130
|
+
return True
|
|
131
|
+
elif self.start_hour is not None:
|
|
132
|
+
if int(self.start_hour) <= hour:
|
|
133
|
+
return True
|
|
134
|
+
elif self.end_hour is not None:
|
|
135
|
+
if hour <= int(self.end_hour):
|
|
136
|
+
return True
|
|
137
|
+
else:
|
|
138
|
+
return True
|
|
139
|
+
elif minute is not None:
|
|
140
|
+
if now_datetime.minute == minute:
|
|
141
|
+
hour = now_datetime.hour
|
|
142
|
+
if self.start_hour is not None and self.end_hour is not None:
|
|
143
|
+
if int(self.start_hour) <= hour <= int(self.end_hour):
|
|
144
|
+
return True
|
|
145
|
+
elif self.start_hour is not None:
|
|
146
|
+
if int(self.start_hour) <= hour:
|
|
147
|
+
return True
|
|
148
|
+
elif self.end_hour is not None:
|
|
149
|
+
if hour <= int(self.end_hour):
|
|
150
|
+
return True
|
|
151
|
+
else:
|
|
152
|
+
return True
|
|
153
|
+
else:
|
|
154
|
+
return True
|
|
155
|
+
return False
|
|
156
|
+
|
|
157
|
+
def remove(self):
|
|
158
|
+
if self.run_total:
|
|
159
|
+
if self.run_count >= self.run_total:
|
|
160
|
+
return True
|
|
161
|
+
return False
|
|
162
|
+
|
|
163
|
+
def go(self, job_func, *args, **kwargs):
|
|
164
|
+
self.job_func = functools.partial(job_func, *args, **kwargs)
|
|
165
|
+
return self
|
|
166
|
+
|
|
167
|
+
def run(self):
|
|
168
|
+
if self.job_func is None:
|
|
169
|
+
return
|
|
170
|
+
now = self.get_now()
|
|
171
|
+
# recodrd run args
|
|
172
|
+
self.last_run = now
|
|
173
|
+
self.run_count += 1
|
|
174
|
+
# run job_func
|
|
175
|
+
tmp_result = self.job_func()
|
|
176
|
+
if inspect.iscoroutine(tmp_result):
|
|
177
|
+
self.loop.create_task(tmp_result)
|
|
178
|
+
self.gen_next_run()
|
|
179
|
+
return tmp_result
|
|
180
|
+
|
|
181
|
+
def gen_next_run(self):
|
|
182
|
+
if not self.at_exact_time:
|
|
183
|
+
if not isinstance(self.unit, str):
|
|
184
|
+
raise Exception(
|
|
185
|
+
f"self.unit must be specified: self.unit->{self.unit}")
|
|
186
|
+
self.next_run = self.last_run.shift(**{self.unit: self.interval})
|
|
187
|
+
|
|
188
|
+
def split_time(self, time_string: str = None):
|
|
189
|
+
hour, minute = time_string.split(':')
|
|
190
|
+
hour = int(hour) if hour else None
|
|
191
|
+
minute = int(minute) if minute else None
|
|
192
|
+
return (hour, minute)
|
|
193
|
+
|
|
194
|
+
def at(self, time_string: str = None, time_shift=8):
|
|
195
|
+
if time_string is None:
|
|
196
|
+
pass
|
|
197
|
+
else:
|
|
198
|
+
time_string = time_string.replace(':', ':')
|
|
199
|
+
first, *_ = time_string.split(':')
|
|
200
|
+
if len(first) > 2:
|
|
201
|
+
try:
|
|
202
|
+
arrow_time = arrow.get(time_string)
|
|
203
|
+
arrow_time = self.get_tz_time(arrow_time).shift(
|
|
204
|
+
hours=-time_shift)
|
|
205
|
+
self.at_exact_time = arrow_time
|
|
206
|
+
self.run_total = 1
|
|
207
|
+
except Exception as tmp:
|
|
208
|
+
logger.exception(tmp)
|
|
209
|
+
logger.info(f'{self.name} parse datetime error')
|
|
210
|
+
else:
|
|
211
|
+
try:
|
|
212
|
+
self.at_time = self.split_time(time_string)
|
|
213
|
+
except Exception as tmp:
|
|
214
|
+
logger.exception(tmp)
|
|
215
|
+
logger.info(f'{self.name} parse hour and minute error')
|
|
216
|
+
return self
|
|
217
|
+
|
|
218
|
+
def every(self, interval: int = None):
|
|
219
|
+
if interval is None:
|
|
220
|
+
pass
|
|
221
|
+
else:
|
|
222
|
+
self.interval = interval
|
|
223
|
+
return self
|
|
224
|
+
|
|
225
|
+
@property
|
|
226
|
+
def second(self):
|
|
227
|
+
self.unit = SECOND
|
|
228
|
+
self.check_gte_day()
|
|
229
|
+
return self
|
|
230
|
+
|
|
231
|
+
@property
|
|
232
|
+
def minute(self):
|
|
233
|
+
self.unit = MINUTE
|
|
234
|
+
self.check_gte_day()
|
|
235
|
+
return self
|
|
236
|
+
|
|
237
|
+
@property
|
|
238
|
+
def hour(self):
|
|
239
|
+
self.unit = HOUR
|
|
240
|
+
self.check_gte_day()
|
|
241
|
+
return self
|
|
242
|
+
|
|
243
|
+
def from_hour(self, hour: int = None):
|
|
244
|
+
self.start_hour = hour
|
|
245
|
+
return self
|
|
246
|
+
|
|
247
|
+
def to_hour(self, hour: int = None):
|
|
248
|
+
self.end_hour = hour
|
|
249
|
+
return self
|
|
250
|
+
|
|
251
|
+
@property
|
|
252
|
+
def day(self):
|
|
253
|
+
self.unit = DAY
|
|
254
|
+
return self
|
|
255
|
+
|
|
256
|
+
@property
|
|
257
|
+
def week(self):
|
|
258
|
+
self.unit = WEEK
|
|
259
|
+
return self
|
|
260
|
+
|
|
261
|
+
def weekday(self, week_day: int = None):
|
|
262
|
+
self.unit = WEEK
|
|
263
|
+
self.week_day = week_day
|
|
264
|
+
return self
|
|
265
|
+
|
|
266
|
+
def monthday(self, month_day: int = None):
|
|
267
|
+
self.unit = MONTH
|
|
268
|
+
self.month_day = month_day
|
|
269
|
+
return self
|
|
270
|
+
|
|
271
|
+
@property
|
|
272
|
+
def month(self):
|
|
273
|
+
self.unit = MONTH
|
|
274
|
+
return self
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
from .job import CronJob
|
|
2
|
+
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
|
|
3
|
+
import subprocess
|
|
4
|
+
import logging
|
|
5
|
+
from ._event_loop import get_event_loop
|
|
6
|
+
|
|
7
|
+
logger = logging.getLogger(__package__)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class JobLoader:
|
|
11
|
+
def __init__(self,
|
|
12
|
+
name=None,
|
|
13
|
+
loop=None,
|
|
14
|
+
log_path=None,
|
|
15
|
+
log_level=None,
|
|
16
|
+
thread=True,
|
|
17
|
+
**kwargs):
|
|
18
|
+
self.name = name
|
|
19
|
+
if thread:
|
|
20
|
+
self.executor = ThreadPoolExecutor()
|
|
21
|
+
else:
|
|
22
|
+
self.executor = ProcessPoolExecutor()
|
|
23
|
+
self.loop = get_event_loop(loop)
|
|
24
|
+
self.log_path = log_path.strip('/')
|
|
25
|
+
|
|
26
|
+
async def load(self):
|
|
27
|
+
raise NotImplementedError
|
|
28
|
+
|
|
29
|
+
@staticmethod
|
|
30
|
+
def sub_process_command(command, env, name, log_path):
|
|
31
|
+
out_path_name = log_path + '/' + f'{name}_out.txt'
|
|
32
|
+
err_path_name = log_path + '/' + f'{name}_err.txt'
|
|
33
|
+
out_log_file = open(out_path_name, 'a')
|
|
34
|
+
err_log_file = open(err_path_name, 'a')
|
|
35
|
+
subprocess.run(command.split(','),
|
|
36
|
+
env=env,
|
|
37
|
+
stdout=out_log_file,
|
|
38
|
+
stderr=err_log_file)
|
|
39
|
+
out_log_file.close()
|
|
40
|
+
err_log_file.close()
|
|
41
|
+
|
|
42
|
+
def create_executor_task(self,
|
|
43
|
+
command=None,
|
|
44
|
+
env=None,
|
|
45
|
+
name=None,
|
|
46
|
+
log_path=None):
|
|
47
|
+
logger.info('create_executor_task---start\n\n')
|
|
48
|
+
try:
|
|
49
|
+
result = self.loop.run_in_executor(self.executor,
|
|
50
|
+
self.sub_process_command,
|
|
51
|
+
*(command, env, name, log_path))
|
|
52
|
+
except Exception as tmp:
|
|
53
|
+
logger.info('create_executor_task_exception')
|
|
54
|
+
logger.exception(tmp)
|
|
55
|
+
result = None
|
|
56
|
+
return result
|
|
57
|
+
|
|
58
|
+
def parse_env(self, env_string=None):
|
|
59
|
+
result = {}
|
|
60
|
+
for i in env_string.split(','):
|
|
61
|
+
k, v = i.split('=')
|
|
62
|
+
result[k] = v
|
|
63
|
+
return result
|
|
64
|
+
|
|
65
|
+
def parse(self, line_data):
|
|
66
|
+
"""
|
|
67
|
+
crontab name job env total_times
|
|
68
|
+
example='*/2,*,*,*,* ceshi python,--name=12 aa=123,bb=345 10'
|
|
69
|
+
"""
|
|
70
|
+
try:
|
|
71
|
+
cron, name, command, *env_total = line_data.split(' ')
|
|
72
|
+
if len(env_total) == 2:
|
|
73
|
+
env, total_times = env_total
|
|
74
|
+
total_times = int(total_times)
|
|
75
|
+
else:
|
|
76
|
+
env = env_total[0]
|
|
77
|
+
total_times = None
|
|
78
|
+
job = self.parse_cron(cron=cron,
|
|
79
|
+
name=name,
|
|
80
|
+
total_times=total_times)
|
|
81
|
+
if job is not None:
|
|
82
|
+
env = self.parse_env(env_string=env)
|
|
83
|
+
job.go(self.create_executor_task,
|
|
84
|
+
command=command,
|
|
85
|
+
env=env,
|
|
86
|
+
name=name,
|
|
87
|
+
log_path=self.log_path)
|
|
88
|
+
return job
|
|
89
|
+
except Exception as tmp:
|
|
90
|
+
logger.info('cron file format error')
|
|
91
|
+
logger.exception(tmp)
|
|
92
|
+
return None
|
|
93
|
+
|
|
94
|
+
def parse_cron(self, cron=None, name=None, total_times=None):
|
|
95
|
+
minute, hour, day, month, week = [i.strip() for i in cron.split(',')]
|
|
96
|
+
month_every, month_at_time = self.parse_detail(month)
|
|
97
|
+
week_every, week_at_time = self.parse_detail(week)
|
|
98
|
+
day_every, day_at_time = self.parse_detail(day)
|
|
99
|
+
hour_every, hour_at_time = self.parse_detail(hour)
|
|
100
|
+
minute_every, minute_at_time = self.parse_detail(minute)
|
|
101
|
+
|
|
102
|
+
tmp_job = CronJob(name=name, run_total=total_times)
|
|
103
|
+
if (month_at_time and day_at_time and hour_at_time and minute_at_time):
|
|
104
|
+
tmp_job = tmp_job.at(f"{month_at_time}-{day_at_time}\
|
|
105
|
+
{hour_at_time}:{minute_at_time}")
|
|
106
|
+
return tmp_job
|
|
107
|
+
|
|
108
|
+
if month != '*':
|
|
109
|
+
if month_every is None:
|
|
110
|
+
month_every = 1
|
|
111
|
+
tmp_job = tmp_job.every(month_every)
|
|
112
|
+
if day_at_time is not None:
|
|
113
|
+
tmp_job = tmp_job.month_day(day_at_time)
|
|
114
|
+
if hour_at_time is not None and minute is not None:
|
|
115
|
+
tmp_job = tmp_job.at_time(f'{hour_at_time}:{minute_at_time}')
|
|
116
|
+
elif week != '*':
|
|
117
|
+
if week_every is None:
|
|
118
|
+
month_every = 1
|
|
119
|
+
if week_at_time is not None:
|
|
120
|
+
tmp_job = tmp_job.week_day(week_at_time)
|
|
121
|
+
if hour_at_time is not None and minute is not None:
|
|
122
|
+
tmp_job = tmp_job.at_time(f'{hour_at_time}:{minute_at_time}')
|
|
123
|
+
elif day != '*':
|
|
124
|
+
if day_every is None:
|
|
125
|
+
day_every = 1
|
|
126
|
+
tmp_job = tmp_job.every(day_every).day
|
|
127
|
+
if hour_at_time is not None and minute is not None:
|
|
128
|
+
tmp_job = tmp_job.at_time(f'{hour_at_time}:{minute_at_time}')
|
|
129
|
+
elif hour != '*':
|
|
130
|
+
if hour_every is None:
|
|
131
|
+
hour_every = 1
|
|
132
|
+
tmp_job = tmp_job.every(hour_every).hour
|
|
133
|
+
if minute is not None:
|
|
134
|
+
tmp_job = tmp_job.at_time(f':{minute_at_time}')
|
|
135
|
+
elif minute != '*':
|
|
136
|
+
if minute_every is None:
|
|
137
|
+
minute_every = 1
|
|
138
|
+
tmp_job = tmp_job.every(minute_every).minute
|
|
139
|
+
if tmp_job.unit:
|
|
140
|
+
return tmp_job
|
|
141
|
+
else:
|
|
142
|
+
return None
|
|
143
|
+
|
|
144
|
+
def parse_detail(self, data=None):
|
|
145
|
+
data = data.split('/')
|
|
146
|
+
every = None
|
|
147
|
+
at_time = None
|
|
148
|
+
if len(data) > 1:
|
|
149
|
+
f_data, every = data
|
|
150
|
+
else:
|
|
151
|
+
at_time = data[0]
|
|
152
|
+
if every is not None:
|
|
153
|
+
if '*' not in every:
|
|
154
|
+
every = int(every)
|
|
155
|
+
if at_time is not None:
|
|
156
|
+
if '*' not in at_time:
|
|
157
|
+
at_time = int(at_time)
|
|
158
|
+
return every, at_time
|
|
159
|
+
|
|
160
|
+
def gen_job(self, data=None):
|
|
161
|
+
pass
|
|
162
|
+
|
|
163
|
+
async def run(self, schedule=None):
|
|
164
|
+
try:
|
|
165
|
+
jobs = await self.load()
|
|
166
|
+
for job in jobs:
|
|
167
|
+
if isinstance(job, CronJob):
|
|
168
|
+
self.add_job_schedule(job, schedule)
|
|
169
|
+
print(schedule.jobs)
|
|
170
|
+
except KeyboardInterrupt:
|
|
171
|
+
logger.info('keyboard exit')
|
|
172
|
+
self.executor.shutdown()
|
|
173
|
+
|
|
174
|
+
@staticmethod
|
|
175
|
+
def add_job_schedule(job, schedule):
|
|
176
|
+
schedule.add_job(job)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
class FileJobLoader(JobLoader):
|
|
180
|
+
def __init__(self, file_path=None, **kwargs):
|
|
181
|
+
super(FileJobLoader, self).__init__(**kwargs)
|
|
182
|
+
self.file_path = file_path
|
|
183
|
+
|
|
184
|
+
async def load(self):
|
|
185
|
+
result = []
|
|
186
|
+
if self.file_path:
|
|
187
|
+
with open(self.file_path, 'r') as file:
|
|
188
|
+
crons = [i.strip('\n').strip() for i in file.readlines()]
|
|
189
|
+
for i in crons:
|
|
190
|
+
tmp_cron = self.parse(i)
|
|
191
|
+
if tmp_cron is not None:
|
|
192
|
+
result.append(tmp_cron)
|
|
193
|
+
return result
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
from .job import CronJob
|
|
3
|
+
import logging
|
|
4
|
+
from ._event_loop import get_event_loop
|
|
5
|
+
|
|
6
|
+
logger = logging.getLogger(__package__)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Scheduler:
|
|
10
|
+
def __init__(self, name=None, check_interval: int = 5,
|
|
11
|
+
loop=None, log_level=None, locale=None):
|
|
12
|
+
"""
|
|
13
|
+
:param name:name of the scheduler
|
|
14
|
+
:param check_interval: check interval of the scheduler, unit is second
|
|
15
|
+
:param locale: locale for humanized time,follows the general rule of locale
|
|
16
|
+
"""
|
|
17
|
+
self.name = name
|
|
18
|
+
self.check_interval = check_interval
|
|
19
|
+
self.loop = get_event_loop(loop)
|
|
20
|
+
self.jobs = {}
|
|
21
|
+
self.locale = locale or "zh_CN"
|
|
22
|
+
|
|
23
|
+
async def start(self):
|
|
24
|
+
while True:
|
|
25
|
+
try:
|
|
26
|
+
await self.check_jobs()
|
|
27
|
+
await asyncio.sleep(self.check_interval)
|
|
28
|
+
except KeyboardInterrupt:
|
|
29
|
+
logger.info('keyboard interrupt,exit')
|
|
30
|
+
break
|
|
31
|
+
except Exception as tmp:
|
|
32
|
+
logger.exception(tmp)
|
|
33
|
+
logger.info('error occurs,exit')
|
|
34
|
+
break
|
|
35
|
+
|
|
36
|
+
def add_job(self, job: CronJob = None):
|
|
37
|
+
if job.name in self.jobs:
|
|
38
|
+
if self.jobs[job.name] == job:
|
|
39
|
+
pass
|
|
40
|
+
else:
|
|
41
|
+
self.jobs[job.name] = job
|
|
42
|
+
|
|
43
|
+
def del_job(self, job_name: str = None):
|
|
44
|
+
if job_name in self.jobs:
|
|
45
|
+
del self.jobs[job_name]
|
|
46
|
+
else:
|
|
47
|
+
logger.info(f'{job_name} is in scheduler jobs list')
|
|
48
|
+
|
|
49
|
+
async def check_jobs(self):
|
|
50
|
+
remove_list = []
|
|
51
|
+
for name, job in self.jobs.items():
|
|
52
|
+
if job.decide_run():
|
|
53
|
+
now = job.get_now()
|
|
54
|
+
logger.info(
|
|
55
|
+
f'runing:{job.name}:{now.humanize(locale=self.locale)}')
|
|
56
|
+
job.run()
|
|
57
|
+
if job.remove():
|
|
58
|
+
remove_list.append(name)
|
|
59
|
+
for i in remove_list:
|
|
60
|
+
self.del_job(i)
|
|
61
|
+
logger.info(f'{i} is out of date ,delete')
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: async_cron
|
|
3
|
+
Version: 1.6.3
|
|
4
|
+
Summary: asyncio crontab utils
|
|
5
|
+
Home-page: https://github.com/aohan237/async_cron
|
|
6
|
+
Author: aohan237
|
|
7
|
+
Author-email: aohan237@gmail.com
|
|
8
|
+
License: MIT
|
|
9
|
+
Platform: POSIX
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Programming Language :: Python
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.4
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.5
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.6
|
|
17
|
+
Classifier: Operating System :: POSIX
|
|
18
|
+
Classifier: Environment :: Web Environment
|
|
19
|
+
Classifier: Intended Audience :: Developers
|
|
20
|
+
Classifier: Topic :: Software Development
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: arrow
|
|
25
|
+
Dynamic: author
|
|
26
|
+
Dynamic: author-email
|
|
27
|
+
Dynamic: classifier
|
|
28
|
+
Dynamic: description
|
|
29
|
+
Dynamic: description-content-type
|
|
30
|
+
Dynamic: home-page
|
|
31
|
+
Dynamic: license
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
Dynamic: platform
|
|
34
|
+
Dynamic: requires-dist
|
|
35
|
+
Dynamic: summary
|
|
36
|
+
|
|
37
|
+
# async_cron
|
|
38
|
+
[](https://pepy.tech/project/async-cron)
|
|
39
|
+
[](https://badge.fury.io/py/async-cron)
|
|
40
|
+
|
|
41
|
+
this repo is influenced by schedule.
|
|
42
|
+
|
|
43
|
+
we supply a async scheduler and async function support
|
|
44
|
+
|
|
45
|
+
you can easily integrate this lib to you async program,with no blocking
|
|
46
|
+
|
|
47
|
+
## Install
|
|
48
|
+
|
|
49
|
+
--------------
|
|
50
|
+
|
|
51
|
+
pip install async-cron
|
|
52
|
+
|
|
53
|
+
## Usage examples
|
|
54
|
+
|
|
55
|
+
--------------
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
import asyncio
|
|
60
|
+
|
|
61
|
+
from async_cron.job import CronJob
|
|
62
|
+
from async_cron.schedule import Scheduler
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
async def test(*args, **kwargs):
|
|
66
|
+
print(args, kwargs)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def tt(*args, **kwargs):
|
|
70
|
+
print(args, kwargs)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
msh = Scheduler(locale="zh_CN")
|
|
74
|
+
myjob = CronJob(name='test', run_total=3).every(
|
|
75
|
+
5).second.go(test, (1, 2, 3), name=123)
|
|
76
|
+
job2 = CronJob(name='exact', tolerance=100).at(
|
|
77
|
+
"2019-01-15 16:12").go(tt, (5), age=99)
|
|
78
|
+
job3 = CronJob(name='very_hour').every().hour.at(
|
|
79
|
+
":44").go(tt, (5), age=99)
|
|
80
|
+
|
|
81
|
+
job3 = CronJob(name='hour').every().hour.at(
|
|
82
|
+
":00").go(tt, (5), age=99)
|
|
83
|
+
job4 = CronJob(name='minute').every(1).minute.go(tt, (5), age=99)
|
|
84
|
+
job5 = CronJob(name='weekday').weekday(2).at("11:18").go(tt, (5), age=99)
|
|
85
|
+
job6 = CronJob(name='monthday').monthday(16).at("11:22").go(tt, (5), age=99)
|
|
86
|
+
job7 = CronJob(name='monthday').every(5).monthday(
|
|
87
|
+
16).at("11:22").go(tt, (5), age=99)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
msh.add_job(myjob)
|
|
91
|
+
msh.add_job(job2)
|
|
92
|
+
msh.add_job(job3)
|
|
93
|
+
msh.add_job(job4)
|
|
94
|
+
msh.add_job(job5)
|
|
95
|
+
msh.add_job(job6)
|
|
96
|
+
msh.add_job(job7)
|
|
97
|
+
|
|
98
|
+
# jobload is only a special job,who gen jobs from config
|
|
99
|
+
# below means, this job load will check every 1 second for cron updates
|
|
100
|
+
# if any updates found,new job will be add to scheduler
|
|
101
|
+
# you dont have the direct way to delete jobs
|
|
102
|
+
# but you can modify the crons total_times to 0 or 1 to delete it
|
|
103
|
+
# by default,FileJobLoader use MultiThread,you can use MultiProcess by add
|
|
104
|
+
# thread=False
|
|
105
|
+
|
|
106
|
+
f_cron = FileJobLoader(name='f_cron', file_path='t_cron', log_path='.',thread=False)
|
|
107
|
+
|
|
108
|
+
fjob = CronJob(name='fjob', run_total=1).every(
|
|
109
|
+
1).second.go(f_cron.run, msh)
|
|
110
|
+
|
|
111
|
+
msh.add_job(fjob)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
loop = asyncio.get_event_loop()
|
|
115
|
+
|
|
116
|
+
try:
|
|
117
|
+
loop.run_until_complete(msh.start())
|
|
118
|
+
except KeyboardInterrupt:
|
|
119
|
+
print('exit')
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## cron file useage:
|
|
123
|
+
|
|
124
|
+
parameter separate by blank.in item separate by comma
|
|
125
|
+
|
|
126
|
+
cron|name|job|env|run_times
|
|
127
|
+
-|-|-|-|-
|
|
128
|
+
`*/1,*,*,*,*`|test|/bin/python,tt.py|aa=123,bb=345|10
|
|
129
|
+
|
|
130
|
+
example as follow:
|
|
131
|
+
|
|
132
|
+
common cron
|
|
133
|
+
|
|
134
|
+
`*/1,*,*,*,*` test /bin/python,tt.py aa=123,bb=345 1
|
|
135
|
+
|
|
136
|
+
delete cron
|
|
137
|
+
`*/1,*,*,*,*` test /bin/python,tt.py aa=123,bb=345 0
|
|
138
|
+
|
|
139
|
+
cron only support:
|
|
140
|
+
`*`,`10`,`*/10` format. which fulfills mostly screen
|
|
141
|
+
|
|
142
|
+
## Event loops
|
|
143
|
+
|
|
144
|
+
`CronJob`, `Scheduler`, `JobLoader`, and `FileJobLoader` reuse an explicitly
|
|
145
|
+
supplied loop or the current event loop. When no loop exists, they create and
|
|
146
|
+
install one, including on Python 3.14 and in worker threads. If you construct
|
|
147
|
+
these objects synchronously, run them on that same loop and close the loop on
|
|
148
|
+
shutdown. When using `asyncio.run(main())`, construct them inside `main()` so
|
|
149
|
+
their asynchronous work uses the running loop.
|
|
150
|
+
|
|
151
|
+
## Tests
|
|
152
|
+
|
|
153
|
+
Install the package and run either test entry point:
|
|
154
|
+
|
|
155
|
+
```sh
|
|
156
|
+
python -m pip install -e .
|
|
157
|
+
python runtests.py -v
|
|
158
|
+
python -m unittest discover -s tests -p 'test_*.py' -v
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
License
|
|
163
|
+
-------
|
|
164
|
+
|
|
165
|
+
The async_cron is offered under MIT license.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
setup.py
|
|
4
|
+
async_cron/__init__.py
|
|
5
|
+
async_cron/_event_loop.py
|
|
6
|
+
async_cron/job.py
|
|
7
|
+
async_cron/job_loader.py
|
|
8
|
+
async_cron/schedule.py
|
|
9
|
+
async_cron/units.py
|
|
10
|
+
async_cron.egg-info/PKG-INFO
|
|
11
|
+
async_cron.egg-info/SOURCES.txt
|
|
12
|
+
async_cron.egg-info/dependency_links.txt
|
|
13
|
+
async_cron.egg-info/requires.txt
|
|
14
|
+
async_cron.egg-info/top_level.txt
|
|
15
|
+
tests/test_event_loop.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
arrow
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
async_cron
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import re
|
|
2
|
+
import os.path
|
|
3
|
+
import sys
|
|
4
|
+
from setuptools import setup, find_packages
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
install_requires = ['arrow']
|
|
8
|
+
NAME = 'async_cron'
|
|
9
|
+
PACKAGE = 'async_cron'
|
|
10
|
+
PY_VER = sys.version_info
|
|
11
|
+
|
|
12
|
+
if PY_VER >= (3, 4):
|
|
13
|
+
pass
|
|
14
|
+
elif PY_VER >= (3, 3):
|
|
15
|
+
pass
|
|
16
|
+
else:
|
|
17
|
+
raise RuntimeError("async_cron doesn't support Python version prior 3.3")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def read(*parts):
|
|
21
|
+
with open(os.path.join(*parts), 'rt') as f:
|
|
22
|
+
return f.read().strip()
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def read_version():
|
|
26
|
+
regexp = re.compile(r"^__version__\W*=\W*'([\d.abrc]+)'")
|
|
27
|
+
init_py = os.path.join(os.path.dirname(__file__),
|
|
28
|
+
'async_cron', '__init__.py')
|
|
29
|
+
with open(init_py) as f:
|
|
30
|
+
for line in f:
|
|
31
|
+
match = regexp.match(line)
|
|
32
|
+
if match is not None:
|
|
33
|
+
return match.group(1)
|
|
34
|
+
else:
|
|
35
|
+
raise RuntimeError('Cannot find version in async_cron/__init__.py')
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
classifiers = [
|
|
39
|
+
'License :: OSI Approved :: MIT License',
|
|
40
|
+
'Development Status :: 4 - Beta',
|
|
41
|
+
'Programming Language :: Python',
|
|
42
|
+
'Programming Language :: Python :: 3',
|
|
43
|
+
'Programming Language :: Python :: 3.4',
|
|
44
|
+
'Programming Language :: Python :: 3.5',
|
|
45
|
+
'Programming Language :: Python :: 3.6',
|
|
46
|
+
'Operating System :: POSIX',
|
|
47
|
+
'Environment :: Web Environment',
|
|
48
|
+
'Intended Audience :: Developers',
|
|
49
|
+
'Topic :: Software Development',
|
|
50
|
+
'Topic :: Software Development :: Libraries',
|
|
51
|
+
]
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
if os.path.exists('README.md'):
|
|
55
|
+
with open('README.md', 'r') as f:
|
|
56
|
+
long_description = f.read()
|
|
57
|
+
else:
|
|
58
|
+
long_description = 'See http://pypi.python.org/pypi/%s' % (NAME,)
|
|
59
|
+
|
|
60
|
+
setup(name='async_cron',
|
|
61
|
+
version=read_version(),
|
|
62
|
+
description=("asyncio crontab utils"),
|
|
63
|
+
long_description=long_description,
|
|
64
|
+
long_description_content_type="text/markdown",
|
|
65
|
+
classifiers=classifiers,
|
|
66
|
+
platforms=["POSIX"],
|
|
67
|
+
author="aohan237",
|
|
68
|
+
author_email="aohan237@gmail.com",
|
|
69
|
+
url="https://github.com/aohan237/async_cron",
|
|
70
|
+
license="MIT",
|
|
71
|
+
packages=find_packages(exclude=["tests"]),
|
|
72
|
+
install_requires=install_requires,
|
|
73
|
+
include_package_data=True,
|
|
74
|
+
)
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
from concurrent.futures import ProcessPoolExecutor
|
|
3
|
+
import contextlib
|
|
4
|
+
import os
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
import sys
|
|
7
|
+
import tempfile
|
|
8
|
+
import threading
|
|
9
|
+
import unittest
|
|
10
|
+
from unittest import mock
|
|
11
|
+
|
|
12
|
+
from async_cron.job import CronJob
|
|
13
|
+
from async_cron.job_loader import FileJobLoader, JobLoader
|
|
14
|
+
from async_cron.schedule import Scheduler
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class TestEventLoopHandling(unittest.TestCase):
|
|
18
|
+
"""All scheduler objects must support construction without a current loop."""
|
|
19
|
+
|
|
20
|
+
constructors = (CronJob, Scheduler, JobLoader, FileJobLoader)
|
|
21
|
+
|
|
22
|
+
def setUp(self):
|
|
23
|
+
asyncio.set_event_loop(None)
|
|
24
|
+
self.loops = []
|
|
25
|
+
self.loaders = []
|
|
26
|
+
self.original_new_event_loop = asyncio.new_event_loop
|
|
27
|
+
self.loop_patch = mock.patch('asyncio.new_event_loop',
|
|
28
|
+
side_effect=self.new_loop)
|
|
29
|
+
self.loop_patch.start()
|
|
30
|
+
|
|
31
|
+
def tearDown(self):
|
|
32
|
+
try:
|
|
33
|
+
for loader in self.loaders:
|
|
34
|
+
loader.executor.shutdown(wait=True)
|
|
35
|
+
for loop in self.loops:
|
|
36
|
+
if not loop.is_closed():
|
|
37
|
+
pending = asyncio.all_tasks(loop)
|
|
38
|
+
for task in pending:
|
|
39
|
+
task.cancel()
|
|
40
|
+
if pending:
|
|
41
|
+
loop.run_until_complete(asyncio.gather(
|
|
42
|
+
*pending, return_exceptions=True))
|
|
43
|
+
loop.close()
|
|
44
|
+
finally:
|
|
45
|
+
self.loop_patch.stop()
|
|
46
|
+
asyncio.set_event_loop(None)
|
|
47
|
+
|
|
48
|
+
def new_loop(self):
|
|
49
|
+
loop = self.original_new_event_loop()
|
|
50
|
+
self.loops.append(loop)
|
|
51
|
+
return loop
|
|
52
|
+
|
|
53
|
+
def construct(self, constructor, **kwargs):
|
|
54
|
+
if issubclass(constructor, JobLoader):
|
|
55
|
+
kwargs.setdefault('log_path', '.')
|
|
56
|
+
obj = constructor(**kwargs)
|
|
57
|
+
if isinstance(obj, JobLoader):
|
|
58
|
+
self.loaders.append(obj)
|
|
59
|
+
return obj
|
|
60
|
+
|
|
61
|
+
def assert_creates_loop(self, constructor):
|
|
62
|
+
obj = self.construct(constructor)
|
|
63
|
+
self.assertIsInstance(obj.loop, asyncio.AbstractEventLoop)
|
|
64
|
+
self.assertFalse(obj.loop.is_closed())
|
|
65
|
+
self.assertIs(asyncio.get_event_loop(), obj.loop)
|
|
66
|
+
|
|
67
|
+
def test_cronjob_creates_event_loop_outside_running_loop(self):
|
|
68
|
+
self.assert_creates_loop(CronJob)
|
|
69
|
+
|
|
70
|
+
def test_scheduler_creates_event_loop_outside_running_loop(self):
|
|
71
|
+
self.assert_creates_loop(Scheduler)
|
|
72
|
+
|
|
73
|
+
def test_jobloader_creates_event_loop_outside_running_loop(self):
|
|
74
|
+
self.assert_creates_loop(JobLoader)
|
|
75
|
+
|
|
76
|
+
def test_filejobloader_creates_event_loop_outside_running_loop(self):
|
|
77
|
+
self.assert_creates_loop(FileJobLoader)
|
|
78
|
+
|
|
79
|
+
def test_explicit_loop_is_used_without_changing_current_loop(self):
|
|
80
|
+
current = self.new_loop()
|
|
81
|
+
explicit = self.new_loop()
|
|
82
|
+
asyncio.set_event_loop(current)
|
|
83
|
+
with mock.patch('asyncio.get_event_loop',
|
|
84
|
+
side_effect=AssertionError('explicit loop ignored')):
|
|
85
|
+
for constructor in self.constructors:
|
|
86
|
+
with self.subTest(constructor=constructor.__name__):
|
|
87
|
+
self.assertIs(self.construct(constructor, loop=explicit).loop,
|
|
88
|
+
explicit)
|
|
89
|
+
self.assertIs(asyncio.get_event_loop(), current)
|
|
90
|
+
|
|
91
|
+
def test_existing_loop_is_reused(self):
|
|
92
|
+
current = self.new_loop()
|
|
93
|
+
asyncio.set_event_loop(current)
|
|
94
|
+
for constructor in self.constructors:
|
|
95
|
+
with self.subTest(constructor=constructor.__name__):
|
|
96
|
+
self.assertIs(self.construct(constructor).loop, current)
|
|
97
|
+
self.assertEqual(len(self.loops), 1)
|
|
98
|
+
|
|
99
|
+
def test_running_loop_is_used_inside_coroutine(self):
|
|
100
|
+
async def check():
|
|
101
|
+
for constructor in self.constructors:
|
|
102
|
+
with self.subTest(constructor=constructor.__name__):
|
|
103
|
+
obj = self.construct(constructor)
|
|
104
|
+
self.assertIs(obj.loop, asyncio.get_running_loop())
|
|
105
|
+
|
|
106
|
+
asyncio.run(check())
|
|
107
|
+
|
|
108
|
+
def test_objects_share_one_implicitly_created_loop(self):
|
|
109
|
+
objects = [self.construct(constructor)
|
|
110
|
+
for constructor in self.constructors]
|
|
111
|
+
self.assertTrue(all(obj.loop is objects[0].loop for obj in objects))
|
|
112
|
+
self.assertEqual(len(self.loops), 1)
|
|
113
|
+
|
|
114
|
+
def test_construction_after_asyncio_run(self):
|
|
115
|
+
for constructor in self.constructors:
|
|
116
|
+
with self.subTest(constructor=constructor.__name__):
|
|
117
|
+
asyncio.run(asyncio.sleep(0))
|
|
118
|
+
self.assert_creates_loop(constructor)
|
|
119
|
+
|
|
120
|
+
def test_worker_thread_installs_its_own_loop(self):
|
|
121
|
+
for constructor in self.constructors:
|
|
122
|
+
with self.subTest(constructor=constructor.__name__):
|
|
123
|
+
results = []
|
|
124
|
+
errors = []
|
|
125
|
+
|
|
126
|
+
def check():
|
|
127
|
+
try:
|
|
128
|
+
obj = self.construct(constructor)
|
|
129
|
+
results.append(obj.loop is asyncio.get_event_loop())
|
|
130
|
+
except Exception as error:
|
|
131
|
+
errors.append(error)
|
|
132
|
+
|
|
133
|
+
worker = threading.Thread(target=check)
|
|
134
|
+
worker.start()
|
|
135
|
+
worker.join(timeout=5)
|
|
136
|
+
self.assertFalse(worker.is_alive())
|
|
137
|
+
self.assertEqual(errors, [])
|
|
138
|
+
self.assertEqual(results, [True])
|
|
139
|
+
with self.assertRaises(RuntimeError):
|
|
140
|
+
asyncio.get_event_loop()
|
|
141
|
+
|
|
142
|
+
def test_implicit_loop_executes_sync_and_async_jobs(self):
|
|
143
|
+
scheduler = self.construct(Scheduler)
|
|
144
|
+
seen = []
|
|
145
|
+
|
|
146
|
+
async def callback():
|
|
147
|
+
await asyncio.sleep(0)
|
|
148
|
+
seen.append('async')
|
|
149
|
+
|
|
150
|
+
for name, function in [('sync', lambda: seen.append('sync')),
|
|
151
|
+
('async', callback)]:
|
|
152
|
+
job = self.construct(CronJob, name=name, run_total=1)
|
|
153
|
+
scheduler.add_job(job.second.go(function))
|
|
154
|
+
|
|
155
|
+
async def check():
|
|
156
|
+
await scheduler.check_jobs()
|
|
157
|
+
tasks = asyncio.all_tasks() - {asyncio.current_task()}
|
|
158
|
+
await asyncio.gather(*tasks)
|
|
159
|
+
await scheduler.check_jobs()
|
|
160
|
+
|
|
161
|
+
scheduler.loop.run_until_complete(check())
|
|
162
|
+
self.assertEqual(seen, ['sync', 'async'])
|
|
163
|
+
self.assertEqual(scheduler.jobs, {})
|
|
164
|
+
|
|
165
|
+
def test_scheduler_repeats_and_removes_jobs(self):
|
|
166
|
+
scheduler = self.construct(Scheduler, check_interval=0.005)
|
|
167
|
+
seen = []
|
|
168
|
+
job = self.construct(CronJob, run_total=3)
|
|
169
|
+
scheduler.add_job(job.every(0.02).second.go(seen.append, 'ran'))
|
|
170
|
+
|
|
171
|
+
async def check():
|
|
172
|
+
runner = asyncio.create_task(scheduler.start())
|
|
173
|
+
try:
|
|
174
|
+
async def wait_for_runs():
|
|
175
|
+
while len(seen) < 3:
|
|
176
|
+
await asyncio.sleep(0.005)
|
|
177
|
+
|
|
178
|
+
await asyncio.wait_for(wait_for_runs(), timeout=2)
|
|
179
|
+
finally:
|
|
180
|
+
runner.cancel()
|
|
181
|
+
with contextlib.suppress(asyncio.CancelledError):
|
|
182
|
+
await runner
|
|
183
|
+
|
|
184
|
+
scheduler.loop.run_until_complete(check())
|
|
185
|
+
self.assertEqual(seen, ['ran'] * 3)
|
|
186
|
+
self.assertEqual(scheduler.jobs, {})
|
|
187
|
+
|
|
188
|
+
def test_standalone_loaders_execute_subprocess(self):
|
|
189
|
+
for constructor in (JobLoader, FileJobLoader):
|
|
190
|
+
for thread in (True, False):
|
|
191
|
+
with self.subTest(constructor=constructor.__name__, thread=thread):
|
|
192
|
+
asyncio.set_event_loop(None)
|
|
193
|
+
with mock.patch('async_cron.job_loader.ProcessPoolExecutor',
|
|
194
|
+
side_effect=lambda: ProcessPoolExecutor(
|
|
195
|
+
max_workers=1)):
|
|
196
|
+
loader = self.construct(constructor, thread=thread)
|
|
197
|
+
with tempfile.TemporaryDirectory() as directory:
|
|
198
|
+
future = loader.create_executor_task(
|
|
199
|
+
command=sys.executable + ',-c,print("loader-ok")',
|
|
200
|
+
env=os.environ.copy(), name='probe',
|
|
201
|
+
log_path=directory)
|
|
202
|
+
self.assertIsNotNone(future)
|
|
203
|
+
loader.loop.run_until_complete(
|
|
204
|
+
asyncio.wait_for(future, timeout=10))
|
|
205
|
+
self.assertEqual(
|
|
206
|
+
(Path(directory) / 'probe_out.txt').read_text(),
|
|
207
|
+
'loader-ok\n')
|
|
208
|
+
self.assertEqual(
|
|
209
|
+
(Path(directory) / 'probe_err.txt').read_text(), '')
|
|
210
|
+
loader.executor.shutdown(wait=True)
|
|
211
|
+
|
|
212
|
+
def test_standalone_filejobloader_loads_file(self):
|
|
213
|
+
with tempfile.TemporaryDirectory() as directory:
|
|
214
|
+
path = Path(directory) / 'crontab'
|
|
215
|
+
path.write_text('')
|
|
216
|
+
loader = self.construct(FileJobLoader, file_path=str(path))
|
|
217
|
+
self.assertEqual(loader.loop.run_until_complete(loader.load()), [])
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
if __name__ == '__main__':
|
|
221
|
+
unittest.main()
|