All Projects → m-barthelemy → vapor-queues-fluent-driver

m-barthelemy / vapor-queues-fluent-driver

Licence: MIT License
A Fluent implementation for https://github.com/vapor/queues (Vapor4)

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to vapor-queues-fluent-driver

VaporCRUDRouter
A Rails-inspired extension to Vapor's routing system
Stars: ✭ 58 (+241.18%)
Mutual labels:  vapor-swift, vapor-4
swift-extras-base64
Base64 encode and decode without the use of Foundation in pure Swift.
Stars: ✭ 65 (+282.35%)
Mutual labels:  swift-server
JSONAPISerializer
JSONAPISerializer for Server Side Swift
Stars: ✭ 21 (+23.53%)
Mutual labels:  vapor-swift
spec
Unit testing Vapor 4 applications through declarative specifications.
Stars: ✭ 32 (+88.24%)
Mutual labels:  vapor-4
moleculer-channels
Reliable messages for Moleculer services via external queue/channel/topic.
Stars: ✭ 45 (+164.71%)
Mutual labels:  queues
Data-Structures-Algorithms-Handbook
A series of important questions with solutions to crack the coding interview and ace it!
Stars: ✭ 30 (+76.47%)
Mutual labels:  queues
swift-lambda-runtime
⚠️ Deprecated AWS Lambda Runtime - please use https://github.com/swift-server/swift-aws-lambda-runtime instead
Stars: ✭ 68 (+300%)
Mutual labels:  swift-server
zenaton-ruby
💎 Ruby gem to run and orchestrate background jobs with Zenaton Workflow Engine
Stars: ✭ 32 (+88.24%)
Mutual labels:  queues
VaporCoin
A peer-to-peer blockchain ledger, built with Swift, using Vapor
Stars: ✭ 68 (+300%)
Mutual labels:  vapor-swift
submissions
Provides a common structure to deal with data based API requests
Stars: ✭ 15 (-11.76%)
Mutual labels:  vapor-4
Problem-Solving
contains all coding interview practice problems, data structures and algorithms implementations. 👨‍💻👨‍💻💥 🚩
Stars: ✭ 14 (-17.65%)
Mutual labels:  queues
swift-nio-redis
A high performance Redis protocol (RESP) implementation for SwiftNIO
Stars: ✭ 27 (+58.82%)
Mutual labels:  swift-server
swift-server-app
Server app with Swift and Docker
Stars: ✭ 18 (+5.88%)
Mutual labels:  swift-server
filequeue
light weight, high performance, simple, reliable and persistent queue for Java applications
Stars: ✭ 35 (+105.88%)
Mutual labels:  queues
1063-Data-Structures
T-Th 11:00 - 12:20
Stars: ✭ 13 (-23.53%)
Mutual labels:  queues
redi-s
A performant Redis server implemented in SwiftNIO.
Stars: ✭ 69 (+305.88%)
Mutual labels:  swift-server
MicroExpress
A micro web server framework on top of Swift NIO
Stars: ✭ 125 (+635.29%)
Mutual labels:  swift-server
VaporElasticsearch
A Vapor/Swift Elasticsearch client
Stars: ✭ 26 (+52.94%)
Mutual labels:  vapor-swift
template
A Vapor template for convenient and fast scaffolding 🏎
Stars: ✭ 33 (+94.12%)
Mutual labels:  vapor-swift
Swift-3-Functional-Programming
Code repository for Swift 3 Functional Programming, published by Packt
Stars: ✭ 78 (+358.82%)
Mutual labels:  vapor-swift

QueuesFluentDriver

This Vapor Queues driver is an alternative to the (default) Redis driver, allowing you to use Fluent to store the Queues jobs into your relational database.

Compatibility

This package makes use of some relatively recent, non standard SQL extensions added to some major database engines to support this exact use case: queuing systems, where there must be a guarantee that a task or job won't be picked by multiple workers.

This package should be compatible with:

  • Postgres >= 11
  • Mysql >= 8.0.1
  • MariaDB >= 10.3

Sqlite will only work if you have a custom, very low number of Queues workers (1-2), which makes it useless except for testing purposes

Postgres: This package relies on some recently added features in sql-kit and postgres-kit >= 2.1.0. Make sure you use a release of postgres-kit that is at least 2.1.0

 

Usage

Add it to the Package.swift of your Vapor4 project:

// swift-tools-version:5.2
import PackageDescription

let package = Package(
    name: "app",
    platforms: [
        .macOS(.v10_15)
    ],
    ...
    dependencies: [
        ...
        .package(name: "QueuesFluentDriver", url: "https://github.com/m-barthelemy/vapor-queues-fluent-driver.git", from: "1.2.0"),
        ...
    ],
    targets: [
        .target(name: "App", dependencies: [
            ...
            .product(name: "QueuesFluentDriver", package: "QueuesFluentDriver"),
            ...
        ]),
        ...
    ]
)

 

This package needs a table, named _jobs by default, to store the Vapor Queues jobs. Add JobModelMigrate to your migrations:

// Ensure the table for storing jobs is created
app.migrations.add(JobModelMigrate())

 

Finally, load the QueuesFluentDriver driver:

app.queues.use(.fluent())

Make sure you call app.databases.use(...) before calling app.queues.use(.fluent())!

 

Options

Using a custom Database

You can optionally create a dedicated Database, set to isdefault: false and with a custom DatabaseID and use it for your Queues. In that case you would initialize the Queues configuration like this:

let queuesDb = DatabaseID(string: "my_queues_db")
app.databases.use(.postgres(configuration: dbConfig), as: queuesDb, isDefault: false)
app.queues.use(.fluent(queuesDb))

Customizing the jobs table name

By default the JobModelMigrate migration will create a table named _jobs. You can customize the name during the migration :

app.migrations.add(JobModelMigrate(schema: "vapor_queues"))

Listing jobs

If needed, you can list the jobs stored into the database:

import QueuesFluentDriver

let queue = req.queue as! FluentQueue

// Get the pending jobs
queue.list()

// Get the ones currently running
queue.list(state: .processing)

// Get the completed ones (only if you didn't set `useSoftDeletes` to `false`)
queue.list(state: .completed)

// For a custom Queue
queue.list(queue: "myCustomQueue")

 

Caveats

Polling interval and number of workers

By default, the Vapor Queues package creates 2 workers per CPU core, and each worker would periodically poll the database for jobs to be run. On a recent 4 cores MacBook, this means 8 workers querying the database every second by default.

You can change the jobs polling interval by calling:

app.queues.configuration.refreshInterval = .seconds(custom_value)

With Queues >=1.4.0, you can also configure the number of workers that will be started by setting app.queues.configuration.workerCount

Soft Deletes

By default, this driver uses Fluent's "soft delete" feature, meaning that completed jobs stay in the database, but with a non-null deleted_at value. If you want to delete the entry as soon as job is completed, you can set the useSoftDeletes option to false:

app.queues.use(.fluent(useSoftDeletes: false))

When using the default soft deletes option, it is probably a good idea to cleanup the completed jobs from time to time.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].