import { Schema, model, Document, Model } from 'mongoose';
// 1. Create an inte
export interface ICronjob {
name: string;
tasks: Array<string>;
flows: Array<string>;
accounts: Array<string>;
status: string;
lastExecuteTime: Date;
period: string;
cron: string;
policy: any;
workflow: string;
createdAt: Date; // 添加 createdAt 字段
updatedAt: Date; // 添加 updatedAt 字段
}
type CronjobModel = Model<ICronjob, {}>;
// 2. Create a Schema corresponding to the document interface.
const cronjobSchema = new Schema<ICronjob, CronjobModel>({
name: { type: String },
accounts: { type: Schema.Types.Mixed },
tasks: { type: Schema.Types.Mixed },
flows: { type: Schema.Types.Mixed },
period: { type: String },
cron: { type: String },
workflow: {type: String},
lastExecuteTime: { type: Schema.Types.Date },
policy: { type: Schema.Types.Mixed },
status: { type: String },
createdAt: {type: Schema.Types.Date},
updatedAt: {type: Schema.Types.Date},
});
// 3. Create a Model.
const Cronjob = model<ICronjob, CronjobModel>('Cronjob', cronjobSchema);
export const findById = async (id: string):Promise<any> => {
const [job] = await Cronjob.find().where('_id').equals(id);
if (!job) {
return null;
}
if (job.status == 'stop') {
return null;
}
return job;
}
const [job] = await Cronjob.find().where('_id').equals(id);
Error message when querying MongoDB via mongoosejs while writing workflow using Typescript SDK:
2023-03-29T05:57:27.935Z [WARN] Workflow failed {
error: TypeError: Cronjob.find is not a function
tao
March 29, 2023, 12:14pm
2
Hi @baxiaoshi , I’m not fully convinced that the error is to do with Temporal.
Were you able to run the code successfully without the workflow?
Yes, running the function outside of Temporal works fine.
@baxiaoshi where are you placing this code?
any communication with an external system should be placed in activities. Can you share a reproducible piece (workflow/activity code included)
I didn’t wrap the database query as an activity; I used the database query in the function below. Why do I need to register the database query as an activity?
export async function executeCronJob(id: string):Promise<any> {
I am not familiar with TypeScript.
activity code
export async function executeCronJob(id: string):Promise<any> {
let instance = await findCronJobById(id)
if (instance == undefined) {
throw new Error('cronjob not found')
}
const accounts = await account.findByLabels(instance.accounts);
if (accounts == null || accounts.length == 0) {
throw new Error('accounts not found')
}
const allTasks = await tasks.findByNames(instance.tasks);
if (allTasks == null || allTasks.length == 0) {
throw new Error('tasks not found')
}
workflow code
export async function asyncCronJobyWorkflow(job: string): Promise<string> {
const result = await executeCronJob(job);
return `result: ${result}`
}
I only registered one activity, and I didn’t register any of the other functions I called.
Thank you @baxiaoshi
I only registered one activity, and I didn’t register any of the other functions I called.
If you have registered executeCronJob
it should work,
I have never used oclif, Can you run the workflow code outside of the oclif
project?
I am not familiar with TypeScript.
if you mean with the sdk-typescript, we just launched this course that might help Temporal 101 with TypeScript | Learn Temporal
I have already completed the 101 course, but I am still unclear as to why all functions must be registered as Activities. I didn’t need to do this when I was using the Golang SDK.
Are you calling executeCronJob
directly or is this a proxied activity function created using proxyActivities
?