dimanche 5 avril 2020

Calling annotated method

I have a system where I want some of my methods to be run periodically. So I have created an annotation like this:

@Service()
export class SensorResolver {
  constructor(private readonly client: HueClient) {}

  @Schedule("*/5 * * * * *")
  @Query(() => [Sensor])
  async getSensors(): Promise<[Sensor]> {
  ...
  }
}

export function Schedule(schedule: string) {
  return function(
    target: any,
    propertyKey: string,
    descriptor: PropertyDescriptor
  ) {
    new CronJob(
      schedule,
      () => {
        const scheduleClasses = [Container.get(SensorResolver)];
        scheduleClasses.forEach((schedule: any) => {
          logger.info("running schedule", {
            data: {
              keys: Object.keys(schedule),
              prototype: Object.getPrototypeOf(schedule)
            }
          });
        });
        logger.info("running schedule function");
      },
      null,
      true,
      "Europe/Stockholm"
    ).start();
    logger.info(
      `registered Schedule hook ${propertyKey} with schedule (${schedule})`,
      {
        data: {
          target,
          propertyKey,
          descriptor
        }
      }
    );
  };
}

so I would like to run the function on a schedule, but I can't seem to get a hold of it. The propertyKey is correct, but I can't find it in the prototype of schedule, and the target is just an empty object.

Am I missing something obvious?





Aucun commentaire:

Enregistrer un commentaire