GithubHelp home page GithubHelp logo

amisadmin / fastapi-scheduler Goto Github PK

View Code? Open in Web Editor NEW
79.0 2.0 13.0 70 KB

FastAPI-Scheduler is a simple scheduled task management FastAPI extension based on APScheduler.

Python 100.00%
fastapi fastapi-amis-admin apscheduler fastapi-scheduler

fastapi-scheduler's Introduction

FastAPI-Scheduler

Package version Downloads Chat on Gitter 229036692

## Project Introduction

FastAPI-Scheduler is a simple scheduled task management FastAPI extension library based on APScheduler.

Install

pip install fastapi-scheduler

Simple example

main.py:

from fastapi import FastAPI
from fastapi_amis_admin.admin.settings import Settings
from fastapi_amis_admin.admin.site import AdminSite
from datetime import date
from fastapi_scheduler import SchedulerAdmin

# Create `FastAPI` application
app = FastAPI()

# Create `AdminSite` instance
site = AdminSite(settings=Settings(database_url_async='sqlite+aiosqlite:///amisadmin.db'))

# # Custom timed task scheduler
# from apscheduler.schedulers.asyncio import AsyncIOScheduler
# from apscheduler.jobstores.redis import RedisJobStore
# # Use `RedisJobStore` to create a job store
# scheduler = AsyncIOScheduler(jobstores={'default':RedisJobStore(db=2,host="127.0.0.1",port=6379,password="test")})
# scheduler = SchedulerAdmin.bind(site,scheduler=scheduler)

# Create an instance of the scheduled task scheduler `SchedulerAdmin`
scheduler = SchedulerAdmin.bind(site)


# Add scheduled tasks, refer to the official documentation: https://apscheduler.readthedocs.io/en/master/
# use when you want to run the job at fixed intervals of time
@scheduler.scheduled_job('interval', seconds=60)
def interval_task_test():
    print('interval task is run...')


# use when you want to run the job periodically at certain time(s) of day
@scheduler.scheduled_job('cron', hour=3, minute=30)
def cron_task_test():
    print('cron task is run...')


# use when you want to run the job just once at a certain point of time
@scheduler.scheduled_job('date', run_date=date(2022, 11, 11))
def date_task_test():
    print('date task is run...')


@app.on_event("startup")
async def startup():
    # Mount the background management system
    site.mount_app(app)
    # Start the scheduled task scheduler
    scheduler.start()


if __name__ == '__main__':
    import uvicorn

    uvicorn.run(app, debug=True)

Interface/UI preview

  • Open http://127.0.0.1:8000/admin/ in your browser:

SchedulerAdmin

Dependent projects

agreement

The project follows the Apache2.0 license agreement.

fastapi-scheduler's People

Contributors

amisadmin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

fastapi-scheduler's Issues

Hello, may I ask why two identical tasks are generated?

Hello, may I ask why two identical tasks are generated?
2022-11-13 11:22:22,464 - uvicorn.error - INFO - Started reloader process [27370] using statreload
2022-11-13 11:22:24,330 - apscheduler.scheduler - INFO - Adding job tentatively -- it will be properly scheduled when the scheduler starts
2022-11-13 11:22:24,348 - apscheduler.scheduler - INFO - Adding job tentatively -- it will be properly scheduled when the scheduler starts
INFO: Started server process [27377]
2022-11-13 11:22:24,349 - uvicorn.error - INFO - Started server process [27377]
INFO: Waiting for application startup.
2022-11-13 11:22:24,350 - uvicorn.error - INFO - Waiting for application startup.
2022-11-13 11:22:24,419 - apscheduler.scheduler - INFO - Added job "cron_task_test" to job store "default"
2022-11-13 11:22:24,419 - apscheduler.scheduler - INFO - Added job "cron_task_test" to job store "default"
2022-11-13 11:22:24,419 - apscheduler.scheduler - INFO - Scheduler started

How to add new job from admin scheduler panel.

I created a endpoint to add new jobs for one particular function passing two parameters: job_name and time variables.

After run the fastapi, i can add new job through this endpoint from doc page.

But i would like to add those jobs through amis admin panel like scheduler page where you only can run, pause or delete jobs and can't do modify or create jobs...

Thanks!

Wrong datetime Next Run Time

Hi, next run time is +6h of my time in admin console. How can i fix this?

I can’t see any time zone option in settings.

thanks

Cron schedule at different time but running at different time.

I have scheduled cron at (Hour=3 and minutes=30) as you can see below code

@scheduler.scheduled_job('cron', hour=3, minute=30)
def cron_task_test():
print('cron task is run...3:30 ')

But in the cron admin panel, you can see that it is scheduled at 1:00 and it running also at 1:00

image

Could you please tell me what going wrong here and how to fix it.

Occurs an error and does not work.

I installed the necessary packages and copied and executed the script provided as an example, but encountered an error.

Below is stack trace:

INFO:     Will watch for changes in these directories: ['/Users/***']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [92055] using StatReload
INFO:     Started server process [92059]
INFO:     Waiting for application startup.
ERROR:    Traceback (most recent call last):
  File "/Users/***/.venv/lib/python3.12/site-packages/starlette/routing.py", line 686, in lifespan
    async with self.lifespan_context(app) as maybe_state:
  File "/Users/***/.venv/lib/python3.12/site-packages/starlette/routing.py", line 575, in __aenter__
    await self._router.startup()
  File "/Users/***/.venv/lib/python3.12/site-packages/starlette/routing.py", line 663, in startup
    await handler()
  File "/Users/***/src/main.py", line 33, in startup
    site.mount_app(app)
  File "/Users/***/.venv/lib/python3.12/site-packages/fastapi_amis_admin/admin/admin.py", line 1541, in mount_app
    fastapi.add_middleware(BaseHTTPMiddleware, dispatch=self.db.asgi_dispatch)
  File "/Users/***/.venv/lib/python3.12/site-packages/starlette/applications.py", line 139, in add_middleware
    raise RuntimeError("Cannot add middleware after an application has started")
RuntimeError: Cannot add middleware after an application has started

ERROR:    Application startup failed. Exiting.

Need help. How can I resolve this problem?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.