-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscheduler.py
40 lines (34 loc) · 1.11 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
import os
import gridfs
from pymongo import MongoClient
from uuid import uuid4
from datetime import datetime
class scheduler:
def __init__(self):
client = MongoClient(os.getenv('MONGO_URL'))
self.db = client['taskmaster']
# self.schedule("tasks")
def schedule(self, directory):
db = self.db
tasks = db['tasks']
fs=gridfs.GridFS(db)
for name in os.listdir(directory):
uuid=str(uuid4())
if not name.endswith('.pdf'):
continue
with open(os.path.join(directory, name), 'rb') as f:
fs.put(f, filename=name,_id=uuid)
tasks.create_index("scheduled_at")
tasks.insert_one({
"_id": uuid,
"command": name,
"scheduled_at": datetime.now(),
"picked_at": None,
"started_at": None,
"completed_at": None,
"completed_by": None
})
print("All PDF files have been uploaded to MongoDB and tasks have been scheduled.")
#the below code is for testing purposes just uncomment it to test the scheduler alone
# s=scheduler()
# # s.schedule("tasks")