-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.py
50 lines (39 loc) · 1.47 KB
/
scheduler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from fuck import work
import aioredis
import asyncio
import sys
import os
async def get_with_userid(redis, key):
result = await redis.get(key)
return key[len('fuck-'):], result
async def fuck_job():
connection = await aioredis.create_redis_pool('redis://fuck-checkin-redis:6379', encoding='utf-8')
redis = aioredis.Redis(pool_or_conn=connection)
cookies = [get_with_userid(redis, key) for key in (await redis.keys('fuck-*'))]
result = [(await work(userid, cookie)) for (userid, cookie) in (await asyncio.gather(*cookies))]
# failed = filter(lambda x: not x[1], result)
# delete_tasks = await asyncio.gather(*[redis.delete(f"fuck-{fail[0]}") for fail in failed])
# result = list(filter(lambda x: x[1], result))
redis.close()
await redis.wait_closed()
if len(result) == 0:
print(result)
else:
for line in result:
print(line)
sys.stdout.flush()
with open('/data/scheduler.log.txt', 'a') as f:
for i in result:
f.write(str(i))
f.write('\n')
if __name__ == "__main__":
INTERVAL = int(os.environ.get('SCHEDULER_INTERVAL', 15))
scheduler = AsyncIOScheduler()
scheduler.add_job(func=fuck_job, trigger='interval', seconds=INTERVAL)
scheduler.start()
print("Scheduler Starting...")
try:
asyncio.get_event_loop().run_forever()
except (KeyboardInterrupt, SystemExit):
pass