Ver código fonte

Add basic cron resource

Oscar Alfredo Leiva Salomón 6 anos atrás
pai
commit
2c8b3f6346
5 arquivos alterados com 77 adições e 8 exclusões
  1. 2 0
      app/__init__.py
  2. 17 1
      app/main/__init__.py
  3. 57 0
      app/main/resources/cron.py
  4. 0 6
      app/main/resources/foo.py
  5. 1 1
      manage.py

+ 2 - 0
app/__init__.py

@@ -1,8 +1,10 @@
 from flask_restful import Api
 from flask import Blueprint
 from app.main.resources.foo import Foo
+from app.main.resources.cron import Cron
 
 api_bp = Blueprint('api', __name__)
 api = Api(api_bp)
 
 api.add_resource(Foo, '/Foo')
+api.add_resource(Cron, '/cron', '/cron/<job_id>')

+ 17 - 1
app/main/__init__.py

@@ -1,9 +1,25 @@
 from flask import Flask
 from app.main.config import config_by_name
+from apscheduler.schedulers.background import BackgroundScheduler
+from apscheduler.jobstores.mongodb import MongoDBJobStore
+from pymongo import MongoClient
+
+client = MongoClient('mongodb://mailScheduler:pqowieuryt@192.168.100.5:27017/Medidores')
+
+
+jobstores = {
+    'default': MongoDBJobStore(database='Medidores',
+                               collection='mail_scheduler',
+                               client=client)
+}
+
+scheduler = BackgroundScheduler({
+    'apscheduler.timezone': 'America/El_Salvador'
+    }, jobstores=jobstores)
 
 
 def create_app(config_name):
     app = Flask(__name__)
     app.config.from_object(config_by_name[config_name])
-
+    scheduler.start()
     return app

+ 57 - 0
app/main/resources/cron.py

@@ -0,0 +1,57 @@
+from flask_restful import Resource, request
+from app.main import scheduler
+import json
+from datetime import datetime
+
+
+def printing_something(text):
+    print("printing {0} at {1}".format(text, datetime.now()))
+
+
+class Cron(Resource):
+    def get(self):
+
+        jobs = []
+
+        for job in scheduler.get_jobs():
+            jobdict = {}
+            crontab = {}
+            jobdict['id'] = job.id
+            jobdict['name'] = job.name
+            jobdict['args'] = str(job.args)
+            jobdict['next_run_time'] = str(job.next_run_time)
+
+            for f in job.trigger.fields:
+                cuarval = str(f)
+                crontab[f.name] = cuarval
+
+            jobdict['crontab'] = crontab
+            jobs.append(jobdict)
+
+        return {'jobs': jobs}
+
+    def post(self):
+        json_data = request.get_json()
+        if not json_data:
+            return {'message': 'No input data provided'}, 400
+
+        day = '*' if not json_data.get('day') else json_data.get('day')
+        hour = '*' if not json_data.get('hour') else json_data.get('hour')
+        minute = '*' if not json_data.get('minute') else json_data.get('minute')
+        text = json_data.get('text')
+
+        job = scheduler.add_job(printing_something, trigger='cron',
+                                day=day, hour=hour, minute=minute, args=[text])
+
+        return "job details: {0}".format(job)
+
+    def delete(self, job_id):
+        # json_data = request.get_json()
+        # if not json_data:
+        #     return {'message': 'No input data provided'}, 400
+
+        # jobid = json_data.get('id')
+
+        resp = scheduler.remove_job(job_id=job_id)
+
+        return resp

+ 0 - 6
app/main/resources/foo.py

@@ -1,6 +0,0 @@
-from flask_restful import Resource
-
-
-class Foo(Resource):
-    def get(self):
-        return {"message": "Hello, World!"}

+ 1 - 1
manage.py

@@ -2,7 +2,7 @@ import os
 from flask_script import Manager
 
 from app import api_bp
-from app.main import create_app
+from app.main import create_app, scheduler
 
 
 app = create_app(os.getenv('BOILERPLATE_ENV') or 'dev')