diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml deleted file mode 100644 index b2ca059..0000000 --- a/.github/FUNDING.yml +++ /dev/null @@ -1,3 +0,0 @@ -# These are supported funding model platforms -liberapay: actumn # Replace with a single Liberapay username -custom: paypal.me/actumn # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md deleted file mode 100644 index e69de29..0000000 diff --git a/.github/ISSUE_TEMPLATE/Bug-Report.md b/.github/ISSUE_TEMPLATE/Bug-Report.md deleted file mode 100644 index 6cab417..0000000 --- a/.github/ISSUE_TEMPLATE/Bug-Report.md +++ /dev/null @@ -1,23 +0,0 @@ ---- -name: Bug Report -about: Is there something wrong with celery.node? -labels: "bug" ---- -## Description - -* **What is the current behavior?** - - - -* **What is the expected behavior?** - - - -* **Please tell us about your environment:** - - - Version: 0.0.0 - - OS: [Windows Server 2016 | OS X | linux] - - Language: [TypeScript X.X | ES6/7 | ES5] - - -* **Other information** (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. stackoverflow, gitter, etc) \ No newline at end of file diff --git a/.github/ISSUE_TEMPLATE/Feature-Request.md b/.github/ISSUE_TEMPLATE/Feature-Request.md deleted file mode 100644 index 04152ec..0000000 --- a/.github/ISSUE_TEMPLATE/Feature-Request.md +++ /dev/null @@ -1,11 +0,0 @@ ---- -name: Feature Request -about: Do you need a new feature? -labels: "enhancement" ---- -## Description -* **What is the motivation / use case for changing the behavior?** - -- **Proposed Behavior** - -* **Other information** (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. stackoverflow, gitter, etc) \ No newline at end of file diff --git a/.github/ISSUE_TEMPLATE/Free-Style.md b/.github/ISSUE_TEMPLATE/Free-Style.md deleted file mode 100644 index a8049b2..0000000 --- a/.github/ISSUE_TEMPLATE/Free-Style.md +++ /dev/null @@ -1,9 +0,0 @@ ---- -name: Free style -about: Anything about celery.node (suggestion, support request, document, etc.) -labels: "" ---- - -## Description \ No newline at end of file diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 3eba02f..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,20 +0,0 @@ -## Description - - -* **What kind of change does this PR introduce?** (Bug fix, feature, docs update, ...) - - - -* **What is the current behavior?** (You can also link to an open issue here) - - - -* **What is the new behavior (if this is a feature change)?** - - - -* **Other information**: - - diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml deleted file mode 100644 index b6121e8..0000000 --- a/.github/workflows/nodejs.yml +++ /dev/null @@ -1,43 +0,0 @@ -# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node -# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions - -name: build - -on: - push: - branches: [ master ] - pull_request: - branches: [ master ] - -jobs: - build: - - runs-on: ubuntu-latest - timeout-minutes: 20 - services: - redis: - image: redis - ports: - - 6379:6379 - options: --entrypoint redis-server - rabbitmq: - image: rabbitmq - ports: - - 5672:5672 - - strategy: - matrix: - node-version: [10.x, 12.x, 14.x] - - steps: - - uses: actions/checkout@v2 - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v1 - with: - node-version: ${{ matrix.node-version }} - - run: npm ci - - run: npm run lint - - run: npm run dist - - run: npm test - env: - CI: true diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml new file mode 100644 index 0000000..de909b2 --- /dev/null +++ b/.github/workflows/npm-publish.yml @@ -0,0 +1,24 @@ +# Github action which publish a package after commits to master +name: Post-commit publish + +# Controls when the workflow will run +on: + # Triggers the workflow on push or pull request events but only for the "master" branch + push: + branches: [ "master" ] + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +jobs: + npm-publish-and-notify: + uses: TechSeePublic/techsee-actions/.github/workflows/npm-publish.yml@main + with: + branch: master + use-slack: true + package-name: ${{ github.repository }} + secrets: + git-token: ${{ secrets.GITHUB_TOKEN }} + npm-token: ${{ secrets.NPM_PUBLISH_TOKEN }} + slack-success-webhookurl: ${{ secrets.SLACK_NPM_PUBLISH_WEBHOOK_URL }} + slack-failure-webhookurl: ${{ secrets.SLACK_NPM_PUBLISH_FAIL_WEBHOOK_URL }} diff --git a/package-lock.json b/package-lock.json index f564084..1c4ad85 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { - "name": "celery-node", - "version": "0.5.8", + "name": "@techsee/celery-node-mod", + "version": "1.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "celery-node", - "version": "0.5.8", + "name": "@techsee/celery-node-mod", + "version": "1.0.0", "license": "MIT", "dependencies": { "amqplib": "^0.8.0", diff --git a/package.json b/package.json index 70e6084..bf40ebe 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "celery-node", - "version": "0.5.9", + "name": "@techsee/celery-node-mod", + "version": "1.0.22", "description": "celery written in nodejs", "main": "dist/index.js", "types": "dist/index.d.ts", @@ -10,7 +10,7 @@ "format-check": "prettier --check \"{,!(node_modules)/**/}*.{js,ts}\"", "dist": "tsc", "test": "mocha -r ts-node/register ./test/**/*", - "prepublishOnly": "npm run dist & npm test" + "prepublishOnly": "npm run dist" }, "repository": { "type": "git", diff --git a/src/app/base.ts b/src/app/base.ts index f5f2eba..add51ec 100644 --- a/src/app/base.ts +++ b/src/app/base.ts @@ -2,9 +2,10 @@ * writes here Base Parent class of Celery client and worker * @author SunMyeong Lee */ -import { CeleryConf, defaultConf } from "./conf"; -import { newCeleryBroker, CeleryBroker } from "../kombu/brokers"; -import { newCeleryBackend, CeleryBackend } from "../backends"; +import {CeleryConf, defaultConf} from "./conf"; +import {CeleryBroker, newCeleryBroker} from "../kombu/brokers"; +import {CeleryBackend, newCeleryBackend} from "../backends"; +import {ICeleryStatus} from "../kombu/brokers/amqp"; export default class Base { private _backend: CeleryBackend; @@ -65,4 +66,18 @@ export default class Base { public disconnect(): Promise { return this.broker.disconnect().then(() => this.backend.disconnect()); } + + public status(): ICeleryStatus { + return this._broker ? this._broker.status() : ICeleryStatus.CLOSED; + } + + public addEventListener(event: string,listener: any): void { + this._broker.addEventListener(event,listener); + } + + public async connect() { + this.broker; + this.backend; + await this._broker.connect(); + } } diff --git a/src/kombu/brokers/amqp.ts b/src/kombu/brokers/amqp.ts index 719dae6..1b473f1 100644 --- a/src/kombu/brokers/amqp.ts +++ b/src/kombu/brokers/amqp.ts @@ -2,6 +2,7 @@ import * as amqplib from "amqplib"; import { CeleryBroker } from "."; import { Message } from "../message"; + class AMQPMessage extends Message { constructor(payload: amqplib.ConsumeMessage) { super( @@ -14,8 +15,15 @@ class AMQPMessage extends Message { } } +export enum ICeleryStatus { + DISCONNECTED = 'disconnected', + CONNECTED = 'connected', + CLOSED = 'closed' +} + export default class AMQPBroker implements CeleryBroker { - connect: Promise; + state: ICeleryStatus = ICeleryStatus.CLOSED; + connection: amqplib.Connection; channel: Promise; queue: string; @@ -26,10 +34,32 @@ export default class AMQPBroker implements CeleryBroker { * @param {object} opts the options object for amqp connect of amqplib * @param {string} queue optional. the queue to connect to. */ - constructor(url: string, opts: object, queue = "celery") { + constructor(private url: string, private opts: object, queue: string = "celery") { this.queue = queue; - this.connect = amqplib.connect(url, opts); - this.channel = this.connect.then(conn => conn.createChannel()); + } + + public addEventListener (event: string,listener: any) { + this.connection.on(event,listener); + } + + public connect() { + return new Promise(async (resolve,reject)=>{ + try { + this.connection = await amqplib.connect(this.url, this.opts); // Await the connection + this.connection.on('close',()=> { + if(this.state !== ICeleryStatus.CLOSED) { + this.state = ICeleryStatus.DISCONNECTED + } + }); + this.channel = this.connection.createChannel(); // Create the channel + this.state = ICeleryStatus.CONNECTED; + return resolve(); + } catch (err) { + this.state = ICeleryStatus.DISCONNECTED; + reject('Failed to connect AMQPBroker:'); + } + }) + } /** @@ -56,17 +86,22 @@ export default class AMQPBroker implements CeleryBroker { }) ]) .then(() => resolve()) - .catch(reject); + .catch(()=>reject()); }); }); } + public status(): ICeleryStatus { + return this.state; + } + /** * @method AMQPBroker#disconnect * @returns {Promise} promises that continues if amqp disconnected. */ public disconnect(): Promise { - return this.connect.then(conn => conn.close()); + this.state = ICeleryStatus.CLOSED; + return this.connection.close(); } /** diff --git a/src/kombu/brokers/index.ts b/src/kombu/brokers/index.ts index c1ec5fa..884b197 100644 --- a/src/kombu/brokers/index.ts +++ b/src/kombu/brokers/index.ts @@ -1,6 +1,7 @@ import * as url from "url"; import RedisBroker from "./redis"; -import AMQPBroker from "./amqp"; +import AMQPBroker, {ICeleryStatus} from "./amqp"; + export interface CeleryBroker { isReady: () => Promise; @@ -13,6 +14,9 @@ export interface CeleryBroker { properties: object ) => Promise; subscribe: (queue: string, callback: Function) => Promise; + status: () => ICeleryStatus + addEventListener: (event: string, listener: any) => void; + connect: () => Promise; } /** diff --git a/src/kombu/brokers/redis.ts b/src/kombu/brokers/redis.ts index 6f7fd3f..714011c 100644 --- a/src/kombu/brokers/redis.ts +++ b/src/kombu/brokers/redis.ts @@ -2,6 +2,7 @@ import * as Redis from "ioredis"; import { v4 } from "uuid"; import { CeleryBroker } from "."; import { Message } from "../message"; +import {ICeleryStatus} from "./amqp"; class RedisMessage extends Message { @@ -209,4 +210,15 @@ export default class RedisBroker implements CeleryBroker { return new RedisMessage(rawMsg); }); } + + addEventListener(event: string, listener: any): void { + } + + connect(): Promise { + return Promise.resolve(undefined); + } + + status(): ICeleryStatus { + return; + } }